wuheng vor 2 Jahren
Ursprung
Commit
c3a66650f1

+ 0 - 0
Mysql-day02.pdf → day02/Mysql-day02.pdf


+ 0 - 0
mysql高级.pdf → day02/mysql高级.pdf


+ 52 - 0
day03/src/Add.java

@@ -0,0 +1,52 @@
+import java.sql.*;
+
+public class Add {
+    /**
+     * * 添加:添加品牌
+     * * 查询:查询所有数据
+     * * 修改:根据id修改
+     * * 删除:根据id删除
+     */
+
+    public static void main(String[] args)  {
+        try {
+            add();
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void add() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+        String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+
+        String brandName = "香飘飘奶茶";
+        String companyName = "奶茶";
+        int ordered = 30;
+        String description = "一年绕地球一周";
+        int status = 2;
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+
+        String insertSql = "INSERT INTO tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?)";
+        PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
+        preparedStatement.setString(1, brandName);
+        preparedStatement.setString(2, companyName);
+        preparedStatement.setInt(3, ordered);
+        preparedStatement.setString(4, description);
+        preparedStatement.setInt(5, status);
+        int i = preparedStatement.executeUpdate();
+        if ( i > 0 ) {
+            System.out.println("插入成功");
+        } else {
+            System.out.println("插入失败");
+        }
+        preparedStatement.close();
+        connection.close();
+    }
+
+}

+ 47 - 0
day03/src/Delete.java

@@ -0,0 +1,47 @@
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+public class Delete {
+    /**
+     * * 添加:添加品牌
+     * * 查询:查询所有数据
+     * * 修改:根据id修改
+     * * 删除:根据id删除
+     */
+
+    public static void main(String[] args)  {
+        try {
+            add();
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void add() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+        String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+
+        int Id = 2;
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+
+        String insertSql = "DELETE from tb_brand WHERE id = ?";
+        PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
+        preparedStatement.setInt(1, Id);
+        int i = preparedStatement.executeUpdate();
+        if ( i > 0 ) {
+            System.out.println("删除成功");
+        } else {
+            System.out.println("删除失败");
+        }
+        preparedStatement.close();
+        connection.close();
+    }
+
+}

+ 112 - 0
day03/src/Jdbc.java

@@ -0,0 +1,112 @@
+import java.sql.*;
+
+public class Jdbc {
+
+    public static void main(String[] args) {
+        try {
+            prep1();
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void prep1() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+        String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+
+        String username = "1 OR 1 = 1";
+        String password = "123456";
+
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+        String sql = "SELECT * FROM tb_users WHERE username = ? LIMIT 1"; //MYSQL 服务器会把这个SQL 编译成2进制
+        PreparedStatement preparedStatement = connection.prepareStatement(sql);
+        preparedStatement.setString( 1, username );
+        ResultSet resultSet = preparedStatement.executeQuery();
+
+        while ( resultSet.next() ) {
+            String password1 = resultSet.getString("password");
+            if ( password1.equals(password) ) {
+                System.out.println( "用户登陆成功" );
+                return ;
+            }
+        }
+
+        System.out.println( "用户登陆失败" );
+
+    }
+
+
+    public static void resultSet() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+        String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+        String id = "3";
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+        Statement statement = connection.createStatement();
+        String sql = "SELECT * FROM tb_brand WHERE id = " + id;
+        ResultSet resultSet = statement.executeQuery(sql);
+
+        while ( resultSet.next() ) {
+            System.out.println( resultSet.getInt("id") );
+            System.out.println( resultSet.getString("brand_name") );
+            System.out.println( resultSet.getString("company_name") );
+            System.out.println( resultSet.getString("description") );
+        }
+
+
+        //System.out.println( resultSet );
+
+    }
+
+    public static void prep() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+        String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+
+        String username = "1 OR 1 = 1";
+        String password = "123456";
+
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+        Statement statement = connection.createStatement();
+        String sql = "SELECT * FROM tb_users WHERE username = " + username + " LIMIT 1";
+        ResultSet resultSet = statement.executeQuery(sql);
+
+        while ( resultSet.next() ) {
+            String password1 = resultSet.getString("password");
+            if ( password1.equals(password) ) {
+                System.out.println( "用户登陆成功" );
+                return ;
+            }
+        }
+
+        System.out.println( "用户登陆失败" );
+
+    }
+
+    public static void Jdbc() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+
+        String url = "jdbc:mysql://localhost:3306/test?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+        connection.setAutoCommit(false);
+        Statement statement = connection.createStatement();
+        String sql = "INSERT INTO `t` (`name`, `age`) VALUES ('李四', 22)";
+        int i = statement.executeUpdate(sql);
+        connection.commit();
+        System.out.println( i );
+    }
+
+}
+

+ 55 - 0
day03/src/Select.java

