import java.sql.*;import java.util.Properties;import java.io.InputStream;import java.io.IOException;public abstract class ConnDB { private final static String DRIVER; private final static String URL; private final static String USERNAME; private final static String PASSWORD; private Connection conn; /** * 读取数据库配置文件,并加载数据库驱动 */ static { Properties props = new Properties(); InputStream in = ConnDB.class .getResourceAsStream("database.properties"); try { props.load(in); in.close(); } catch (IOException e) { e.printStackTrace(); } DRIVER = props.getProperty("jdbc.driver"); try { // System.setProperty("jdbc.drivers", DRIVER); Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } URL = props.getProperty("jdbc.url"); USERNAME = props.getProperty("jdbc.username"); PASSWORD = props.getProperty("jdbc.password"); } /** * 获得数据库连接 * * @return Connection */ public Connection getConnDB() { conn = null; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } public ConnDB() { } }