使用Statement执行sql语句
来源:互联网 发布:算法工程师培训班 编辑:程序博客网 时间:2024/06/05 18:21
Demo2.java
package com.cn.statement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import org.junit.Test;import com.cn.Util.JdbcUtil;/*** Author:Liu Zhiyong(QQ:1012421396)* Version:Version_1* Date:2017年3月17日11:01:28* Desc:使用Statement执行sql语句1.使用jdbc创建一张员工表员工表字段:编号,姓名,性别,年龄,职位,邮箱,电话2.使用jdbc对员工表执行以下操作:1)插入一条数据2)修改一条数据3)删除一条数据4)查询并打印所有员工数据*/public class Demo2 {private String url = "jdbc:mysql://localhost:3306/mydb";private String user = "root";private String password = "root";/** * 使用Statement执行DDL语句 */@Testpublic void test1(){Connection conn = null;Statement stmt = null;try {//1.注册MySQL驱动Class.forName("com.mysql.jdbc.Driver");//2.获取连接对象conn = DriverManager.getConnection(url, user, password);//3.创建StateMentstmt = conn.createStatement();//4.准备sqlString sql = "create table employee(eid int primary key auto_increment, ename varchar(20), gender varchar(2), dept varchar(20), email varchar(30), phone varchar(20) )";//5.执行sql,并返回结果int count = stmt.executeUpdate(sql);//6.输出结果System.out.println(count);} catch (Exception e) {throw new RuntimeException(e);}finally{//7.关闭资源(顺序:后打开,先关闭)if(stmt != null){try {stmt.close();} catch (SQLException e) {System.out.println("Statement关闭失败!");throw new RuntimeException(e);}}if(conn != null){try {conn.close();} catch (SQLException e) {System.out.println("Connection关闭失败!");throw new RuntimeException(e);}}}}/** * * 使用Statement执行DML语句 * * 插入insert */@Testpublic void test2() {Connection conn = null;Statement stmt = null;try {/** * 抽取代码 * 通过工具获取连接对象 */conn = JdbcUtil.getConnection();//3.创建Statement对象stmt = conn.createStatement();//4.准备sqlString sql = "INSERT INTO employee(ename, gender, dept,email,phone) VALUES('刘先森', '男', '开发部', '1012421396@qq.com', '18071897425')";//5.执行sqlint count = stmt.executeUpdate(sql);System.out.println(count);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();throw new RuntimeException(e);}finally{JdbcUtil.close(conn, stmt);}} /** * 使用Statement执行DML语句 * * 修改update */@Testpublic void test3() {Connection conn = null;Statement stmt = null;//模拟用户输入String name = "张馨予";int id = 3;try {//通过工具获取连接对象conn = JdbcUtil.getConnection();//3.创建Statement对象stmt = conn.createStatement();//4.准备sqlString sql = "UPDATE employee SET ename='" + name + "' WHERE eid = " + id;//5.执行sqlint count = stmt.executeUpdate(sql);System.out.println(count);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();throw new RuntimeException(e);}finally{JdbcUtil.close(conn, stmt);}}/** * 使用Statement执行DML语句 * * 删除delete */@Testpublic void test4() {Connection conn = null;Statement stmt = null;//模拟用户输入int id = 6;try {//通过工具获取连接对象conn = JdbcUtil.getConnection();//3.创建Statement对象stmt = conn.createStatement();//4.准备sqlString sql = "delete from employee WHERE eid = " + id;//5.执行sqlint count = stmt.executeUpdate(sql);System.out.println(count);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();throw new RuntimeException(e);}finally{JdbcUtil.close(conn, stmt);}}/** * 使用Statement执行DQL语句(查询操作) */@Testpublic void test5() {Connection conn = null;Statement stmt = null;try {//获取连接对象conn = JdbcUtil.getConnection();//创建Statement对象stmt = conn.createStatement();//准备sql语句String sql = "select * from employee";//执行sqlResultSet rs = stmt.executeQuery(sql);System.out.println("根据列的索引取值");while(rs.next()){System.out.println(rs.getInt(1) + "#" + rs.getString(2) + "#" + rs.getString(3) + "#" + rs.getString(4) + "#" + rs.getString(5) + "#" + rs.getString(6));}System.out.println("根据列的名称取值");rs = stmt.executeQuery(sql);while(rs.next()){System.out.println(rs.getInt("eid") + "#" + rs.getString("ename") + "#" + rs.getString("gender") + "#" + rs.getString("dept") + "#" + rs.getString("email") + "#" + rs.getString("phone"));}} catch (Exception e) {throw new RuntimeException(e);}finally{JdbcUtil.close(conn, stmt);}}}
抽取jdbc获取Connection对象和关闭Connection对象和Statement对象的工具类
JdbcUtil.java
package com.cn.Util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;/** * jdbc的工具类 * @author liuzhiyong * */public class JdbcUtil {private static String url = "jdbc:mysql://localhost:3306/mydb";private static String user = "root";private static String password = "root";/** * 静态代码块(只调用一次) */static{//注册驱动程序try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("驱动程序注册出错!");}}/** * 获取连接对象的方法 */public static Connection getConnection(){try {Connection conn = DriverManager.getConnection(url, user, password);return conn;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();throw new RuntimeException(e);}}/** * 释放资源的方法 */public static void close(Connection conn, Statement stmt){//关闭资源(顺序:后打开,先关闭)if(stmt != null){try {stmt.close();} catch (SQLException e) {System.out.println("Statement关闭失败!");throw new RuntimeException(e);}}if(conn != null){try {conn.close();} catch (SQLException e) {System.out.println("Connection关闭失败!");throw new RuntimeException(e);}}}}
0 0
- 使用Statement执行sql语句
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- SQL语句的执行:Statement、PrepareStatement、CallableStatement
- Java中使用Statement接口SQL语句
- Statement三种执行SQL语句的方法
- JDBC学习笔记(3)—Statement执行SQL语句
- Statement、PreparedStatement执行SQL
- JDBC中Statement 接口提供了三种执行 SQL 语句的方法: executeQuery executeUpdate execute 使用哪一个方法由 SQL 语句所产生的内容决定。
- statement 对象执行sql语句时,sql语句怎么写的
- 使用批处理执行sql 语句
- 使用SqlCommand执行SQL语句
- 使用hibernate执行sql语句
- 使用PreparedStatement执行sql语句
- statement切割SQL语句中的转义符使用
- PreparedStatement 与 Statement 的区别 —— 理解动态SQL语句的执行机制
- Statement批量执行sql语句,批量操作数据库,提高数据库效率
- JDBC学习笔记(四):执行SQL语句(Statement与PreparedStatement)
- 3、Statement陈述接口的子接口提前准备sql语句PreparedStatement中的方法来执行SQL语句
- IO之ByteInputStream ByteOutputStream
- 输出缓存区的内存分配
- 初始正则
- Leetcode 150 Evaluate Reverse Polish Notation (求值逆波兰表达式)
- MYSQL(6)数据操作2
- 使用Statement执行sql语句
- leetcode 128. Longest Consecutive Sequence .
- 基于POI的Excel表格数据导出
- Private Members in JavaScript
- Caffe中的Makefile.config的一些说明
- javaweb学习总结(七)——HttpServletResponse对象(一)
- 3.17
- AB测试结果分析
- lenovo Win10 安装 Androd Studio 新版本编程开发的软件