JDBC连接工厂类ConnectionFactory
package com.yaku.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
* 数据库连接工厂类,专门取得数据连接的
* @author hery
*/
public class ConnectionFactory {
private static Properties config = new Properties();
static {
InputStream in = ConnectionFactory.class.getResourceAsStream("db-config.properties");
if (in == null) {
throw new RuntimeException("not found the DBConfig file");
}
try {
config.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError("the file load fail!");
}
}
public static Connection getDirectConnection() {
Connection con = null;
String jdbcDriver = null;
String jdbcUrl = null;
String userName = null;
String password = null;
try {
jdbcDriver = config.getProperty("drivername");
jdbcUrl = config.getProperty("jdbcurl");
userName = config.getProperty("username");
password = config.getProperty("password");
// Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName(jdbcDriver);
// con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.13:1521:sta","sta01","sta02");
con = DriverManager.getConnection(jdbcUrl, userName, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static Connection getJNDIConnection() {
Connection con = null;
String jndiName = config.getProperty("jndiName");
try {
Context context = new InitialContext();
DataSource ds = (DataSource) context.lookup("java:comp/env/"
+ jndiName);
con = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static Connection getConnection() throws SQLException {
Connection con = null;
if (config.getProperty("jndiName") != null) {
try {
con = getJNDIConnection();
} catch (Exception e) {
e.printStackTrace();
}
if (con == null) {
con = getDirectConnection();
}
} else {
con = getDirectConnection();
}
return con;
}
@SuppressWarnings("static-access")
public static void main(String args[]) throws SQLException {
ConnectionFactory cf = new ConnectionFactory();
System.out.println(cf.getConnection());
}
}
JDBC关闭工厂类CloseUtil
package com.yaku.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CloseUtil {
public static void close(Statement st, Connection con) {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement st, Connection con) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
close(st, con);
}
}
配置文件db-config.properties
drivername = com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcurl = jdbc:sqlserver://192.168.1.2:1433;DatabaseName=Test
username = sa
password =sa