首页 > 数据库技术 > 详细

Java通过Properties加载数据库

时间:2020-12-21 18:21:24      阅读:21      评论:0      收藏:0      [点我收藏+]

通过配置文件加载数据库

前言

在b站上看了狂神的MySql,没写过的博客的我第一次有了写一篇博客的想法,但最近又是期末,时间比较紧张。就自己动手码了一遍,其中也有很多不清楚的知识,在这里总结一下。

参考博客链接

  • Properties类: 继承自HashTable(HashTable又是什么-。-),直接已知子类有Provider。用于在java中配置文件。.properties文件中以键值对的方式存放数据。

    • 用到的方法:getProperty(String)获取属性

      //在此属性列表中搜索具有指定键的属性。如果在此属性列表中找不到该键,则会检查默认属性列表及其默认值(递归)。如果未找到该属性,则该方法返回默认值参数。
      public String getProperty(String key) {
              Object oval = this.map.get(key);
              String sval = oval instanceof String ? (String)oval : null;
              Properties defaults;
              return sval == null && (defaults = this.defaults) != null ? defaults.getProperty(key) : sval;
          }
      
          public String getProperty(String key, String defaultValue) {
              String val = this.getProperty(key);
              return val == null ? defaultValue : val;
          }
      

代码

1.创建properties文件

driver = com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3304/test?useUnicode=true&character=utf8&useSSL=true
username=root
password=123456

2.通过反射获得相同src目录下的properties文件

public class utils {

    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static {
        try {
            InputStream in = utils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            //加载驱动
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url);
    }

    //释放连接

    public void release(ResultSet resultset, Connection connection, PreparedStatement preparedStatement) throws SQLException {
        if (resultset != null){
            resultset.close();
        }
        if (connection != null){
            connection.close();
        }
        if (preparedStatement != null){
            preparedStatement.close();
        }
    }

}

3.测试SQL插入

package com.ctgu.px.ctgu.px;

import com.ctgu.px.JdbcTest;

import java.sql.*;

/**
 * @author px
 */
public class TestInsert {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //获得数据库连接
            connection = utils.getConnection();

            //获得SQL的执行对象
            statement = (PreparedStatement) connection.createStatement();

            //
            String sql = "";
            int i = statement.executeUpdate(sql);

            if (i != 0) {
                System.out.println("插入成功!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(connection, statement, null);
        }
    }

}

Java通过Properties加载数据库

原文:https://www.cnblogs.com/cspx/p/14168098.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!