package cn.JDBCUtils.com; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class JDBCUtils { private static Properties props =null; static{ try{ InputStream in = JDBCUtils.class.getClassLoader() .getResourceAsStream("dbconfig.properties"); props = new Properties(); props.load(in);}catch(Exception e){ throw new RuntimeException(); } try{ Class.forName(props.getProperty("driverClassName")); }catch (Exception e) { throw new RuntimeException(); } } public static Connection getConnection() throws SQLException{ /* * 1、加载配置文件 * 2、加载驱动类 * 3、调用DriverManager.getConnection() * */ return DriverManager.getConnection(props.getProperty("url"),props.getProperty("username"), props.getProperty("password")); } }
//四大参数.propertise
driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://127.0.0.1\:3306/mydb1 username=root password=123
时间: 2024-10-15 08:54:53