81,094
社区成员
发帖
与我相关
我的任务
分享
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = DbConnection.getConn();
st = con.createStatement();
//查询
rs = st.executeQuery(sql);
//一些操作
st.executeUpdate(sql)
//一些操作
rs = st.executeQuery(sql)
//一些操作
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭连接
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//获取Connection部分代码
public static Connection getConn(){
if(conn==null){
try {
conn=DriverManager.getConnection(url,username , password);
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}