2020-09-28

数据库的连接*

第一步:首先配置文件中的信息
第二步:加载驱动
第三步:获取连接`
jdbc.properies的配置

username=root
password=root
url=jdbc:mysql://localhost:3306/book?rewriteBatchedStatements=true
driverClass=com.mysql.jdbc.Driver

public class Connection {
   @Test
     public void getConnection() throws Exception{
       //读取配置文件中的信息
       InputStream inputStream =ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
//    InputStream inputStream1 =ClassLoader.getSystemResourceAsStream("jdbc.properties");
       Properties properties =new Properties();
       properties.load(inputStream);
       String username = properties.getProperty("username");
       String password = properties.getProperty("password");
       String url = properties.getProperty("url");
       String driverClass = properties.getProperty("driverClass");
       //加载驱动
     Class.forName(driverClass);
       //获取连接
       java.sql.Connection connection = DriverManager.getConnection(url, username, password);
       System.out.println(connection);
   }

}

 

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页