Java进阶十—JDBC

Java进阶十—JDBC

一.说明 

用Java语言操作Mysql,首先需要学习Mysql 

MySQL入门教程-CSDN博客

二.JDBC的由来以及定义 

 JDBC是什么?

  • Java数据库连接(Java Database Connectivity)简称JDBC
  • JDBC是Java操作各数据库的一种规范,是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。JDBC是面向关系型数据库的

打个比方?

  • 假设Java公司是布料厂,那么各SQL数据库公司就是服装设计厂
  • Java公司规定JDBC接口,允许去操作各数据库,相当于提供原材料
  • 各SQL公司去实现接口,相当于拿原材料设计出自己的服装

 Java进阶十—JDBC_第1张图片

三.JDBC体验,statement.executeQuery() 查询 

1.首先下载架包:Maven Repository: mysql » mysql-connector-java (mvnrepository.com) Java进阶十—JDBC_第2张图片

下载完成后,将架包拖入idea中的lib文件夹下(没有的话,自己创建),然后点击Java进阶十—JDBC_第3张图片Java进阶十—JDBC_第4张图片

点击OK

 2.连接

package com.jdbc;

import java.sql.*;

public class JDBCDemo {

    public static final String URL = "jdbc:mysql://localhost:3306/student";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";

   // 需要抛出异常:ClassNotFoundException, SQLException
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        // 1.加载驱动程序 ——> 打电话给布料公司,你给我送来,我是mysql公司
        Class.forName("com.mysql.jdbc.Driver");

        // 2.获取数据库连接 ——> 告诉送货员,送货路线
        Connection connection = DriverManager.getConnection(URL,USER,PASSWORD);

        // 3.获取数据库操作对象 ——> 货到了,卸货到仓库
        Statement statement = connection.createStatement();

        // 4.从仓库选择要用的货
        // 我要查询student表
        ResultSet resultSet  = statement.executeQuery("SELECT * FROM student");
        // 判断是否有数据
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            int age = resultSet.getInt(3);
            double score = resultSet.getDouble(4);
            int classId = resultSet.getInt(5);

            System.out.println("["+id+","+name+","+age+","+score+","+classId+"]");
        }

        // 5.关闭仓库与数据库
        statement.close();
        connection.close();
        
    }
}

Java进阶十—JDBC_第5张图片Java进阶十—JDBC_第6张图片

四.整理和释放 

 整理上面的代码

package com.jdbc;

import java.sql.*;

public class JDBCDemo {

    // 配置
    public static final String URL = "jdbc:mysql://localhost:3306/student";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";
    public static final String DRIVER = "com.mysql.jdbc.Driver";

    public static Connection connection;
    public static Statement statement;
    public static ResultSet resultSet;

   // 需要抛出异常:ClassNotFoundException, SQLException
    public static void main(String[] args)  {

        try {
            // 1.加载驱动程序 ——> 打电话给布料公司,你给我送来,我是mysql公司
            Class.forName(DRIVER);

            // 2.获取数据库连接 ——> 告诉送货员,送货路线
            connection = DriverManager.getConnection(URL,USER,PASSWORD);

            // 3.获取数据库操作对象 ——> 货到了,卸货到仓库
            statement = connection.createStatement();

            // 4.从仓库选择要用的货
            // 我要查询student表
             resultSet  = statement.executeQuery("SELECT * FROM student");
             
            // 判断是否有数据
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                int age = resultSet.getInt(3);
                double score = resultSet.getDouble(4);
                int classId = resultSet.getInt(5);

                System.out.println("["+id+","+name+","+age+","+score+","+classId+"]");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                // 5.关闭仓库与数据库
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        
    }
}

五.封装JDBCUtils

