oracle JDBC类

<strong>db.properties文件:</strong>

jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.user=*****
jdbc.password=*****


==================严谨的分割线========================


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * oracle数据库JDBC类
 * 
 * @author Dimon
 * 
 */
public class DBManager {
    private static Properties properties = new Properties();
    
    private static String driver = null;
    
    private static String url = null;
    
    private static String user = null;
    
    private static String pwd = null;
    static {
        try {
            // 加载配置文件
            properties.load(DBManager.class.getClassLoader().getResourceAsStream("db.properties"));
            driver = properties.getProperty("jdbc.driver");
            url = properties.getProperty("jdbc.url");
            user = properties.getProperty("jdbc.user");
            pwd = properties.getProperty("jdbc.password");
            Class.forName(driver);
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 数据库连接
     * 
     * @return
     * @throws SQLException
     */
    protected static Connection openConnection()
        throws SQLException {
        return DriverManager.getConnection(url, user, pwd);
    }
    
    /**
     * 关闭连接
     * 
     * @param con
     */
    protected static void closeConnection(Connection con) {
        if (con != null) {
            try {
                con.close();
            }
            catch (SQLException e) {
            }
        }
    }
}


你可能感兴趣的:(oracle JDBC类)