日期:2014-05-17  浏览次数:21253 次

ps.setString(1, name); 放这段代码里什么意思
public int deleteBookById(String name) {
String sql = "delete from book where id= ? ";
int count = 0;
try {
conn = DBConnection.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, name);
count = ps.executeUpdate();
为什么要放这句代码啊ps.setString(1, name); 还有 int count = 0; 这个有什么用嘛

------解决方案--------------------
Java code

public int deleteBookById(String name) {
String sql = "delete from book where id= ? ";
// 初始化count 在下面的update中使用
int count = 0;
try {
conn = DBConnection.getConnection();
ps = conn.prepareStatement(sql);
// 将name 设置到 sql 的问号处
ps.setString(1, name);
// 如果更新成功 则返回 1给 count 否则是 0
count = ps.executeUpdate();

------解决方案--------------------
1代表你sql语句中的第一个问号,name是你要传得参数
比如你要删除 id为1的信息
ps.setString(1, “1”);
这样你的sql语句就成了 delete from book where id= ‘1’
这样你执行这条语句的时候 就把id为1的信息删除了