这个方法是把我的一个JSP提交的内容存入数据库的方法:
public void saveUserInfor(Userinformation userinformation)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = java.sql.DriverManager
.getConnection(
"jdbc:mysql://localhost/db1?useUnicode=true&characterEncoding=GBK",
"root", "root");
String sql = "insert into userinfor (username, password, age, interesting, description ) values ( "
+ "'"
+ userinformation.getUsername()
+ "'"
+ ","
+ "'"
+ userinformation.getPassword()
+ "'"
+ ","
+ "'"
+ userinformation.getAge()
+ "'"
+ ","
+ "'"
+ userinformation.getInteresting()
+ "'"
+ ","
+ "'"
+ userinformation.getDescription() + "'" + ")";
System.out.println("the insert sql of userinfor is:" + sql); Statement stmt = con.createStatement();
stmt.execute(sql);
stmt.close();
con.close(); }
这个是查询并输出数据库中的方法:
public List getUserInforList() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = java.sql.DriverManager
.getConnection(
"jdbc:mysql://localhost/db1?useUnicode=true&characterEncoding=GBK",
"root", "root");
Statement stmt = con.createStatement();
ResultSet rst = stmt
.executeQuery("select * from userinfor order by id asc");
List userinformations = new ArrayList();
while (rst.next()) {
Userinformation userinformation = new Userinformation();
System.out.println("-------------------------------------");
System.out.println("user id is:" + rst.getString("id"));
System.out.println("user password is:" + rst.getString("password"));
System.out.println("user interesting is:"
+ rst.getString("interesting"));
System.out.println("user age is:" + rst.getString("age"));
System.out.println("user description is:"
+ rst.getString("description"));
System.out.println("user username is:" + rst.getString("username"));
userinformation
.setAge(new Integer(rst.getString("age")).intValue());
userinformation.setDescription(rst.getString("description"));
userinformation.setInteresting(rst.getString("interesting"));
userinformation.setUsername(rst.getString("username"));
userinformations.add(userinformation); }
// 关闭连接、释放资源
rst.close();
stmt.close();
con.close();
return userinformations;
}
我想请前辈们帮我按上面的格式,写一下删其中的一条信息和改其中的一条信息的方法,我昨天已经整了一天了,上午也弄了一个上午,还是没有弄清楚,写的代码没有错误,可是就是起不了效果,还请前辈们指点!在此,我提前说声谢谢了!