标签:
// (1)使用Class.forName来导入drive Class.forName("oracle.jdbc.driver.OracleDriver"); // (2)使用DriverManager.getconnection(url,用户名,密码)建立连接返回类型是Connection类型 conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl", "news", "news");
将以上信息放入资源文件,重复使用
database.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.uname=news jdbc.upwd=news
configManager.class
package pb.until; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigManager { ///声明静态变量 private static ConfigManager confingManager ; //声明静态变量Properties private static Properties properties; private ConfigManager(){ //获取文件路径 String configfile="database.properties"; //声明新的Properties对象 properties = new Properties(); InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream(configfile); //使用load方法来读取文件 try { properties.load(is); is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //创建单个例对象 public static ConfigManager getintance(){ if(confingManager==null){ confingManager=new ConfigManager(); } return confingManager; } public String getvalue(String key){ return properties.getProperty(key); } }
String driver=ConfigManager.getintance().getvalue("jdbc.driver"); String url =ConfigManager.getintance().getvalue("jdbc.url"); String uname =ConfigManager.getintance().getvalue("jdbc.uname"); String upwd =ConfigManager.getintance().getvalue("jdbc.upwd"); try { // (1)使用Class.forName来导入drive Class.forName(driver); // (2)使用DriverManager.getconnection(url,用户名,密码)建立连接返回类型是Connection类型 conn = DriverManager.getConnection(url, uname, upwd);
标签:
原文地址:http://www.cnblogs.com/liunanjava/p/4246049.html