package danli;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import jdbc_test.DBConnection;
import com.mysql.jdbc.Statement;
public class DBConnect_OracleWithProperties {
private static String driverName=null;
private static String url=null;
private static String user=null;
private static String password=null;
private DBConnect_OracleWithProperties() {
}
static {
/**
* 读取配置文件
*/
Properties properties = new Properties();
try {
properties.load(DBConnect_OracleWithProperties.class.getClassLoader().getResourceAsStream("db.properties"));
url=properties.getProperty("url");
driverName=properties.getProperty("driverClass");
user=properties.getProperty("user");
password=properties.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 得到Connection类型的conn
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void close(ResultSet rs, PreparedStatement ps,
Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws SQLException {
Connection connection = DBConnect_OracleWithProperties.getConnection();
if (connection==null) {
System.out.println("失败");
} else {
System.out.println("成功");
}
}
}
配置文件:
<span style="font-size:14px;">url=jdbc\:oracle\:thin\:@10.1.5.7\:1521\:devel
user=CWDB_LDL
password=CWDB_LDL
driverName=com.mysql.jdbc.Driver</span>