jdbc使用 Statement 接口实现更新数据操作
package chap03.sec03;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import model.Book;
import util.DbUtil;
public class Demo1 {
public static int updateBook(Book b) throws SQLException {
String sql = "update t_book set bookName='" + b.getBookName() + "',price=" + b.getPrice() + ",author='"
+ b.getAuthor() + "',bookTypeId=" + b.getBookTypeId() + " where id="+b.getId()+"";
DbUtil dbUtil = new DbUtil();
Connection con = dbUtil.getCon();
Statement s = con.createStatement();
int result = s.executeUpdate(sql);
dbUtil.closeCon(s, con);
return result;
}
public static void main(String[] args) {
Book book=new Book(3,"javaqwe234",1212,"李四2",2);
try {
int result =updateBook(book);
if(result==1){
System.out.println("更新成功");
}
else{
System.out.println("更新失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}