日期:2014-05-20  浏览次数:20580 次

连接数据库问题
public void insert(User user) throws Exception {//增加操作
// TODO Auto-generated method stub
String sql = " insert into luser_tbl(name,password,age) values('?','?','?')";
PreparedStatement pstmt=null;
DataBaseConnection dbc=null;

dbc=new DataBaseConnection();
pstmt=dbc.preparestatement(sql);//这条语句报错要如何解决?提示找不到方法 pstmt.setString(1, user.getName());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getAge());
pstmt.execute();


------解决方案--------------------
String sql = " insert into luser_tbl(name,password,age) values('?','?','?')";
改成
String sql = " insert into luser_tbl(name,password,age) values(?,?,?)";
------解决方案--------------------
探讨
String sql = " insert into luser_tbl(name,password,age) values('?','?','?')";
改成
String sql = " insert into luser_tbl(name,password,age) values(?,?,?)";

------解决方案--------------------
虽然?号的写法有错,但是红色的报错不是因为这个,

而是应该把
pstmt=dbc.preparestatement(sql);
改成
pstmt=dbc.prepareStatement(sql);

S要大写。。。

探讨

String sql = " insert into luser_tbl(name,password,age) values('?','?','?')";
改成
String sql = " insert into luser_tbl(name,password,age) values(?,?,?)";