发布网友 发布时间:2022-04-22 11:32
共1个回答
热心网友 时间:2023-11-03 09:24
1.提取单条记录
//importjava.sql.*;
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
try{
Class.forName("sun.jdbc.odbc.");
Stringurl="jdbc:odbc:%%1";
con=.(url,%%2,%%3);
stmt=conn.();
stmt.(%%4);
rs=stmt.(%%5);
}catch(Exceptione){
e.();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(e){
e.();
}
}
3.显示表格
/*
importjava.awt.*;
importjavax.swing.*;
importjava.sql.*;
importjavax.swing.table.*;
String[]colHeads=%%4;
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
try{
Class.forName("sun.jdbc.odbc.");
Stringurl="jdbc:odbc:%%1";
conn=.(url,%%2,%%3);
stmt=conn.();
rs=stmt.("SELECTcount(*)asau_countfrom"%%5);
rs.next();
intiCount=rs.getInt("au_count");
Object[][]data=newObject[iCount][];
inti=0;
rs=stmt.("SELECT*from"%%5);
while(rs.next()){
data[i]=newObject[iCount];
data[i][0]=rs.getString("au_fname");
data[i][1]=rs.getString("Phone");
data[i][2]=rs.getString("City");
i;
}
JTabletable=newJTable(data,colHeads);
JScrollPanejsp=newJScrollPane(table);
().add(jsp);
}catch(Exceptione){
e.printStackTrace();
}
finally{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
6.关闭时关闭连接
//importjava.sql.*;
addWindowListener(newWindowAdapter{
publicvoidwindowClosing(WindowEventwevent){
if(stmt!=null){
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
7.执行命令
//importjava.sql.*;
Connectionconn=null;
PreparedStatementpst=null;
try{
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("InsertIntograde(%%1)Values(?)");
pst.setInt(1,%%2);
//pst.setString(2,%%2);
pst.addBatch();
pst.executeBatch();
}catch(SQLExceptione){
e.printStackTrace();
}
finally{
try{
if(pst!=null)
pst.close();
if(conn!=null)
conn.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}