期末考试中出现的数据库连接问题
代码解释一下问题,其中代码如下。
数据库链接,一定要连接对库名,表名
public static int update(Bean1 bean)
{
String sql = "update classtable set teacher='" + bean.getTeacher() + "', address='" + bean.getAddress() + "', time='" + bean.getTime()
+ "' where classname='" + bean.getClassname() + "'";
Connection conn = Util1.getConn();
Statement state = null;
int count=0;
try {
state = conn.createStatement();
count = state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
Util1.close(state, conn);
}
return count;
}//和上述一样
//查找
public static Bean1 Search(String classnamet)
{
String sql = "select * from classtable where classname ='" + classnamet + "'";
Connection conn = Util1.getConn();
Statement state = null;
ResultSet rs = null;
Bean1 tbean=null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String classname = rs.getString("classname");
String teacher = rs.getString("teacher");
String address = rs.getString("address");
String time = rs.getString("time");
tbean = new Bean1(classname,teacher,address,time);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Util1.close(rs, state, conn);
}
return tbean;
}