1提取单条记录
//importjavasql;
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
try{
ClassforName("sunjdbcodbc");
Stringurl="jdbc:odbc:%%1";
con=(url,%%2,%%3);
stmt=conn();
stmt(%%4);
rs=stmt(%%5);
}catch(Exceptione){
e();
}
finally{
try{
if(rs!=null)
rsclose();
if(stmt!=null)
stmtclose();
if(conn!=null)
connclose();
}catch(e){
e();
}
}
3显示表格
/
importjavaawt;
importjavaxswing;
importjavasql;
importjavaxswingtable;
String[]colHeads=%%4;
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
try{
ClassforName("sunjdbcodbc");
Stringurl="jdbc:odbc:%%1";
conn=(url,%%2,%%3);
stmt=conn();
rs=stmt("SELECTcount()asau_countfrom"%%5);
rsnext();
intiCount=rsgetInt("au_count");
Object[][]data=newObject[iCount][];
inti=0;
rs=stmt("SELECTfrom"%%5);
while(rsnext()){
data[i]=newObject[iCount];
data[i][0]=rsgetString("au_fname");
data[i][1]=rsgetString("Phone");
data[i][2]=rsgetString("City");
i;
}
JTabletable=newJTable(data,colHeads);
JScrollPanejsp=newJScrollPane(table);
()add(jsp);
}catch(Exceptione){
eprintStackTrace();
}
finally{
try{
if(rs!=null)
rsclose();
if(stmt!=null)
stmtclose();
if(conn!=null)
connclose();
}catch(SQLExceptione){
eprintStackTrace();
}
}
6关闭时关闭连接
//importjavasql;
addWindowListener(newWindowAdapter{
publicvoidwindowClosing(WindowEventwevent){
if(stmt!=null){
try{
if(rs!=null)
rsclose();
if(stmt!=null)
stmtclose();
if(conn!=null)
connclose();
}catch(SQLExceptione){
eprintStackTrace();
}
}
7执行命令
//importjavasql;
Connectionconn=null;
PreparedStatementpst=null;
try{
conn=DriverManagergetConnection(url);
pst=connprepareStatement("InsertIntograde(%%1)Values()");
pstsetInt(1,%%2);
//pstsetString(2,%%2);
pstaddBatch();
pstexecuteBatch();
}catch(SQLExceptione){
eprintStackTrace();
}
finally{
try{
if(pst!=null)
pstclose();
if(conn!=null)
connclose();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
eprintStackTrace();
}
}
首先你要明白一个问题javaswing给你提供了一个可视化的界面,这个界面可以使你在后端接受到界面互动产生的数据行为。
然后,写入数据库这个 *** 作,你只要知道你写一个jdbc的连接,写sql语句。
你把这两步分开做第一步是打印点按钮打印你的对话框里的内容。第二部,你写个main方法写入数据库最后把这两步合并起来。
什么按钮都实现不了,你需要自己做:
1、为按钮添加侦听器
2、在侦听器中解析excel(可以考虑jxl\POI等等)
3、将解析的数据用sql插入到mysql数据库
1写一个 *** 作数据库的通用DAO(假设为Daojava),提供·加载数据库驱动和获取数据库连接的方法:voidgetConnection();·执行查询的方法:ResultSetexecuteQuery(Stringsql);·执行更新的方法:IntegerexecuteUpdate(Stringsql);·关闭资源的方法:voidreleaseResource(Connectioncon);2在你的界面中把用户名和密码取出来·username=yourTextFieldgetText();·password=yourPasswordFieldgetText();3匹配·验证username、password是否是有效输入值(例如:不能包含特殊字符,不能有注入嫌疑等)·Stringsql="selectfromyourTablewhereusername='"+username+"'andpassword='"+password+"'";·DaoexecuteQuery(sql):如果有结果说明合法,否则不合法。不懂Connect我
sqlment=contcreateStatement();
String str="use easy;select RoomNo from Room where TypeCode='"+roomty+"'"+
" and RoomStatus1 in ('VC') order by RoomNo";
ResultSet rs=sqlmentexecuteQuery(str);
while(rsnext()){
textsetText(rsgetString(1)trim());
}
}
设置模型(model)然后使用模型的方法添加行! 例如:dtm
=
new
DefaultTableModel(new
String[]{"姓名","性别","年龄"},0);
JTable
jTable
=
new
JTable(dtm); //使用一下方法添加dtmaddRow(new
String[]{"sa","dasd","dasd"});
以上就是关于在Java中如何对数据库中的数据进行 *** 作(java怎么调用数据库里的数据)全部的内容,包括:在Java中如何对数据库中的数据进行 *** 作(java怎么调用数据库里的数据)、javaswing怎么向数据库中添加数据、swing按钮怎么实现excel转入mysql中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)