例如:其中conn是连接对象, unitsintstock和productid都是整型数据字段. ?代表参数PreparedStatement pst; pst = new conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");pst.setInt(1,1000); //设置第1个参数 pst.setInt(2,101); //设置第2个参数 pst.executeUpdate();
请问 xtaotao 兄弟, new conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");conn是一个对象而不是一个类,new conn.prepareStatement()这个语法我有点胡涂,请问是你写错了还是我确实没见到过这种语法。谢谢你们的回答!!!!
Connection conn; //连接 statement stmt; Resultset data;sql = "select * from a where c2 = " + f1 + " and substring(c3,0,10) = " + d1; data = stmt.executeQuery(sql);
对不起,我写错了,应该是: conn连接对象的prepareStatement方法返回一个PreparedStatement的对象.pst = conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");
对不起,又错! 应该是: conn连接对象的prepareStatement方法返回一个PreparedStatement的对象.pst = conn.prepareStatement("update Products set UnitsInStock=UnitsInStock=? where ProductID=?");
keen1982,你的意思是让f1,d1都自动转行位String吗?
以上解都正确但: 1,sql = "select * from a where c2 = " + f1 + " and substring(c3,0,10) = " + d1; 此种问题下,如果f1,d1是字符串,且包含单引号等,会出错的; 2,sql语句中定义变量,不推荐使用"?"。 若,sql语句中定义的变量太多,会影响程序可读性。
?代表参数PreparedStatement pst;
pst = new conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");pst.setInt(1,1000); //设置第1个参数
pst.setInt(2,101); //设置第2个参数
pst.executeUpdate();
new conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");conn是一个对象而不是一个类,new conn.prepareStatement()这个语法我有点胡涂,请问是你写错了还是我确实没见到过这种语法。谢谢你们的回答!!!!
statement stmt;
Resultset data;sql = "select * from a where c2 = " + f1 + " and substring(c3,0,10) = " + d1;
data = stmt.executeQuery(sql);
conn连接对象的prepareStatement方法返回一个PreparedStatement的对象.pst = conn.prepareStatement("update Products set UnitsInStock=UnitsInStock+? where ProductID=?");
conn连接对象的prepareStatement方法返回一个PreparedStatement的对象.pst = conn.prepareStatement("update Products set UnitsInStock=UnitsInStock=? where ProductID=?");
1,sql = "select * from a where c2 = " + f1 + " and substring(c3,0,10) = " + d1;
此种问题下,如果f1,d1是字符串,且包含单引号等,会出错的;
2,sql语句中定义变量,不推荐使用"?"。
若,sql语句中定义的变量太多,会影响程序可读性。