JDBC连接MySQL的终极方法

代码:

	public class JDBCUtils {
    private static Connection connection = null;

	// 获取数据库连接
    public static Connection getConnection() throws Exception {
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbcConnection.properties");
        Properties info = new Properties();
        info.load(resourceAsStream);
        String username = info.getProperty("username");
        String password = info.getProperty("password");
        String driver = info.getProperty("driver");
        String url = info.getProperty("url");

        Class.forName(driver);
        connection = DriverManager.getConnection(url, username, password);

        return connection;
    }

	// 关闭数据库连接,并释放资源
    public static void closeResource(Connection connection, Statement preparedStatement) {
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }

        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
    }

	//通用的增删改操作
    public static void update(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, preparedStatement);
        }
    }
}

	// 通用查询方法
	public <T> List<T> getForList(Class<T> clazz, String sql, Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSetMetaData metaData = null;
        ResultSet resultSet = null;
        ArrayList<T> arrayList = new ArrayList<>();

        try {
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }

            resultSet = preparedStatement.executeQuery();
            metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            while (resultSet.next()) {
                T t = clazz.getDeclaredConstructor().newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object value = resultSet.getObject(i + 1);
                    String columnLabel = metaData.getColumnLabel(i + 1);

                    Field declaredField = t.getClass().getDeclaredField(columnLabel);
                    declaredField.setAccessible(true);
                    declaredField.set(t, value);
                }
                arrayList.add(t);
            }
            return arrayList;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

IDEA中直接使用properties文件的方法:

  1. 在模块中新建一个目录,命名为resource
  2. 右键单击这个目录,在最下方找标记目录为,然后点标记为测试资源
  3. 在目录中放置properties文件即可。

Properties中的信息(注意等号前后一定不要有空格),以及注意事项

username=root // 你自己的登录账户
password=123456789// 你自己的登录密码
driver=com.mysql.cj.jdbc.Driver// MySQL8之前的去掉.cj即可
url=jdbc:mysql://localhost:3306/jdbc_learn?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true

注意事项
// jdbc:mysql://可以类比http:://  表示连接所使用的的协议
// localhost::3306这个是你所要连接MySQL数据库的端口号,根据你自己本机的情况来,因为这个是可以修改的
// jdbc_learn? 这个是数据库名。也是按照你想要连接的库名来修改,这里只是一个例子

// serverTimezone=Asia/Shanghai这个是MySQL8.x必须的,设置连接服务器的时区
// 注意不要看那些复制粘贴的大神告诉你的时区填 UTC,纯属坑人。
// 会造成你存进去的日期比我们这里慢一天。不懂的为什么的,去看高中地理

// useSSL=false。这个是关闭SSL连接,MySQL8.x不需要发起SSL连接了
// allowPublicKeyRetrieval=true。这个表示允许从服务器获取公钥。
// 默认是关闭的。打开会造成不安全,但是有很多人会遇到连接错误,不过学习阶段也无所谓了
// 所以最好加上,避免出现一些奇奇怪怪的问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风舞落叶殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值