JDBC获取连接

public void getConnection() throws Exception {
        //1.获取数据库连接信息
        InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(is);
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");

        //2.加载注册数据库驱动
        Class.forName(driver);

        //3.获取数据库连接
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
    }
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=admin

 

posted on 2022-03-26 17:07  金满仓  阅读(31)  评论(0)    收藏  举报

导航