@@ -0,0 +1,55 @@
+import pojo.Brand;
+
+import java.sql.*;
+
+public class Select {
+    /**
+     * * 添加:添加品牌
+     * * 查询:查询所有数据
+     * * 修改:根据id修改
+     * * 删除:根据id删除
+     */
+
+    public static void main(String[] args)  {
+        try {
+            add();
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void add() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+        String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+
+        String brandName = "三只";
+
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+
+        String selectSql = "SELECT * FROM tb_brand WHERE brand_name LIKE ?";
+        PreparedStatement preparedStatement = connection.prepareStatement(selectSql);
+        preparedStatement.setString(1, "%" + brandName + "%");
+
+        ResultSet resultSet = preparedStatement.executeQuery();
+
+        while ( resultSet.next() ) {
+            Brand brand = new Brand();
+            brand.setId(resultSet.getInt("id"));
+            brand.setBrandName(resultSet.getString("brand_name"));
+            brand.setCompanyName(resultSet.getString("company_name"));
+            brand.setOrdered(resultSet.getInt("ordered"));
+            brand.setDescription(resultSet.getString("description"));
+            brand.setStatus(resultSet.getInt("status"));
+            System.out.println( brand );
+        }
+
+        preparedStatement.close();
+        connection.close();
+    }
+
+}

+ 57 - 0
day03/src/Update.java

@@ -0,0 +1,57 @@
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+public class Update {
+    /**
+     * * 添加:添加品牌
+     * * 查询:查询所有数据
+     * * 修改:根据id修改
+     * * 删除:根据id删除
+     */
+
+    public static void main(String[] args)  {
+        try {
+            add();
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void add() throws ClassNotFoundException, SQLException {
+
+        Class.forName("com.mysql.jdbc.Driver");
+        String url = "jdbc:mysql://localhost:3306/vip21?autoReconnect=true&failOverReadOnly=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8";
+        String user = "root";
+        String passwd = "123456";
+
+        String brandName = "香飘飘奶茶";
+        String companyName = "奶茶";
+        int ordered = 30;
+        String description = "一年绕地球一周";
+        int status = 2;
+        int Id = 2;
+        Connection connection = DriverManager.getConnection(url, user, passwd);
+
+        String insertSql = "UPDATE tb_brand SET brand_name=?,company_name=?,ordered=?,description=?,status=? WHERE id = ?";
+        PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
+        preparedStatement.setString(1, brandName);
+        preparedStatement.setString(2, companyName);
+        preparedStatement.setInt(3, ordered);
+        preparedStatement.setString(4, description);
+        preparedStatement.setInt(5, status);
+        preparedStatement.setInt(6, Id);
+        int i = preparedStatement.executeUpdate();
+        if ( i > 0 ) {
+            System.out.println("修改成功");
+        } else {
+            System.out.println("修改失败");
+        }
+        preparedStatement.close();
+        connection.close();
+    }
+
+}

+ 10 - 0
day03/src/druid.properties

@@ -0,0 +1,10 @@
+driverClassName=com.mysql.jdbc.Driver
+url=jdbc:mysql://localhost:3306/vip21?useSSL=false&useServerPrepStmts=true
+username=root
+password=123456
+#初始化链接库池大小
+initialSize=5
+#最大连接数
+maxActive=10
+#最大等待时间
+maxWait=3000

+ 70 - 0
day03/src/pojo/Brand.java

@@ -0,0 +1,70 @@
+package pojo;
+
+public class Brand {
+    private int id;
+    private String brandName;
+    private String companyName;
+    private int ordered;
+    private String description;
+    private int status;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getBrandName() {
+        return brandName;
+    }
+
+    public void setBrandName(String brandName) {
+        this.brandName = brandName;
+    }
+
+    public String getCompanyName() {
+        return companyName;
+    }
+
+    public void setCompanyName(String companyName) {
+        this.companyName = companyName;
+    }
+
+    public int getOrdered() {
+        return ordered;
+    }
+
+    public void setOrdered(int ordered) {
+        this.ordered = ordered;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+
+    public void setStatus(int status) {
+        this.status = status;
+    }
+
+    @Override
+    public String toString() {
+        return "Brand{" +
+                "id=" + id +
+                ", brandName='" + brandName + '\'' +
+                ", companyName='" + companyName + '\'' +
+                ", ordered=" + ordered +
+                ", description='" + description + '\'' +
+                ", status=" + status +
+                '}';
+    }
+}

+ 59 - 0
day03/src/pojo/DruidTest.java

@@ -0,0 +1,59 @@
+package pojo;
+
+import com.alibaba.druid.pool.DruidDataSourceFactory;
+
+import javax.sql.DataSource;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.Properties;
+
+public class DruidTest {
+    public static void main(String[] args) {
+        try {
+            test();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void test() throws Exception {
+
+        Properties properties = new Properties();
+
+        properties.load( new FileInputStream("day03/src/druid.properties"));
+
+        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
+
+        //System.out.println( dataSource );
+
+        String brandName = "香飘飘奶茶";
+        String companyName = "奶茶";
+        int ordered = 30;
+        String description = "一年绕地球一周";
+        int status = 2;
+
+        String insertSql = "INSERT INTO tb_brand (brand_name, company_name, ordered, description, status) values (?,?,?,?,?)";
+
+        Connection connection = dataSource.getConnection();
+
+        PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
+        preparedStatement.setString(1, brandName);
+        preparedStatement.setString(2, companyName);
+        preparedStatement.setInt(3, ordered);
+        preparedStatement.setString(4, description);
+        preparedStatement.setInt(5, status);
+        int i = preparedStatement.executeUpdate();
+        if ( i > 0 ) {
+            System.out.println("插入成功");
+        } else {
+            System.out.println("插入失败");
+        }
+        preparedStatement.close();
+
+    }
+}

BIN
day03/web/WEB-INF/lib/druid-1.1.12.jar


BIN
day03/web/WEB-INF/lib/mysql-connector-java-5.1.48.jar


BIN
day03/web/WEB-INF/lib/mysql-connector-java-8.0.28 .jar


+ 6 - 0
day03/web/WEB-INF/web.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
+         version="4.0">
+</web-app>

+ 16 - 0
day03/web/index.jsp

@@ -0,0 +1,16 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: 武恒
+  Date: 2023/1/13
+  Time: 9:16
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+  <head>
+    <title>$Title$</title>
+  </head>
+  <body>
+  $END$
+  </body>
+</html>