可以帮我看看int i = sm.executeUpdate(sql); 为什么出错了么?
package com.pp.admin;
import
java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import javax.security.sasl.SaslException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.pp.control.Action;
import com.pp.control.common.DataBase;
public class Db_stu {
//创建公用数据库连接类对象
DataBase database=new DataBase();
Statement sm = null;
//添加注册用户
public int addstu(Stu stu) {
database.getConnection();
try {
String sql = "insert into stu(id,stuname,stucode, " +
"stuphone,classcode,username,psw,type) values( ' "+ stu.getId()
+ " ', ' " + stu.getStucode() + " ', ' " + stu.getStuname()
+ " ', ' " + stu.getDeparment()+ " ', ' " + stu.getStuphone()
+ " ', ' " + stu.getUsername()+ " ', ' " + stu.getPsw()
+ " ', ' " + stu.getType()+ " ') ";
sql = new String(sql.getBytes( "ISO8859-1 "), "GB2312 ");
int i = sm.executeUpdate(sql); // 执行sql语句
return i;
}
catch (
SQLException SqlE) {
SqlE.printStackTrace();
return -1;
}
catch (Exception E) {
E.printStackTrace();
return -2;
}
finally {
// 关闭连接,释放数据库资源:
database.close();
}
}
}
------解决方案--------------------你在使用sm.executeUpdate之前
应该先sm = database.getConnection().createStatement()
------解决方案--------------------首先你的程序应该 简化
在sql中应该用占位符~~
------解决方案--------------------尽信书不如无书