只能写个大概的,要写数据到数据库中,先得在数据库中建库,库里建表,表里建字段,然后java里建立数据库连接,用SQL语言写数据到表中的字段
ClassforName("commicrosoftsqlserverjdbcSQLServerDriver")newInstance();
//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名"; //70、2000
String url="jdbc:sqlserver://localhost:1433;DatabaseName=数据库名"; //2005
Connection conn=null;
conn= DriverManagergetConnection(url,用户名,密码);
PreparedStatement pst=null;
pst=connprepareStatement("Insert Into grade(表名) Values ()");
pstsetInt(1,你要写的整弄数据);
//pstsetString(2,你要写的字符串数据);
pstaddBatch();
pstexecuteBatch();
前段时间正好做了,导入和导出的,你自己看吧
/导出数据为XLS格式
@param fos
@param bo
/
public void writeExcelBo(FileOutputStream fos, javautilVector ve)
{
jxlwriteWritableWorkbook wwb;
try
{
wwb= WorkbookcreateWorkbook(fos);
jxlwriteWritableSheet ws= wwbcreateSheet("booksheet", 10);
wsaddCell(new jxlwriteLabel(0, 1, "书目ID"));
wsaddCell(new jxlwriteLabel(1, 1, "ISBN"));
wsaddCell(new jxlwriteLabel(2, 1, "定价"));
wsaddCell(new jxlwriteLabel(3, 1, "书名"));
wsaddCell(new jxlwriteLabel(4, 1, "原书名"));
wsaddCell(new jxlwriteLabel(5, 1, "副题名"));
wsaddCell(new jxlwriteLabel(6, 1, "著者"));
wsaddCell(new jxlwriteLabel(7, 1, "译者"));
wsaddCell(new jxlwriteLabel(8, 1, "版次"));
wsaddCell(new jxlwriteLabel(9, 1, "出版地"));
wsaddCell(new jxlwriteLabel(10, 1, "出版社"));
wsaddCell(new jxlwriteLabel(11, 1, "出版日期"));
wsaddCell(new jxlwriteLabel(12, 1, "页数"));
wsaddCell(new jxlwriteLabel(13, 1, "书高"));
wsaddCell(new jxlwriteLabel(14, 1, "装帧"));
wsaddCell(new jxlwriteLabel(15, 1, "丛书名"));
wsaddCell(new jxlwriteLabel(16, 1, "一般性附注项"));
wsaddCell(new jxlwriteLabel(17, 1, "简介"));
wsaddCell(new jxlwriteLabel(18, 1, "主题词"));
wsaddCell(new jxlwriteLabel(19, 1, "中图法分类"));
wsaddCell(new jxlwriteLabel(20, 1, "更新日期"));
wsaddCell(new jxlwriteLabel(21, 1, "本数"));
book=new Book[vesize()];
for (int i= 0; i < vesize(); i++)
{
book[i]= (Book)veget(i);
wsaddCell(new jxlwriteLabel(0, i + 2, "" + book[i]getBookId()));
wsaddCell(new jxlwriteLabel(1, i + 2, book[i]getIsbn()));
wsaddCell(new jxlwriteLabel(2, i + 2, "" + book[i]getPrice()));
wsaddCell(new jxlwriteLabel(3, i + 2, book[i]getBookTitle()));
wsaddCell(new jxlwriteLabel(4, i + 2, book[i]getOldFilename()));
wsaddCell(new jxlwriteLabel(5, i + 2, book[i]getSubTitle()));
wsaddCell(new jxlwriteLabel(6, i + 2, book[i]getWriter()));
wsaddCell(new jxlwriteLabel(7, i + 2, book[i]getTranscribe()));
wsaddCell(new jxlwriteLabel(8, i + 2, "" + book[i]getVersion()));
wsaddCell(new jxlwriteLabel(9, i + 2, book[i]getPublishCity()));
wsaddCell(new jxlwriteLabel(10, i + 2, book[i]getPublisher()));
wsaddCell(new jxlwriteLabel(11, i + 2, book[i]getPublishDate()toString()));
wsaddCell(new jxlwriteLabel(12, i + 2, "" + book[i]getPage()));
wsaddCell(new jxlwriteLabel(13, i + 2, "" + book[i]getHight()));
wsaddCell(new jxlwriteLabel(14, i + 2, book[i]getInstall()));
wsaddCell(new jxlwriteLabel(15, i + 2, book[i]getSeries()));
wsaddCell(new jxlwriteLabel(16, i + 2, book[i]getNotes()));
wsaddCell(new jxlwriteLabel(17, i + 2, book[i]getPrecisnotes()));
wsaddCell(new jxlwriteLabel(18, i + 2, book[i]getSubject()));
wsaddCell(new jxlwriteLabel(19, i + 2, book[i]getCls()replaceAll("_", "")));
wsaddCell(new jxlwriteLabel(20, i + 2, book[i]getUpdatedate()toString()));
wsaddCell(new jxlwriteLabel(21, i + 2, "0"));
}
jxlwriteWritableFont wfc=
new jxlwriteWritableFont(
WritableFontARIAL,
255,
WritableFontBOLD,
false,
UnderlineStyleNO_UNDERLINE,
jxlformatColourBLACK);
jxlwriteWritableCellFormat wcfFC= new jxlwriteWritableCellFormat(wfc);
wsaddCell(new jxlwriteLabel(0, 0, "为保证您提交定单的稳定和正确,导入定单时候请勿更改此表格式(请勿更改书目ID,订购本数自行添加!)"));
wwbwrite();
//关闭Excel工作薄对象
wwbclose();
} catch (IOException e)
{} catch (RowsExceededException e)
{} catch (WriteException e)
{}
}
//导入EXCEL
if (fgetName()indexOf("xls") > 0)
{
try
{
fis= new FileInputStream(f);
BookBean bob= new BookBean();
UserBean usb= new UserBean();
jxlWorkbook rwb= WorkbookgetWorkbook(fis);
jxlSheet sh= rwbgetSheet(0);
int rowCount= shgetRows();
SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
book= new Book[rowCount - 1];
for (int i= 1; i < rowCount; i++)
{
book[i - 1]= new Book();
jxlCell[] ce= shgetRow(i);
book[i - 1]setIsbn(ce[0]getContents()toString());
book[i - 1]setSeries(ce[1]getContents()toString());
book[i - 1]setBookTitle(ce[2]getContents()toString());
book[i - 1]setWriter(ce[3]getContents()toString());
book[i - 1]setTranscribe(ce[4]getContents()toString());
book[i - 1]setPublisher(ce[5]getContents()toString());
book[i - 1]setPublishDate(sdfparse(ce[6]getContents()toString(), new ParsePosition(0)));
book[i-1]setVersion(IntegerparseInt(ce[7]getContents()toString()));
book[i-1]setPage(IntegerparseInt(ce[8]getContents()toString()));
book[i-1]setCls(ce[9]getContents()toString());
book[i-1]setPrecisnotes(ce[10]getContents()toString());
book[i-1]setInstall(ce[11]getContents()toString());
book[i-1]setPrice(FloatparseFloat(ce[12]getContents()toString()));
book[i-1]setUserid(usbgetUser()getUserid());
getVector()addElement(book[i - 1]);
}
rwbclose();
fisclose();
} catch (FileNotFoundException e)
{} catch (BiffException e)
{} catch (IOException e)
{} catch (NumberFormatException e)
{
ShowMessage("数据导入失败,请按照本软件要求的EXCEL格式导入定单");
}
}
import jxl; \x0d\import jxlwrite; \x0d\import javaio; \x0d\import javaioFile; \x0d\import javautil; \x0d\\x0d\public class excel \x0d\{ \x0d\public static void main(String[] args) \x0d\{ \x0d\\x0d\String targetfile = "c:/outxls";//输出的excel文件名 \x0d\String worksheet = "List";//输出的excel文件工作表名 \x0d\String[] title = {"ID","NAME","DESCRIB"};//excel工作表的标题 \x0d\\x0d\WritableWorkbook workbook; \x0d\try \x0d\{ \x0d\//创建可写入的Excel工作薄,运行生成的文件在tomcat/bin下 \x0d\//workbook = WorkbookcreateWorkbook(new File("outputxls")); \x0d\Systemoutprintln("begin"); \x0d\\x0d\OutputStream os=new FileOutputStream(targetfile); \x0d\workbook=WorkbookcreateWorkbook(os); \x0d\\x0d\WritableSheet sheet = workbookcreateSheet(worksheet, 0); //添加第一个工作表 \x0d\//WritableSheet sheet1 = workbookcreateSheet("MySheet1", 1); //可添加第二个工作 \x0d\/ \x0d\jxlwriteLabel label = new jxlwriteLabel(0, 2, "A label record"); //put a label in cell A3, Label(column,row) \x0d\sheetaddCell(label); \x0d\/ \x0d\\x0d\jxlwriteLabel label; \x0d\for (int i=0; i回答于 2022-11-16
下面的代码是读取文本文件的例子,程序会读取texttxt文件,并将它的内容显示出来。
1importjavaio;
2importjavaioFile;
3importjavaioFileReader;
4importjavaio;
5importjavaioIOException;
6
7publicclass
8{
9publicstaticvoidmain(String[]args)
10{
11Filefile=newFile("testtxt");
12contents=new();
13reader=null;
14
15try
16{
17reader=new(newFileReader(file));
18Stringtext=null;
19
20//repeatuntilalllinesisread
21while((text=readerreadLine())!=null)
22{
23contentsappend(text)
24append(SystemgetProperty(
25"lineseparator"));
26}
27}catch(e)
28{
29e();
30}catch(IOExceptione)
31{
32e();
33}finally
34{
35try
36{
37if(reader!=null)
38{
39readerclose();
40}
41}catch(IOExceptione)
42{
43e();
44}
45}
46
47//showfilecontentshere
48Systemoutprintln(contentstoString());
下面是我写的一个运用jxl生成Excel文件的方法
该方法接收一个保存MarkesData数据的ArrayList arlist和文件生成路径Path
通过取得arlist里的数据生成Excel文件
感觉应该是你想要的 我没调试过也许有错 你也可以根据你的需要进行修改^_^
import javaioFile;
import javaioIOException;
import javautilArrayList;
import javautilIterator;
import jxlWorkbook;
import jxlwriteLabel;
import jxlwriteWritableSheet;
import jxlwriteWritableWorkbook;
import jxlwriteWriteException;
import jxlwritebiffRowsExceededException;
public class WriteExcel {
WritableWorkbook book=null;
public void OutputExcel(ArrayList arlist,String Path){
try{
book = WorkbookcreateWorkbook(new File(Path));
//设置表名
WritableSheet sheet = bookcreateSheet("考试单",0);
//生成表格题头
Label labe1 = new Label(0, 0, "考生姓名" );
Label labe2 = new Label(1, 0, "地区");
Label labe3 = new Label(2, 0, "所属院校");
Label labe4 = new Label(3, 0, "班级");
Label labe5 = new Label(4, 0, "考试号");
Label labe6 = new Label(5, 0, "考试时间");
Label labe7 = new Label(6, 0, "科目名称");
//将生成的单元格添加到工作表中
sheetaddCell(labe1);
sheetaddCell(labe2);
sheetaddCell(labe3);
sheetaddCell(labe4);
sheetaddCell(labe5);
sheetaddCell(labe6);
sheetaddCell(labe7);
Iterator it = arlistiterator();
int i = 1;
while(ithasNext()){
//通过迭代获得arlist里的MarkesData对象
MarkesData temp = (MarkesData)itnext();
//取得数据生成单元格
Label label1=new Label(0,i,tempgetUser_name());
Label label2=new Label(1,i,tempgetArea_name());
Label label3=new Label(2,i,tempgetCollege_name());
Label label4=new Label(3,i,tempgetClass_name());
Label label5=new Label(4,i,tempgetTest_name());
Label label6=new Label(5,i,tempgetStarttime());
Label label7=new Label(6,i,tempgetSubject_name());
//将生成的单元格添加到工作表中
sheetaddCell(label1);
sheetaddCell(label2);
sheetaddCell(label3);
sheetaddCell(label4);
sheetaddCell(label5);
sheetaddCell(label6);
sheetaddCell(label7);
i++;
}
bookwrite();
bookclose();
} catch (RowsExceededException e) {
eprintStackTrace();
} catch (WriteException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
} finally{
try{
if(book!=null)bookclose();
}catch(Exception e){
Systemoutprintln("exception when closing Connection in finally");
Systemoutprintln(egetMessage()toString());
}
}
}
}
该是一些特殊符被转义了,你可以把mysqldump -u root -proot --opt mysql > C:/Users/success/aksql 这句写到一个bat文件里面,然后java中就直接调用这个bat文件就可以避免了!
刚刚漏了帐号密码了,现在补上
try {
//这里的是MYSQL 举例
//加载驱动
ClassforName("commysqljdbcDriver");
//创建数据库连接
Connection con = DriverManagergetConnection("jdbc:mysql://localhost:3306/test","root","root");
//创建查询 “请求”
PreparedStatement ps = conprepareStatement("select from user");
//返回查询结果
ResultSet rs = psexecuteQuery();
//遍历结果
while(rsnext()) {
//假如 User 表中 有个 name 列
Systemoutprintln("name >> "+rsgetString("name"));
}
//关闭
rsclose();
psclose();
conclose();
} catch (Exception e) {
eprintStackTrace();
}
oracle
数据库导入导出(用dmp的方法)
cmd
1
将数据库SampleDB完全导出,用户名system
密码manager
导出到E:\SampleDBdmp中
exp
file=E:\sampleDBdmp
full=y
2将备份数据库文件中的数据导入指定的数据库SampleDB
中,如果
SampleDB
已存在该表,则不再导入;
imp
file=D:\SampleDBdmp
full=y
ignore=y
如果没有DBA权限,用以下命令授予DBA权限
sqlplus
/
as
sysdba;
conn
system/manager
sql>grant
dba
to
SampleDB
以上就是关于用java怎样把数据存到数据库中全部的内容,包括:用java怎样把数据存到数据库中、java中把SQL数据库中的表导出到excel中.怎么实现、在Java编程中怎么将从数据库查询出来的数据导成Excel文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)