fanjialong před 1 dnem
rodič
revize
f706a1cb3c

+ 29 - 0
jdbc-demo/.gitignore

@@ -0,0 +1,29 @@
+### IntelliJ IDEA ###
+out/
+!**/src/main/**/out/
+!**/src/test/**/out/
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+bin/
+!**/src/main/**/bin/
+!**/src/test/**/bin/
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 8 - 0
jdbc-demo/.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 8 - 0
jdbc-demo/.idea/compiler.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CompilerConfiguration">
+    <annotationProcessing>
+      <profile default="true" name="Default" enabled="true" />
+    </annotationProcessing>
+  </component>
+</project>

+ 10 - 0
jdbc-demo/.idea/libraries/lib.xml

@@ -0,0 +1,10 @@
+<component name="libraryTable">
+  <library name="lib">
+    <CLASSES>
+      <root url="file://$PROJECT_DIR$/lib" />
+    </CLASSES>
+    <JAVADOC />
+    <SOURCES />
+    <jarDirectory url="file://$PROJECT_DIR$/lib" recursive="false" />
+  </library>
+</component>

+ 6 - 0
jdbc-demo/.idea/misc.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 8 - 0
jdbc-demo/.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/jdbc-demo.iml" filepath="$PROJECT_DIR$/jdbc-demo.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
jdbc-demo/.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+  </component>
+</project>

+ 12 - 0
jdbc-demo/jdbc-demo.iml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="library" name="lib" level="project" />
+  </component>
+</module>

binární
jdbc-demo/lib/hamcrest-core-1.3.jar


binární
jdbc-demo/lib/junit-4.13.jar


binární
jdbc-demo/lib/lombok-1.18.30.jar


binární
jdbc-demo/lib/mysql-connector-java-5.1.48.jar


+ 7 - 0
jdbc-demo/src/com/sf/_01_simulate_jdbc/MyJDBCAble.java

@@ -0,0 +1,7 @@
+package com.sf._01_simulate_jdbc;
+
+public interface MyJDBCAble {
+    void connection();
+
+    void close();
+}

+ 11 - 0
jdbc-demo/src/com/sf/_01_simulate_jdbc/MySQLConction.java

@@ -0,0 +1,11 @@
+package com.sf._01_simulate_jdbc;
+
+public class MySQLConction implements MyJDBCAble{
+    public void connection(){
+        System.out.println("mysql连接");
+    }
+
+    public void close(){
+        System.out.println("mysql关闭");
+    }
+}

+ 11 - 0
jdbc-demo/src/com/sf/_01_simulate_jdbc/OracleConction.java

@@ -0,0 +1,11 @@
+package com.sf._01_simulate_jdbc;
+
+public class OracleConction implements MyJDBCAble{
+    public void connection(){
+        System.out.println("oracle连接");
+    }
+
+    public void close(){
+        System.out.println("oracle关闭");
+    }
+}

+ 11 - 0
jdbc-demo/src/com/sf/_01_simulate_jdbc/SQLServerConction.java

@@ -0,0 +1,11 @@
+package com.sf._01_simulate_jdbc;
+
+public class SQLServerConction implements MyJDBCAble {
+    public void connection(){
+        System.out.println("sqlServer连接");
+    }
+
+    public void close(){
+        System.out.println("sqlServer关闭");
+    }
+}

+ 32 - 0
jdbc-demo/src/com/sf/_01_simulate_jdbc/Test.java

@@ -0,0 +1,32 @@
+package com.sf._01_simulate_jdbc;
+
+import com.sun.org.apache.xpath.internal.operations.Or;
+
+public class Test {
+
+    @org.junit.Test
+    public void testMysql(){
+//        MySQLConction mySQLConction = new MySQLConction();
+//        mySQLConction.mysqlConction();
+//        mySQLConction.mysqlClose();
+
+//        OracleConction oracleConction = new OracleConction();
+//        oracleConction.oracleConction();
+//        oracleConction.oracleClose();
+//
+//        SQLServerConction sqlServerConction = new SQLServerConction();
+//        sqlServerConction.sqlServerConction();
+//        sqlServerConction.sqlServerClose();
+    }
+
+    @org.junit.Test
+    public void test1(){
+        MySQLConction mySQLConction = new MySQLConction();
+        mySQLConction.connection();
+        mySQLConction.close();
+
+        OracleConction oracleConction = new OracleConction();
+        oracleConction.connection();
+        oracleConction.close();
+    }
+}

+ 190 - 0
jdbc-demo/src/com/sf/_02_jdbc_hello_word/JDBCTest.java

