package com.jdbc.util;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
static String driverClass = null;
static String url = null;
static String name = null;
static String password = null;
static {
try {
Properties properties = new Properties();
InputStream is = new FileInputStream("jdbc.properties");
properties.load(is);
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, name, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs) {
closeRs(rs);
closeSt(st);
closeConn(conn);
}
private static void closeRs(ResultSet rs) {
try {
if(rs !=null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs = null;
}
}
private static void closeSt(Statement st) {
try {
if(st !=null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
st = null;
}
}
private static void closeConn(Connection conn) {
try {
if(conn !=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn = null;
}
}
}
properties配置文件
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/(数据库名字)
name=root
password=密码