JDBC--使用statement接口实现增,删,改operate
来源:互联网 发布:kettle 删除表数据 编辑:程序博客网 时间:2024/06/11 01:24
1.statement接口引入-----用于执行静态SQL语句返回所生成结果的对象。
2.int executeUpdate(String sql)-----支持增删改
void close(); 关闭数据库连接
封装方法--
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 jdbc.chap03;import java.sql.Connection;import java.sql.Statement;import jdbc.util.DbUtil;public class Demo02 {private static DbUtil dbUtil=new DbUtil();/** * 添加个人信息 * @param personName * @param age * @return * @throws Exception */private static int addinformation(String personName,Integer age)throws Exception{Connection con=dbUtil.getCon();//获取连接String sql="insert into t_student values(656,'"+personName+"',"+age+")";Statement stmt=con.createStatement();//创建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con);//关闭Statement和连接return result;}public static void main(String[] args)throws Exception{int result=addinformation("Allen", 43);if(result==1){System.out.println("添加成功了!");}else {System.out.println("添加失败!");}}}
结果::
方法二:
package jdbc.chap03;import java.sql.Connection;import java.sql.Statement;import jdbc.util.DbUtil;public class Demo01 {public static void main(String[] args) throws Exception{DbUtil dbUtil=new DbUtil();String sql="insert into t_student values(233,'文科',21)";Connection con=dbUtil.getCon();//获取数据库连接Statement stmt=con.createStatement();//获取Statementint result=stmt.executeUpdate(sql);System.out.println("操作的结果:"+result+"数据");stmt.close();//关闭statementcon.close();//关闭连接}}
方法三:面向对象
package jdbc.chap03;import java.sql.Connection;import java.sql.Statement;import jdbc.util.DbUtil;import model.Person;public class Demo02 {private static DbUtil dbUtil=new DbUtil();/** * 添加个人信息2 * @param person * @return * @throws Exception */private static int add2(Person person)throws Exception{Connection con=dbUtil.getCon();//获取连接String sql="insert into t_student values(null,'"+person.getPersonName()+"',"+person.getAge()+")";Statement stmt=con.createStatement();//创建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con);//关闭Statement和连接return result;}/** * 添加个人信息 * @param personName * @param age * @return * @throws Exception *//*private static int addinformation(String personName,Integer age)throws Exception{Connection con=dbUtil.getCon();//获取连接String sql="insert into t_studeMnt values(654,'"+personName+"',"+age+")";Statement stmt=con.createStatement();//创建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con);//关闭Statement和连接return result;}*/public static void main(String[] args)throws Exception{/*int result=addinformation("Allen", 43);if(result==1){System.out.println("添加成功了!");}else {System.out.println("添加失败!");}*///多行注释 ctrl+shift+/Person person=new Person("周星驰",28);int result=add2(person);if(result==1){System.out.println("添加成功了!");}else {System.out.println("添加失败!");}}}
person类:
package model;/** * 个人信息 * @author MC-DS * */public class Person {private int id;private String personName;private int 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;}}
0 0
- JDBC--使用statement接口实现增,删,改operate
- jdbc03.使用Statement 接口实现增/删/改操作
- JDBC利用Statement接口实现增删改操作
- JDBC--使用statement接口实现更新数据
- JDBC--使用statement接口实现删除数据
- jdbc使用 Statement 接口实现添加数据操作小例子
- jdbc使用 Statement 接口实现更新数据操作
- jdbc使用 Statement 接口实现删除数据操作
- JDBC Statement接口实现的execute方法
- 初学JDBC(四)-使用Preparedstatement接口实现增删改操作
- jdbc使用 Statement 接口实现添加数据操作(使用面向对象和数据库连接工具类)
- eclipse+jdbc+mysql+statement实现数据库的增删改
- JDBC Statement实现数据库增删改查案例
- JDBC-实现操作:增、删、改(DML)
- 使用 Statement 接口实现添加数据操作
- JDBC接口介绍之Statement
- JDBC - Statement的增删改查
- 使用jdbc实现增删改查示例
- Android修改系统语言
- 9.最佳实践
- ora-4031原因和解决方法
- HandlerThread
- mysql 联合主键的作用
- JDBC--使用statement接口实现增,删,改operate
- 一起写RPC框架(十二)RPC服务提供端四--服务的限流
- excel怎么一次性生成10万个6位连续数 和 随机6位数
- 【转载】Tomcat 7.0.3x 启动慢并且遇到StackOverflowError的异常的解决办法
- xUtils源码阅读(2)-ImageManager
- android小功能代码片段
- @Transactional
- hdu5893 List wants to travel(树链剖分+线段树)
- android itemtouchhelper-extension 仿微信删除