import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JDBC {
private static JDBC jdbc;
// 双重检查
public static JDBC getInstance() {
if (jdbc == null) {
synchronized(JDBC.class) {
if (jdbc == null) {
jdbc = new JDBC();
}
}
}
return jdbc;
}
// 同步方法实现
public static synchronized JDBC getInstance2() {
if (jdbc == null) {
jdbc = new JDBC();
}
return jdbc;
}
public static Properties props = new Properties();
private static final Log log = LogFactory.getLog(JDBC.class);
static {
try {
props.load(JDBC.class.getClassLoader()
.getResourceAsStream("jdbc_mysql.properties"));
Class.forName(props.getProperty("driver"));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
*
* @return 连接
*/
public static Connection getConnection() {
Connection conn = null;
String url = props.getProperty("url");
String user = props.getProperty("user");
String password = props.getProperty("password");
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
log.error("建立连接失败");
}
return conn;
}
/**
* 释放资源
*
* @param rs 结果集
* @param stmt 执行语句
* @param conn 连接
*/
public static void freeResource(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.error("关闭数据集失败");
}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.error("关闭执行语句失败");
}
rs = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.error("关闭连接失败");
}
conn = null;
}
}
/**
* 释放资源
*
* @param stmt 执行语句
* @param conn 连接
*/
public static void freeResource(Statement stmt, Connection conn) {
freeResource(null, stmt, conn);
}
/**
* 释放资源
*
* @param conn 连接
*/
public static void freeResource(Connection conn) {
freeResource(null, null, conn);
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = getConnection();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from t");
while (rs.next()) {
System.out.println(rs.getObject("t") + "/t");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
freeResource(rs, stmt, conn);
}
}
}
jdbc_mysql.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=root
characterEncoding=gb2312
log4j.properties
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=d:/out.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n