|
@@ -0,0 +1,260 @@
|
|
|
+package com.sf.day26._01_jdbc;
|
|
|
+
|
|
|
+import com.sf.day26._01_jdbc.domain.Student;
|
|
|
+import com.sf.day26._01_jdbc.service.MyJDBCApi;
|
|
|
+import com.sf.day26._01_jdbc.service.impl.OracleApi;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+public class JDBTest {
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+ MyJDBCApi myJDBCApi = new OracleApi();
|
|
|
+ myJDBCApi.execute();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test1() throws ClassNotFoundException, SQLException {
|
|
|
+ //1 加载驱动类
|
|
|
+ // 这个就是我们驱动对象, 我们只需要加载一下他的字节码
|
|
|
+ // 我们是不需要自己创建Driver 类, 我们只需要加载字节码, 他这类中他又一个静态代码块是可以帮助我们去创建Driver
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ //2 获取连接对象
|
|
|
+ /**
|
|
|
+ * 三个参数:
|
|
|
+ * 第一个参数: 连接数据库url 信息
|
|
|
+ * 第二个参数: 连接数据库账号信息
|
|
|
+ * 第三个参数: 连接数据库密码信息
|
|
|
+ */
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo",
|
|
|
+ "root",
|
|
|
+ "admin");
|
|
|
+ //3 创建执行语句对象
|
|
|
+ // 创建字符串写sql 语句
|
|
|
+ String sql = "delete from t_student where id =2";
|
|
|
+ //statement 就是语句对象 , 他可以帮助我们去执行sql
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ //4 执行sql语句
|
|
|
+ // 在statement 当中有俩个方法 一个是execeuteUpage 执行增删改 DML 一个是executeQuery 执行DQL
|
|
|
+ // executeUpdate 返回值是受影响行
|
|
|
+ int count = statement.executeUpdate(sql);
|
|
|
+ System.out.println("受影响行:"+ count);
|
|
|
+ //5 释放资源
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test2() throws ClassNotFoundException, SQLException {
|
|
|
+ // 加载驱动类
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ // 创建执行语句对象
|
|
|
+ String sql = "insert into t_student(name,email,age) values('zhangsan','123@qq.com',10)";
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ // 执行sql语句
|
|
|
+ statement.executeUpdate(sql);
|
|
|
+ // 关闭资源
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test3() throws ClassNotFoundException, SQLException {
|
|
|
+ // 加载驱动类
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ // 创建执行语句对象
|
|
|
+ String sql = "update t_student set name='zhangsan123' where id = 21";
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ // 执行sql语句
|
|
|
+ statement.executeUpdate(sql);
|
|
|
+ // 关闭资源
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) throws SQLException, ClassNotFoundException {
|
|
|
+// method1();
|
|
|
+// method2();
|
|
|
+ method3();
|
|
|
+ }
|
|
|
+// 需求3 : 在控台当中输入名字, 删除指定名字的数据
|
|
|
+ private static void method3() throws ClassNotFoundException, SQLException {
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ System.out.println("请输入要删除的id:");
|
|
|
+ long id = scanner.nextLong();
|
|
|
+ // 加载驱动类
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ // 创建执行语句对象
|
|
|
+ String sql = "delete from t_student where id = "+ id;
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ // 执行sql语句
|
|
|
+ statement.executeUpdate(sql);
|
|
|
+ // 关闭资源
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 需求2 : 在控制台当中输入用户名 , 年龄 , 邮箱 , id , 要求修改指定id 数据
|
|
|
+ private static void method2() throws ClassNotFoundException, SQLException {
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ System.out.println("请输入name:");
|
|
|
+ String name = scanner.next();
|
|
|
+ System.out.println("请输入邮箱:");
|
|
|
+ String email = scanner.next();
|
|
|
+ System.out.println("请输入年龄");
|
|
|
+ int age = scanner.nextInt();
|
|
|
+ System.out.println("请输入id:");
|
|
|
+ long id = scanner.nextLong();
|
|
|
+
|
|
|
+ //加载驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ //获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ //创建SQL 语句
|
|
|
+ String sql ="update t_student set name='"+name+"',email='"+email+"',age="+age+" where id="+id+"";
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ //执行sql 语句
|
|
|
+ statement.executeUpdate(sql);
|
|
|
+ //关闭资源
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需求1 : 要求在控制台当中输入用户姓名和年龄邮箱, 往mysql 当中要求插入 5 条数据
|
|
|
+ * 需求2 : 在控制台当中输入用户名 , 年龄 , 邮箱 , id , 要求修改指定id 数据
|
|
|
+ * 需求3 : 在控台当中输入名字, 删除指定名字的数据
|
|
|
+ * 温馨提示: 在控制当中输出Scanner , 不能够用@Test 方法 要用main 主方法
|
|
|
+ */
|
|
|
+ private static void method1() throws ClassNotFoundException, SQLException {
|
|
|
+ Scanner scanner = new Scanner(System.in);
|
|
|
+ //加载驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ // 创建要执行的sql语句
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
+ System.out.println("请输入name:");
|
|
|
+ String name = scanner.next();
|
|
|
+ System.out.println("请输入邮箱:");
|
|
|
+ String email = scanner.next();
|
|
|
+ System.out.println("请输入年龄");
|
|
|
+ int age = scanner.nextInt();
|
|
|
+
|
|
|
+ // 创建要执行sql
|
|
|
+ String sql = "insert into t_student (name,email,age) values('"+name+"','"+email+"',"+age+")";
|
|
|
+ // 执行sql
|
|
|
+ statement.executeUpdate(sql);
|
|
|
+ }
|
|
|
+ // 关闭资源
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 预编译语句对象
|
|
|
+ // 往t_student 表当中插入t_student
|
|
|
+ @Test
|
|
|
+ public void test4() throws ClassNotFoundException, SQLException {
|
|
|
+ // 1加载运行
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 2获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ // 3创建预编译语句对象
|
|
|
+ String sql = "insert into t_student (name,email,age) values(?,?,?)";
|
|
|
+ // 创建预编译语句第对象, 然后编译解析 sql 注意现在还没有去执行sql
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ // 设置参数
|
|
|
+ /**
|
|
|
+ * 第一个参数: 要给第一个? 设置值 , 从1 开始
|
|
|
+ * 第二个参数: 要设置的值
|
|
|
+ */
|
|
|
+ preparedStatement.setString(1,"张三");
|
|
|
+ preparedStatement.setString(2,"123@qq.com");
|
|
|
+ preparedStatement.setInt(3,30);
|
|
|
+ // 4 执行sql
|
|
|
+ // 在去执行executeUpdate 的时候就会把? 替换成对应的值 , 然后执行sql
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ // 5 释放资源
|
|
|
+ connection.close();
|
|
|
+ preparedStatement.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询t_student id为5 的数据
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test5() throws ClassNotFoundException, SQLException {
|
|
|
+ // 1 加载驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 2 获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ // 3 创建预编译语句
|
|
|
+ String sql = "select * from t_student where id =?";
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setLong(1,5);
|
|
|
+ // 4 执行sql
|
|
|
+ ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
+ // 这个resultSet 封装了我们返回的数据
|
|
|
+ // 判断如果有数据就获取数据进行封装
|
|
|
+ Student student = new Student();
|
|
|
+ if(resultSet.next()){
|
|
|
+ String name = resultSet.getString("name");
|
|
|
+ String email = resultSet.getString("email");
|
|
|
+ long id = resultSet.getLong("id");
|
|
|
+ int age = resultSet.getInt("age");
|
|
|
+ student = new Student(id,name,email,age);
|
|
|
+ }
|
|
|
+ // 5 释放资源
|
|
|
+ System.out.println(student);
|
|
|
+ connection.close();
|
|
|
+ preparedStatement.close();
|
|
|
+ resultSet.close();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询出来数据库当中所有数据 返回List<Student> list
|
|
|
+ @Test
|
|
|
+ public void test6() throws ClassNotFoundException, SQLException {
|
|
|
+ // 1 加载驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 2 获取连接对象
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "admin");
|
|
|
+ // 3 创建预编译sql 语句
|
|
|
+ String sql = "select * from t_student";
|
|
|
+ // 创建预编译语句
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ ResultSet resultSet = preparedStatement.executeQuery();
|
|
|
+ List<Student> students = new ArrayList<>();
|
|
|
+ while (resultSet.next()){
|
|
|
+ String name = resultSet.getString("name");
|
|
|
+ String email = resultSet.getString("email");
|
|
|
+ long id = resultSet.getLong("id");
|
|
|
+ int age = resultSet.getInt("age");
|
|
|
+ Student student = new Student(id,name,email,age);
|
|
|
+
|
|
|
+ students.add(student);
|
|
|
+ }
|
|
|
+ System.out.println(students);
|
|
|
+ // 关闭资源
|
|
|
+ connection.close();
|
|
|
+ resultSet.close();
|
|
|
+ preparedStatement.close();
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|