jdbc使用 Statement 接口实现更新数据操作
2016-06-24 21:17
423 查看
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();
}
}
}
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();
}
}
}
相关文章推荐
- 第一篇博客,线性表插入数据。
- 做敏捷估算,请忘掉人/天
- 毕向东视频学习笔记【Java_Day10 多态+内部类】
- Linux操作系统
- android中的service与Activity之间的通信
- 用于 Unity 和虚幻引擎的 Asm.js 与 WebGL
- uiview 手势的一些问题
- Java compiler level does not match的解决方法
- ln和mount的区别
- 机器学习系列:(九)从感知器到支持向量机
- SpringIOC容器
- ajax 跨域调取新浪股票的接口
- 24个小诀窍让你的工作更有效率
- Java-排序算法2
- Creating Form Elements --Using BeginForm and EndForm 使用内建的Form辅助器方法 精通ASP.NET MVC 5
- 代码触发表格编辑事件
- phpcms制作静态手机移动站
- 提高项目8-k次方之和
- 从“如何设计用户超过1亿的应用”说起—数据库调优实战
- Mac系统mds、mds_stores、mdworker疯狂工作的问题