//添加
public static int add(Bean1 bean)
{
String sql = "insert into classtable(classname,teacher,address,time) values('" +bean.getClassname() + "','" + bean.getTeacher() + "','" + bean.getAddress() + "','" +bean.getTime()+"')";
//改表名,表内数据(就是values后面,bean是Bean类的一个对象,有一些属性和方法,bean.getClassname()就是取bean中的classname元素), 表的名称可以从数据库图形化软件中看,(因为有一些表要加类似单引号括起来,不太清除什么时候加和,搁数据库图形化界面看最靠谱,我的表名classtable)
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 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;
}