JDBC--使用statement接口实现更新数据
来源:互联网 发布:java语言培训哪家好 编辑:程序博客网 时间:2024/05/19 12:39
package jdbc.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class DbUtil {//数据库地址private static String dbUrl="jdbc:mysql://localhost:3306/mybatis_db";//用户名private static String dbUserName="root";//密码private static String dbPassword="#######################################################";//驱动名称private static String jdbcName="com.mysql.jdbc.Driver";/** * 获取数据库连接 * @return * @throws Exception */public Connection getCon()throws Exception{Class.forName(jdbcName);Connection con=DriverManager.getConnection(dbUrl, dbUserName,dbPassword);return con;}/** * 关闭连接 * @param con * @throws Exception */public void close(Statement stmt,Connection con)throws Exception{if(stmt!=null){stmt.close();if(con!=null){con.close();}}}}
package model;/** * 个人信息 * @author MC-DS * */public class Person {private int id;private String personName;private int age;public Person(int id, String personName, int age) {super();this.id = id;this.personName = personName;this.age = age;}public Person(String personName, int age) {super();this.personName = personName;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getPersonName() {return personName;}public void setPersonName(String personName) {this.personName = personName;}public Integer getAge() {return age;}public void setAge(int age) {this.age = age;}}
package jdbc.chap03;import java.sql.Connection;import java.sql.Statement;import jdbc.util.DbUtil;import model.Person;public class Demo03 {private static DbUtil dbUtil=new DbUtil();private static int update01(Person person)throws Exception{Connection con=dbUtil.getCon();//获取连接String sql="update t_student set name='"+person.getPersonName()+"',age="+person.getAge()+" where id="+person.getId()+"";Statement stmt=con.createStatement();//创建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con);//关闭Statement和连接return result;}public static void main(String[] args) throws Exception {Person person=new Person(7,"古天樂",37);int result=update01(person);if(result==1){System.out.println("更新成功了!");}else {System.out.println("更新失败!");}}}
更新成功了!
0 0
- JDBC--使用statement接口实现更新数据
- jdbc使用 Statement 接口实现更新数据操作
- JDBC--使用statement接口实现删除数据
- jdbc使用 Statement 接口实现添加数据操作小例子
- jdbc使用 Statement 接口实现删除数据操作
- jdbc使用 Statement 接口实现添加数据操作(使用面向对象和数据库连接工具类)
- jdbc使用 PreparedStatement 接口实现更新数据操作
- 使用 Statement 接口实现添加数据操作
- JDBC--使用statement接口实现增,删,改operate
- JDBC Statement接口实现的execute方法
- JDBC访问数据中Statement接口的常用方法
- JDBC利用Statement接口实现增删改操作
- JDBC--Statement(更新操作)
- JDBC接口介绍之Statement
- 通过Statement接口实现数据库的更新操作
- jdbc使用 PreparedStatement 接口实现添加数据操作
- jdbc使用 PreparedStatement 接口实现删除数据操作
- JDBC:通过 Statement 执行更新操作
- 七天学会ASP.NET MVC (五)——Layout页面使用和用户角色管理
- CodeForces 474EPillars(线断树区间最大)
- 300. Longest Increasing Subsequence
- RC522读卡器 M1卡学习总结(二)
- NOIP2015普及组 金币
- JDBC--使用statement接口实现更新数据
- 简单算法之插入排序及其优化shell排序
- Go语言基础:并发
- makefile
- http://www.cnblogs.com/spring87/p/4496930.html
- linux和windows的进程的虚拟地址空间
- 第四周项目4-建设双链表算法库
- Linux管道pipe -- C和Python两种实现方案解析
- 树链剖分模板