static{ //加载驱动
try{
//方案一:反射加载驱动
// Class.forName(Myproperties.getInstance().getProperty("driver")); // 这个无依赖
//方案二:驱动注册
java.sql.DriverManager.registerDriver(new OracleDriver()); //有依赖,这个必须要有相应的类
}catch(Exception e){
System.out.println("系统无有效驱动");
e.printStackTrace();
System.exit(0);
}
}
//获取配置文件里面的信息
public class Myproperties extends Properties {
private static Myproperties instance=new Myproperties();
private Myproperties(){
InputStream iis= this.getClass().getClassLoader().getSystemResourceAsStream("db.properties");
try {
this.load(iis);
} catch (Exception e) {
// TODO: handle exception
}finally{
if(iis!=null)
{
try {
iis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static Myproperties getInstance()
{
if(instance==null)
{
instance=new Myproperties();
}
return instance;
}
properties文件的信息
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@//localhost:1521/orcl.COM
user=scott
pwd=a