封装JDBC基本操作的工具类
//在src目录下:类路径 xxx.properties
//driverClass=com.mysql.jdbc.Driver
//url=jdbc:mysql://localhost:3306/库名
//username=root
//password=登录的密码
class JdbcUtils{
private static String drierClass = null ;
private static String url = null ;
private static String username = null ;
private static String password = null ;
private JdbcUtils(){}
//提供静态代码块:随着类的加载而加载
static{
//创建属性列表:属性集合类 Properties
Properties prop = new Properties() ;
//读取配置文件 获取配置文件所在的输入流对象
InputStream inputStream = JdbcUtils.class.getClassLoader().getResurcesAsStream("xxx.properties") ;
//将流对象中的加载属性列表中prop
prop.load(inputStream) ;
//通过key获取它里面的value
drierClass = prop.getProperty("driverClass") ;
url = prop.getProperty("url") ;
username = prop.getProperty("username") ;
password = prop.getProperty("password") ;
//注册驱动
Class.forName(drierClass) ;
}
//获取连接对象
public static Connection getConnection(){
Connnection conn = DriverManager.getConnection(url,username,password) ;
return conn ;
}
//释放