日期:2014-05-16 浏览次数:20397 次
package baor.test.sql; public abstract class ConnPropInfo { public abstract String getUrl(); public abstract String getDriver(); public abstract String getUser(); public abstract String getPwd(); }
package baor.test.sql; import java.util.Properties; public class ConnPropInfoMySQL extends ConnPropInfo { private String url; private String driver; private String pwd; private String user; // static String defaultDriver = "com.mysql.jdbc.Driver"; @Override public String getUrl() { // TODO Auto-generated method stub System.out.println(url); return url; } @Override public String getDriver() { // TODO Auto-generated method stub System.out.println(driver); return driver; } @Override public String getUser() { // TODO Auto-generated method stub System.out.println(user); return user; } @Override public String getPwd() { // TODO Auto-generated method stub System.out.println(pwd); return pwd; } public ConnPropInfoMySQL(PropInfoUtil prop) { Properties demoProp = prop.LoadPropInfo(); this.url = demoProp.getProperty("url"); this.driver = demoProp.getProperty("driver"); this.pwd = demoProp.getProperty("pwd"); this.user = demoProp.getProperty("user"); } }
package baor.test.sql; public abstract class PropInfoFactory { public abstract ConnPropInfo createPropInfo(); }
package baor.test.sql; public class PropMySQLFactory extends PropInfoFactory { PropInfoUtil prop ; @Override public ConnPropInfo createPropInfo() { // TODO Auto-generated method stub prop = new PropInfoUtil(); return new ConnPropInfoMySQL(prop); } }
package baor.test.sql; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; public class PropInfoUtil { public Properties LoadPropInfo() { Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream("DB.properties"); prop.load(input); System.out.println(prop.getProperty("driver")); System.out.println(prop.getProperty("url")); System.out.println(prop.getProperty("user")); System.out.println(prop.getProperty("pwd")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return prop; } }
package baor.test.sql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnUtil { Connection connection = null; public Connection getConnection (ConnPropInfo prop){ if (connection == null) { try { // 调用Class.forName()方法加载驱动程序 Class.forName(prop.getDriver()); System.out.println("成功加载MySQL驱动!"); connection = DriverManager.getConnection(prop.getUrl(), prop.getUser(), prop.getPwd()); System.out.print("成功连接到数据库!"); } catch (ClassNotFoundException e1) { System.out.println("找不到MySQL驱动!"); e1.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return connection; } public void closeConnection() { try { if (connection !=null ) { connection.close(); connection = null; System.out.println("连接关闭!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String args[]) { //测试