@@ -0,0 +1,190 @@
+package com.sf._02_jdbc_hello_word;
+
+import org.junit.Test;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class JDBCTest {
+
+    @Test
+    public void test() throws ClassNotFoundException, SQLException {
+        System.out.println("注册");
+        // 1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver");
+        // 2 获取连接对象
+        /**
+         * URL: 连接数据库地址
+         * jdbc:mysql://数据库ip:数据库端口号/数据库名字"
+         * username: 数据账号
+         * password : 数据库密码
+         */
+        // connction 是数据库连接对象, 必须能够成功连接才可以执行sql 语句
+        Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo",
+                "root",
+                "admin");
+        // 3 创建语句对象
+        // statement : 语句对象主要用来后续执行sql 语句
+        Statement statement = connection.createStatement();
+        String sql = "insert into t_student(name,age,email) values('fanjialong',10,'123@qq.com')";
+        // 4 执行sql语句
+        // executeUpdate() : 可以执行DML 语句   增删改
+        statement.executeUpdate(sql);
+        // 5 释放资源
+        connection.close();
+        statement.close();
+        System.out.println("注册其他逻辑");
+    }
+
+    @Test
+    public void test4() throws ClassNotFoundException, SQLException {
+        System.out.println("学生批量处理");
+        // 1 加载驱动
+        Class.forName("com.mysql.jdbc.Driver");
+        // 2 获取连接对象
+        /**
+         * URL: 连接数据库地址
+         * jdbc:mysql://数据库ip:数据库端口号/数据库名字"
+         * username: 数据账号
+         * password : 数据库密码
+         */
+        // connction 是数据库连接对象, 必须能够成功连接才可以执行sql 语句
+        Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo",
+                "root",
+                "admin");
+        // 3 创建语句对象
+        // statement : 语句对象主要用来后续执行sql 语句
+        Statement statement = connection.createStatement();
+        String sql = "insert into t_student(name,age,email) values('fanjialong',10,'123@qq.com')";
+        // 4 执行sql语句
+        // executeUpdate() : 可以执行DML 语句   增删改
+        statement.executeUpdate(sql);
+        // 5 释放资源
+        connection.close();
+        statement.close();
+        System.out.println("注册批量其他逻辑");
+    }
+
+
+    @Test
+    public void test5(){
+        String a = "-";
+        // li '-' si
+        String  str = "li "  +a+  "si";
+        System.out.println(str);
+    }
+
+    /**
+     * 删除数据id 为1 的数据
+     */
+    @Test
+    public void test2() throws Exception{
+        // 1 加载驱动类
+        /**
+         * class.forName(com.mysql.jdbc.Driver)
+         * 当程序用到Driver 这个类的时候就会加载静态代码块
+         *  static {
+         *         try {
+         *             DriverManager.registerDriver(new Driver());
+         *         } catch (SQLException var1) {
+         *             throw new RuntimeException("Can't register driver!");
+         *         }
+         *     }
+         *  注册驱动   registerDriver
+         */
+        Class.forName("com.mysql.jdbc.Driver");
+        // 2 获取连接对象
+        /**
+         * 数据这里不是随便都可以访问
+         * 必须有正确账号和正确密码已经正确数据库地址
+         * 才可以获取连接对象才能够操作数据库
+         */
+        Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo",
+                "root",
+                "admin");
+        // 3 创建语句对象
+        Statement statement = connection.createStatement();
+        String sql = "delete from t_student where id = 1";
+        // 4 执行sql 语句 DML 语句  insert  update  delete
+        statement.executeUpdate(sql);
+        // 5 释放资源
+        connection.close();
+        statement.close();
+    }
+
+    /**
+     * 更id 为2 数据把name 修改成lisi
+     */
+    @Test
+    public void test3() throws Exception{
+        // 1 加载驱动类
+        /**
+         * class.forName(com.mysql.jdbc.Driver)
+         * 当程序用到Driver 这个类的时候就会加载静态代码块
+         *  static {
+         *         try {
+         *             DriverManager.registerDriver(new Driver());
+         *         } catch (SQLException var1) {
+         *             throw new RuntimeException("Can't register driver!");
+         *         }
+         *     }
+         *  注册驱动   registerDriver
+         */
+        Class.forName("com.mysql.jdbc.Driver");
+        // 2 获取连接对象
+        /**
+         * 数据这里不是随便都可以访问
+         * 必须有正确账号和正确密码已经正确数据库地址
+         * 才可以获取连接对象才能够操作数据库
+         */
+        Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo",
+                "root",
+                "admin");
+        // 3 创建语句对象
+        Statement statement = connection.createStatement();
+        String sql = "update t_student set name='lisi' where id = 2";
+        // 4 执行sql 语句 DML 语句  insert  update  delete
+        statement.executeUpdate(sql);
+        // 5 释放资源
+        connection.close();
+        statement.close();
+    }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+}

