|
@@ -0,0 +1,158 @@
|
|
|
+package com.sf.javase.day24;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static java.time.LocalTime.now;
|
|
|
+
|
|
|
+/**
|
|
|
+ * jdbc 操作
|
|
|
+ */
|
|
|
+public class T {
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t1() throws ClassNotFoundException, SQLException {
|
|
|
+ // 1 加载数据库的驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 2 创建数据库的连接
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8", "root", "root");
|
|
|
+ // 3、通过数据库的连接创建statement对象
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ Date date = new Date(System.currentTimeMillis());
|
|
|
+ // 将sql语句提交到数据库
|
|
|
+ String sql = "INSERT INTO user (user_name,price,create_time) values ('张三',100.12,now())";
|
|
|
+ int i = statement.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
|
|
|
+ ResultSet resultSet = statement.getGeneratedKeys();
|
|
|
+ System.out.println("影响行数 = "+i);
|
|
|
+ if(resultSet.next()){
|
|
|
+ int anInt = resultSet.getInt(1);
|
|
|
+ System.out.println(anInt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t2() throws ClassNotFoundException, SQLException {
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbc?characterEncoding=utf-8","root","root");
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ String sql = "UPDATE user SET user_name = 'wwww',price = 111.11,create_time = now() WHERE user_id = 1014";
|
|
|
+ int i = statement.executeUpdate(sql);
|
|
|
+ System.out.println(i);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询
|
|
|
+ * 三个查询和查询集合
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t3() throws ClassNotFoundException, SQLException {
|
|
|
+ List<User> list = new ArrayList<>();
|
|
|
+ //获取数据库的驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 创建数据库的连接
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8", "root", "root");
|
|
|
+ // 创建statement对象
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ String sql = "SELECT user_id,user_name,price,create_time FROM user";
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);
|
|
|
+ while (resultSet.next()){
|
|
|
+ User user = new User();
|
|
|
+ user.setUserId(resultSet.getInt("user_id"));
|
|
|
+ user.setUserName(resultSet.getString("user_name"));
|
|
|
+ user.setPrice(resultSet.getDouble("price"));
|
|
|
+ user.setCreateTime(resultSet.getDate("create_time"));
|
|
|
+ list.add(user);
|
|
|
+ }
|
|
|
+ System.out.println("users = "+list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询一个
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t4() throws ClassNotFoundException, SQLException {
|
|
|
+ //获取数据库的驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 创建数据库的连接
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8", "root", "root");
|
|
|
+ // 创建statement对象
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ String sql = "SELECT user_id,user_name,price,create_time FROM user WHERE user_id = 1000";
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);
|
|
|
+ if(resultSet.next()){
|
|
|
+ User user = new User();
|
|
|
+ user.setUserId(resultSet.getInt("user_id"));
|
|
|
+ user.setUserName(resultSet.getString("user_name"));
|
|
|
+ user.setPrice(resultSet.getDouble("price"));
|
|
|
+ user.setCreateTime(resultSet.getDate("create_time"));
|
|
|
+ System.out.println(user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t5() throws ClassNotFoundException, SQLException {
|
|
|
+ List<User> users = new ArrayList<>();
|
|
|
+ //获取数据库的驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 创建数据库的连接
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8", "root", "root");
|
|
|
+ // 创建statement对象
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ String sql = "SELECT user_id,user_name,price,create_time FROM user limit 1,2";
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);
|
|
|
+ while (resultSet.next()){
|
|
|
+ User user = new User();
|
|
|
+ user.setUserId(resultSet.getInt("user_id"));
|
|
|
+ user.setUserName(resultSet.getString("user_name"));
|
|
|
+ user.setPrice(resultSet.getDouble("price"));
|
|
|
+ user.setCreateTime(resultSet.getDate("create_time"));
|
|
|
+ users.add(user);
|
|
|
+ }
|
|
|
+ System.out.println(users);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 预编译操作
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t6() throws ClassNotFoundException,SQLException{
|
|
|
+ //加载数据库的驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ //创建数据库的连接
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8","root","root");
|
|
|
+ //获取
|
|
|
+ String sql = "insert into user (user_name,price,create_time) values (?,?,?)";
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setString(1,"lisa");
|
|
|
+ preparedStatement.setDouble(2,122.34);
|
|
|
+ preparedStatement.setDate(3,new Date(System.currentTimeMillis()));
|
|
|
+ int row = preparedStatement.executeUpdate();
|
|
|
+ System.out.println(row);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除 多个
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t7() throws ClassNotFoundException,SQLException{
|
|
|
+ //加载数据库的驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ //创建数据库的连接
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?characterEncoding=utf-8","root","root");
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement("delete from user where user_id in (?,?,?)");
|
|
|
+ //对占位符进行赋值操作
|
|
|
+ preparedStatement.setInt(1,1014);
|
|
|
+ preparedStatement.setInt(2,1015);
|
|
|
+ preparedStatement.setInt(3,1016);
|
|
|
+ int i = preparedStatement.executeUpdate();
|
|
|
+ System.out.println(i);
|
|
|
+ }
|
|
|
+}
|