 每个人的配置都不一样,那我们就读取位置文件。

1.创建db.properties配置文件,写入配置Java进阶十—JDBC_第7张图片

2.创建JDBCUtils类,读取配置

package com.google.util;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {

    public static String url;
    public static String user;
    public static String password;
    public static String driver;

    // 使用静态代码块,做预处理
    static {
        // 使用try/catch包裹
        try {
            // 读取配置文件
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties");
            // 加载对象
            Properties properties = new Properties();;
            properties.load(inputStream);
            // 读取配置
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");

            System.out.println(url+"\n"+user+"\n"+password+"\n"+driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 测试是否能加载成功
    public static void init() {
        System.out.println("加载成功");
    }

    // 创建单例,获取配置项
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(url,user,password);
    }

    // 释放,关闭结果,关闭仓库,关闭数据库连接
    public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException{
        if (resultSet != null){
            resultSet.close();
        }
        if (statement != null){
            statement.close();
        }
        if (connection != null){
            connection.close();
        }
    }

    // 如果没有操作那么,关闭仓库,关闭数据库连接,方法的重载
    public static void close(Connection connection,Statement statement) throws SQLException{
        if  (statement != null){
            statement.close();
        }
        if (connection != null){
            connection.close();
        }
    }


}

3.测试是否能读取 

package com.google.util;

public class Test {
    public static void main(String[] args) {
        JDBCUtils.init();
    }
}

Java进阶十—JDBC_第8张图片

注意:ClassLoader的getResourceAsStream方法使用及在java和web项目中的路径问题_classloader.getresourceasstream-CSDN博客 

六.增删改 executeUpdate() 

 使用创建JDBCUtils类,更新数据

package com.google.util;

import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateTest {

    public Connection connection;
    public Statement statement;

    @Test
    public void updateTest() {
        try {
            // 1.获取数据库连接和操作对象
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            // 2.更新一条数据
            String sql = "update student set score=33 where id=4";
            int res = statement.executeUpdate(sql);

            if (res > 0) {
                System.out.println("更新成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.close(connection,statement);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

Java进阶十—JDBC_第9张图片Java进阶十—JDBC_第10张图片

删除数据

 public void deleteTest() {
        try {
            // 1.获取数据库连接和操作对象
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            // 2.删除一条数据
            String sql = "delete from student where id=6";
            int res = statement.executeUpdate(sql);

            if (res > 0) {
                System.out.println("删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.close(connection,statement);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

Java进阶十—JDBC_第11张图片

 插入数据

 public void insertTest() {
        try {
            // 1.获取数据库连接和操作对象
            connection = JDBCUtils.getConnection();
            statement = connection.createStatement();
            // 2.插入一条数据
            String sql = "insert into student values(6,'LiLi',15,44.3,2)";
            int res = statement.executeUpdate(sql);

            if (res > 0) {
                System.out.println("插入成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.close(connection,statement);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

Java进阶十—JDBC_第12张图片

七.字符编码问题 

如果插入中文时乱码怎么办?

  • 字符编码问题,需要更改数据库、IDE、终端的编码格式为UTF-8,在配置项中添加字符编码Java进阶十—JDBC_第13张图片Java进阶十—JDBC_第14张图片

八. PreparedStatement和问号占位符 

如果想从键盘中输入数据, 而不是像下面这样

String sql = "insert into student values(7,'mimi',33,52.3,1)";

那就把Statement换成PreparedStatement 

1.第一步:修改封装JDBCUtils:把Statement换成PreparedStatement 

package com.google.util;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {

    public static String url;
    public static String user;
    public static String password;
    public static String driver;

    // 使用静态代码块,做预处理
    static {
        // 使用try/catch包裹
        try {
            // 读取配置文件
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties");
            // 加载对象
            Properties properties = new Properties();;
            properties.load(inputStream);
            // 读取配置
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            driver = properties.getProperty("driver");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 测试是否能加载成功
    public static void init() {
        System.out.println("加载成功");
    }

    // 创建单例,获取配置项
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(url,user,password);
    }

    // 释放,关闭结果,关闭仓库,关闭数据库连接
    public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) throws SQLException{
        if (resultSet != null){
            resultSet.close();
        }
        if (preparedStatement != null){
            preparedStatement.close();
        }
        if (connection != null){
            connection.close();
        }
    }

    // 如果没有操作那么,关闭仓库,关闭数据库连接,方法的重载
    public static void close(Connection connection,PreparedStatement preparedStatement) throws SQLException{
        if  (preparedStatement != null){
            preparedStatement.close();
        }
        if (connection != null){
            connection.close();
        }
    }


}

第二步: 使用下面代码方式

package com.google.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Test {

    // 注意此处是静态的
    public static Connection connection;
    public static PreparedStatement preparedStatement;
    // 接收器
    public static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {


        try {
            // 1.获取数据库
            connection = JDBCUtils.getConnection();

            // 2.插入一条数据
            String sql = "insert into student values(?,?)";
            System.out.println("请输入id,年龄,用回车隔开");
            // 接收数据
            int id = scanner.nextInt();
            int age = scanner.nextByte();


            // 3.preparedStatement传入带占位符的sql语句,set方法设置每一个位置的值,并执行更新操作
            preparedStatement = connection.prepareStatement(sql);
            // 将接收器的数据放入sql语句的?中
            preparedStatement.setInt(1,id);
            preparedStatement.setInt(2,age);


            int res = preparedStatement.executeUpdate();

            if (res > 0) {
                System.out.println("插入成功");
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                JDBCUtils.close(connection,preparedStatement);
            } catch (SQLException e) {
                scanner.close();
                throw new RuntimeException(e);
            }
        }

    }

}

Java进阶十—JDBC_第15张图片Java进阶十—JDBC_第16张图片

同理删除与更改一样

读取只需要更改一小部分

            // 2.插入一条数据
            String sql = "select * from student where id=?";
            System.out.println("请输入id");
            // 接收数据
            int id = scanner.nextInt();


            // 3.preparedStatement传入带占位符的sql语句,set方法设置每一个位置的值,并执行更新操作
            preparedStatement = connection.prepareStatement(sql);
            // 将接收器的数据放入sql语句的?中
            preparedStatement.setInt(1,id);


            ResultSet res = preparedStatement.executeQuery();

          while (res.next()) {
              System.out.println(res.getInt(1)+"|"+res.getInt(2));
          }

Java进阶十—JDBC_第17张图片

你可能感兴趣的:(java,开发语言)