插入数据库的问题

[复制链接]
查看11 | 回复1 | 2010-5-9 22:50:35 | 显示全部楼层 |阅读模式
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:student","","");

Statement stmt=con.createStatement();
String sql="insert usersInfo(userID,userName,userMima,userClass) values(\'"+studentId+"\',\'"+studentName+"\',\'"+studentPassword+"\',\'"+studentClass+"\')";

stmt.executeUpdate(sql);
out.println("插入成功");

}catch(SQLException e){

e.printStackTrace();

}catch(ClassNotFoundException e){
e.printStackTrace();
}
上面是我在servlet里面插入数据的一段代码,编译能通过,在tomcat里面运行的时候就是 INSERT语句语法错误,各位帮帮忙啊,是哪点的语法错误呢,我翻书了也看不出是哪点的
PreparedStatement psmt = con.createPrepareStatement();是预编译,要事先在数据库里面创建预编译么?你的方法和书上的是一样的,因为不能插入,所以我才用这种方法的

回复

使用道具 举报

千问 | 2010-5-9 22:50:35 | 显示全部楼层
插入的字段多了容易出错,可以使用PreparedStatement接口PreparedStatement psmt = con.createPrepareStatement();String sql="insert usersInfo(userID,userName,userMima,userClass) values(?,?,?,?)";psmt.setInt(1,studentId);psmt.setString(2,studentName);psmt.setString(3,studentPassword);psmt.setString(4,studentClass);具体是set什么类型以你的数据类型为准
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行