+ 35 - 0
jdbc-demo/src/com/sf/_03_jdbc_dao/dao/StudentDAO.java

@@ -0,0 +1,35 @@
+package com.sf._03_jdbc_dao.dao;
+
+import com.sf._03_jdbc_dao.domain.Student;
+
+import java.util.List;
+
+public interface StudentDAO {
+    /**
+     * 新增学生方法
+     */
+    void save(Student student);
+
+
+    /**
+     * 更新学生方法
+     * @param student
+     */
+    void update(Student student);
+
+    /**
+     * 根据id 删除
+     */
+    void delete(Long id);
+
+    /**
+     * 根据id 查询单个学生信息
+     */
+    Student get(Long id);
+
+
+    /**
+     * 查询数据库当中所有的学生信息
+     */
+    List<Student> list();
+}

+ 235 - 0
jdbc-demo/src/com/sf/_03_jdbc_dao/dao/impl/StudentDAOImpl.java

@@ -0,0 +1,235 @@
+package com.sf._03_jdbc_dao.dao.impl;
+
+import com.sf._03_jdbc_dao.dao.StudentDAO;
+import com.sf._03_jdbc_dao.domain.Student;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * JDBC 对于DAO 层实现类
+ */
+public class StudentDAOImpl implements StudentDAO {
+    @Override
+    public void save(Student student) {
+        //1 加载驱动
+        Connection connection = null;
+        PreparedStatement statement = null;
+        try {
+            Class.forName("com.mysql.jdbc.Driver");
+            //2 获取连接对象
+            connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
+            //3 创建语句对象
+//            statement = connection.createStatement();
+            statement = connection.prepareStatement("insert into t_student (name,age,email) values(?,?,?)");
+            statement.setString(1,student.getName());
+            statement.setInt(2,student.getAge());
+            statement.setString(3,student.getEmail());
+            //4 执行sql 语句
+            // statement   拼接字符串  可能会产生sql 注入问题
+            // li -si
+            statement.executeUpdate();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }finally {
+            // 判断连接对象不为空再关闭资源
+            if(connection!=null){
+                try {
+                    connection.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            if(statement!=null){
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+    }
+
+
+    @Override
+    public void update(Student student) {
+        //1 加载驱动
+        Connection connection = null;
+        PreparedStatement statement = null;
+        try {
+            Class.forName("com.mysql.jdbc.Driver");
+            //2 获取连接对象
+            connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
+            //3 创建语句对象
+//            statement = connection.createStatement();
+            statement = connection.prepareStatement("update t_student set name=?,age=?,email=? where id=?");
+            statement.setString(1,student.getName());
+            statement.setInt(2,student.getAge());
+            statement.setString(3,student.getEmail());
+            statement.setLong(4,student.getId());
+            //4 执行sql 语句
+            // statement   拼接字符串  可能会产生sql 注入问题
+            // li -si
+            statement.executeUpdate();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }finally {
+            // 判断连接对象不为空再关闭资源
+            if(connection!=null){
+                try {
+                    connection.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            if(statement!=null){
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+    }
+
+
+    @Override
+    public void delete(Long id) {
+        //1 加载驱动
+        Connection connection = null;
+        PreparedStatement statement = null;
+        try {
+            Class.forName("com.mysql.jdbc.Driver");
+            //2 获取连接对象
+            connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
+            //3 创建语句对象
+//            statement = connection.createStatement();
+            statement = connection.prepareStatement("delete from t_student where id=?");
+            statement.setLong(1,id);
+            //4 执行sql 语句
+            // statement   拼接字符串  可能会产生sql 注入问题
+            // li -si
+            statement.executeUpdate();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }finally {
+            // 判断连接对象不为空再关闭资源
+            if(connection!=null){
+                try {
+                    connection.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            if(statement!=null){
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+    }
+
+    @Override
+    public Student get(Long id) {
+        Connection connection = null;
+        PreparedStatement statement = null;
+        try {
+            Class.forName("com.mysql.jdbc.Driver");
+            //2 获取连接对象
+            connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
+            //3 创建语句对象
+//            statement = connection.createStatement();
+            statement = connection.prepareStatement("select * from t_student where id=?");
+            statement.setLong(1,id);
+            //4 执行sql 语句
+            // statement   拼接字符串  可能会产生sql 注入问题
+            // li -si
+            ResultSet resultSet = statement.executeQuery();
+            // resultSet 结果集, 从数据库当中查询数据都在resultSet 中
+            // 利用.next 判断是否查询到了数据.如果next为true 表示有数据 false 表示没有数据
+            if(resultSet.next()){
+                // 有数据,创建出来一个Student 对象 , 把数据库当中数据设置到student 对象当中
+                Student student = new Student();
+                student.setId(resultSet.getLong("id"));
+                student.setName(resultSet.getString("name"));
+                student.setEmail(resultSet.getString("email"));
+                student.setAge(resultSet.getInt("id"));
+                return student;
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }finally {
+            // 判断连接对象不为空再关闭资源
+            if(connection!=null){
+                try {
+                    connection.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            if(statement!=null){
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public List<Student> list() {
+        Connection connection = null;
+        PreparedStatement statement = null;
+        try {
+            Class.forName("com.mysql.jdbc.Driver");
+            //2 获取连接对象
+            connection = DriverManager.getConnection("jdbc:mysql:///jdbc_demo", "root", "admin");
+            //3 创建语句对象
+//            statement = connection.createStatement();
+            statement = connection.prepareStatement("select * from t_student ");
+            //4 执行sql 语句
+            // statement   拼接字符串  可能会产生sql 注入问题
+            // li -si
+            ResultSet resultSet = statement.executeQuery();
+            List<Student> list = new ArrayList<>();
+            // resultSet 结果集, 从数据库当中查询数据都在resultSet 中
+            // 利用.next 判断是否查询到了数据.如果next为true 表示有数据 false 表示没有数据
+            while (resultSet.next()){
+                // 有数据,创建出来一个Student 对象 , 把数据库当中数据设置到student 对象当中
+                Student student = new Student();
+                student.setId(resultSet.getLong("id"));
+                student.setName(resultSet.getString("name"));
+                student.setEmail(resultSet.getString("email"));
+                student.setAge(resultSet.getInt("id"));
+                // 把学生信息添加到集合当中
+                list.add(student);
+            }
+            return list;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }finally {
+            // 判断连接对象不为空再关闭资源
+            if(connection!=null){
+                try {
+                    connection.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+            if(statement!=null){
+                try {
+                    statement.close();
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }
+    }
+
+}

+ 16 - 0
jdbc-demo/src/com/sf/_03_jdbc_dao/domain/Student.java

@@ -0,0 +1,16 @@
+package com.sf._03_jdbc_dao.domain;
+
+import lombok.*;
+
+@Getter
+@Setter
+@ToString
+@AllArgsConstructor
+@NoArgsConstructor
+public class Student {
+    private Long id;
+    private String name;
+    private Integer age;
+    private String email;
+
+}

+ 60 - 0
jdbc-demo/src/com/sf/_03_jdbc_dao/test/StudentJDBCTest.java

@@ -0,0 +1,60 @@
+package com.sf._03_jdbc_dao.test;
+
+import com.sf._03_jdbc_dao.dao.StudentDAO;
+import com.sf._03_jdbc_dao.dao.impl.StudentDAOImpl;
+import com.sf._03_jdbc_dao.domain.Student;
+import org.junit.Test;
+
+public class StudentJDBCTest {
+
+
+    @Test
+    public void test(){
+        System.out.println("注册");
+        // 插入一个学生
+        Student s= new Student(null,"zhangsan",10,"123@qq.com");
+        StudentDAO studentDAO = new StudentDAOImpl();
+        studentDAO.save(s);
+        System.out.println("注册后逻辑");
+    }
+
+
+    @Test
+    public void test1(){
+        System.out.println("批量处理");
+        // 插入一个学生
+        Student s= new Student(null,"zhangsan123",20,"123@qq.com");
+        StudentDAO studentDAO = new StudentDAOImpl();
+        studentDAO.save(s);
+        System.out.println("批量处理后逻辑");
+    }
+
+
+    @Test
+    public void test2(){
+        Student student = new Student(5L,"fanjialong123",20,"234@qq.com");
+        StudentDAO studentDAO = new StudentDAOImpl();
+        studentDAO.update(student);
+    }
+
+    @Test
+    public void test3(){
+        StudentDAO studentDAO = new StudentDAOImpl();
+        studentDAO.delete(5L);
+    }
+
+    @Test
+    public void test4(){
+        StudentDAO studentDAO = new StudentDAOImpl();
+        System.out.println(studentDAO.get(6L));
+    }
+
+
+    @Test
+    public void test5(){
+        StudentDAO studentDAO = new StudentDAOImpl();
+        for (Student student : studentDAO.list()) {
+            System.out.println(student);
+        }
+    }
+}