MYSQL中JDBC工具類的抽取

1.前提準備

use day04_db;
create table user (
  id int primary key auto_increment,
  username varchar(20) not null,
  birthday date,
  sex char(1) default '男',
  address varchar(50)
);

insert into user values (null, '孫悟空','1980-10-24','男','花果山水簾洞');
insert into user values (null, '白骨精','1992-11-12','女','白虎嶺白骨洞');
insert into user values (null, '豬八戒','1983-05-20','男','福臨山雲棧洞');
insert into user values (null, '蜘蛛精','1995-03-22','女','盤絲洞');

select * from user;

2抽取JDBC工具類

抽取工具類的好處:
提高開發效率
減少代碼量

2.1創建jdbc.properties文件

url=jdbc:mysql://localhost/day04_db
username=root
password=root
driverClass=com.mysql.jdbc.Driver


2.2抽取工具類

思想:抽取必要的方法爲靜態代碼塊,調用即執行
在靜態代碼塊中創建註冊驅動的方法
創建連接的方法getConnection
創建釋放資源的方法release

代碼:

package cn.itcast.utils;

import javax.xml.transform.Result;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    //思路:方便對數據庫的使用,省去了大量的創建連接數據庫使用語句
    //基本步驟
    //1.對基本的語句抽取成靜態代碼塊
    //2.創建連接的方法
    //創建銷燬的方法
    private static  String driverClass;
    private static  String url;
    private static String username;
    private static String password;
    static {
        try {
            Properties properties= new Properties();
            properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            driverClass = properties.getProperty("driverClass");
            url = properties.getProperty("url");
            username= properties.getProperty("username");
            password= properties.getProperty("password");
            Class.forName(driverClass);

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

    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




3.使用工具類對數據的增刪改查操作

3.1查詢所有

@Test
    public void test01() throws Exception {
        //查詢所有
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from user";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String id = resultSet.getString("id");
            String username = resultSet.getString("username");
            String birthday = resultSet.getString("birthday");
            String sex = resultSet.getString("sex");
            String address = resultSet.getString("address");
            System.out.println(id + username + birthday + sex + address);
        }


        JDBCUtils.release(connection,preparedStatement,resultSet);
    }


3.2根據id查詢


@Test
    public void test02() throws Exception {
        //通過ID查詢
        Connection connection = JDBCUtils.getConnection();
        String sql="select * from user where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,2);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String id = resultSet.getString("id");
            String username = resultSet.getString("username");
            String birthday = resultSet.getString("birthday");
            String sex = resultSet.getString("sex");
            String address = resultSet.getString("address");
            System.out.println(id + username + birthday + sex + address);
        }
        JDBCUtils.release(connection,preparedStatement,resultSet);

    }

3.3添加數據


@Test
    public void test03() throws Exception {
        //添加數據
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into user values(null,?,?,?,?)";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"quanweiya");
        preparedStatement.setString(2,"2020-06-18");
        preparedStatement.setString(3, "男");
        preparedStatement.setString(4, "北京");
        int i = preparedStatement.executeUpdate();
        if (i == 1) {
            System.out.println("數據添加成功");
        } else {
            System.out.println("數據添加失敗");
        }
        JDBCUtils.release(connection,preparedStatement,null);
    }

3.4修改數據


@Test
    public void test04() throws Exception {
        //修改數據
        Connection connection = JDBCUtils.getConnection();
        String sql = "update user set username=? ,address=? where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "大太陽");
        preparedStatement.setString(2,"北京朝陽區");
        preparedStatement.setInt(3, 10);
        int i = preparedStatement.executeUpdate();
        if (i == 1) {
            System.out.println("數據修改成功");
        } else {
            System.out.println("數據修改失敗");
        }

        JDBCUtils.release(connection,preparedStatement,null);
    }


3.5刪除數據



 @Test
    public void test05() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "delete from user where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, 10);
        int i = preparedStatement.executeUpdate();
        if (i == 1) {
            System.out.println("數據刪除成功");
        } else {
            System.out.println("數據刪除失敗");
        }

        JDBCUtils.release(connection,preparedStatement,null);

    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章