JDBC工具类封装

jdbc工具类的封装:

package com.hyc.study02.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String psw = null;

    static {
        try {
            InputStream inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);

            driver = properties.getProperty("driver");
            //2、获取用户信息、url
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            psw = properties.getProperty("psw");
            //驱动只用加载一次
            //1、加载驱动
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, psw);
    }

    //释放连接
    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

插入数据测试:

package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestInsert {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            //3、获取数据库连接
            connection = JDBCUtils.getConnection();

            //4、创建执行sql的对象
            statement = connection.createStatement();

            //5、执行sql语句
            //6、返回执行结果集
            String sql = "INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " +
                    "VALUES (4,'hyc','123456','[email protected]','2022-04-18')";
            int num = statement.executeUpdate(sql);
            if (num > 0) {
                System.out.println("插入数据成功!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //7、释放连接
            JDBCUtils.release(connection, statement, resultSet);
        }
    }
}

删除数据测试:

package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestDelete {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //3、获取数据库连接
            connection = JDBCUtils.getConnection();

            //4、创建执行sql的对象
            statement = connection.createStatement();

            //5、执行sql语句
            //6、返回执行结果集
            String sql = "DELETE FROM `users` WHERE id=4";
            int num = statement.executeUpdate(sql);
            if (num > 0) {
                System.out.println("删除数据成功!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //7、释放连接
            JDBCUtils.release(connection, statement, resultSet);
        }
    }
}

修改数据测试:

package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestUpdate {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //3、获取数据库连接
            connection = JDBCUtils.getConnection();

            //4、创建执行sql的对象
            statement = connection.createStatement();

            //5、执行sql语句
            //6、返回执行结果集
            String sql = "UPDATE `users` SET `NAME`='zxzb',`email`='[email protected]' WHERE `id`=1";
            int num = statement.executeUpdate(sql);
            if (num > 0) {
                System.out.println("更新数据成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //7、释放连接
            JDBCUtils.release(connection, statement, resultSet);
        }
    }
}

查询数据测试:

package com.hyc.study02;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestSelect {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            //3、获取数据库连接
            connection = JDBCUtils.getConnection();

            //4、创建执行sql的对象
            statement = connection.createStatement();

            //5、执行sql语句
            //6、返回执行结果集
            String sql = "SELECT * FROM `users` WHERE id=1";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("NAME"));
                System.out.println(resultSet.getString("PASSWORD"));
                System.out.println(resultSet.getString("email"));
                System.out.println(resultSet.getDate("birthday"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //7、释放连接
            JDBCUtils.release(connection, statement, resultSet);
        }
    }
}

你可能感兴趣的:(数据库,java,mysql)