刚刚漏了帐号密码了,现在补上
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();
}
java中的JTABLE是采用的MVC设计模式,有几个知识点要了解清楚
1 数据模型:你首先要把数据库中的数据存入数据模型
建立数据模型要实现一个AbstractTableModel接口。接口里有一个类似动态数组的vector变量,将数据库查询的结果存在这个动态的数组(vector)中
例 :public class yourModel extends AbstractTableModel {}
2在JTABLE中调用模型: 在JTABLE组件有个一SETMODEL方法,模型建好后调用这个方法setmodel(model);
先说这么多,网上也有些例子一时没去找,有不明白的再留言
前段时间正好做了,导入和导出的,你自己看吧
/导出数据为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格式导入定单");
}
}
public void jButton1_actionPerformed(ActionEvent actionEvent) {
try {
Connection con=conDB();
Statement st = concreateStatement();
ResultSet rs=stexecuteQuery("select NAME,PRICE from test");
StringBuffer sb=null;
定义一个 StringBuffer sb;
sbappend(rsgetString("NAME")+" "+rsgetString("PRICE"))
while(rsnext() )
{
sbappend(rsgetString("NAME")+" "+rsgetString("PRICE"));
jTextAreaappend(sbtoString());
}
rsclose();
stclose();
conclose();
}
catch(Exception e){
JOptionPaneshowMessageDialog(null,"出现异常!");
}
}
以上就是关于java怎么从数据库中查询数据并输出全部的内容,包括:java怎么从数据库中查询数据并输出、在JAVA中怎样把数据库中的数据在JTABLE中输出、JAVA 怎么实现将查询数据库的结果导出excel表格等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)