//配置文件:config.properties放在classpath下 jdbc_driver=com.mysql.jdbc.Driver jdbc_username=root jdbc_password= jdbc_url=jdbc\:mysql\://localhost\:3306/student?useUnicode\=true&characterEncoding\=utf-8 //读取properties文件的工具类 package com.util; import java.io.IOException; import java.util.Properties; public class ProMgr { private static Properties props = new Properties(); static { try { props.load(ProMgr.class.getClassLoader().getResourceAsStream( "config.properties")); } catch (IOException e) { e.printStackTrace(); } } private ProMgr() { } public static String getProperty(String key) { return props.getProperty(key); } } //数据库连接类: package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import com.util.ProMgr; public class DBC { // private static final String DBDRIVER="oracle.jdbc.driver.OracleDriver"; // private static final String USERNAME="scott"; // private static final String PASSWORD ="tiger"; // private static final String // URL="jdbc:oracle:thin@localhost:1521:student"; // private static final String DBDRIVER = "com.mysql.jdbc.Driver"; // private static final String USERNAME = "root"; // private static final String PASSWORD = ""; // private static final String URL = // "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8"; private static final String DBDRIVER = ProMgr.getProperty("jdbc_driver"); private static final String USERNAME = ProMgr.getProperty("jdbc_username"); private static final String PASSWORD = ProMgr.getProperty("jdbc_password"); private static final String URL = ProMgr.getProperty("jdbc_url"); private Connection conn; public DBC() { try { Class.forName(DBDRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public Connection conn() { try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } public void close() { if (conn != null) { try { conn.close(); conn = null; } catch (SQLException e) { e.printStackTrace(); } } } } //测试类: package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import com.util.ProMgr; public class DBC { // private static final String DBDRIVER="oracle.jdbc.driver.OracleDriver"; // private static final String USERNAME="scott"; // private static final String PASSWORD ="tiger"; // private static final String // URL="jdbc:oracle:thin@localhost:1521:student"; // private static final String DBDRIVER = "com.mysql.jdbc.Driver"; // private static final String USERNAME = "root"; // private static final String PASSWORD = ""; // private static final String URL = // "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8"; private static final String DBDRIVER = ProMgr.getProperty("jdbc_driver"); private static final String USERNAME = ProMgr.getProperty("jdbc_username"); private static final String PASSWORD = ProMgr.getProperty("jdbc_password"); private static final String URL = ProMgr.getProperty("jdbc_url"); private Connection conn; public DBC() { try { Class.forName(DBDRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public Connection conn() { try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } public void close() { if (conn != null) { try { conn.close(); conn = null; } catch (SQLException e) { e.printStackTrace(); } } } }
?