zoukankan      html  css  js  c++  java
  • Java MySQL连接

    1.新建一个properties文件,将数据库的配置参数写入

    url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    user=root
    password=123456
    driverClass=com.mysql.cj.jdbc.Driver

    2.连接的函数

     public static Connection getConnection() throws Exception {
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
    
            Properties properties = new Properties();
            properties.load(is);
    
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driverClass = properties.getProperty("driverClass");
    
            Class.forName(driverClass);
    
            Connection connection = DriverManager.getConnection(url, user, password);
    
            return connection;
        }
  • 相关阅读:
    Annotation
    jdbc
    集合
    与运行环境交互
    计数排序and基数排序
    面向对象下
    面向对象
    流程控制与数组
    庆祝自己的BLOG开张
    Raspberry Pi配置为无线路由器
  • 原文地址:https://www.cnblogs.com/Gazikel/p/14149158.html
Copyright © 2011-2022 走看看