Java连接数据库
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
public class JDBCUtil {
private JDBCUtil() {
}
private static String URL = null;
private static String USERNAME = null;
private static String PASSWORD = null;
private static Connection conn = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
private static ResultSetMetaData rsd = null;
private static Logger log = Logger.getLogger(JDBCUtil.class);
static {
try {
InputStream is = JDBCUtil.class
.getResourceAsStream("/mysql.properties");
Properties pts = new Properties();
pts.load(is);
Class.forName(pts.getProperty("forName"));
URL = pts.getProperty("url");
USERNAME = pts.getProperty("user");
PASSWORD = pts.getProperty("password");
} catch (Exception e) {
System.out.println("数据库连接失败!");
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void Close(Connection conn, Statement stat) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
}
}
}
public static void Close(Connection conn, Statement stat, ResultSet rst) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
}
}
if (rst != null) {
try {
rst.close();
} catch (SQLException e) {
}
}
}
public static int executeUpdate(String sql, Object[] param) {
Connection conn = getConnection();
ps = null;
int count = 0;
try {
ps = conn.prepareStatement(sql);
setParameter(param);
count = ps.executeUpdate();
} catch (SQLException e) {
log.error(e);
} finally {
Close(conn, ps);
}
return count;
}
public static String columnFormat(String[] str) {
String fieldName = "";
for (int j = 0; j < str.length; j++) {
if (j != 0) {
str[j] = str[j].substring(0, 1).toUpperCase()
+ str[j].substring(1);
}
fieldName += str[j];
}
return fieldName;
}
public static int executeProcedure(String sql, Object[] param) {
int i = 0;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
setParameter(param);
rs = ps.executeQuery();
while(rs.next()){
i = rs.getInt(1);
}
} catch (Exception e) {
System.out.println("数据库存储过程异常:"+e);
}finally {
Close(conn, ps, rs);
}
return i;
}
public static List<Object> executeQuery(String sql, Object[] param,
Class clz) {
List<Object> list = new ArrayList<Object>();
Object obj = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
setParameter(param);
rs = ps.executeQuery();
rsd = rs.getMetaData();
int columnCount = rsd.getColumnCount();
while (rs.next()) {
obj = clz.newInstance();
for (int i = 1; i <= columnCount; i++) {
String[] str = rsd.getColumnName(i).toLowerCase()
.split("_");
String fieldName = columnFormat(str);
Field filed = clz.getDeclaredField(fieldName);
filed.setAccessible(true);
String columnName = rsd.getColumnName(i);
Object value = rs.getObject(columnName);
filed.set(obj, value);
}
list.add(obj);
}
} catch (SQLException e) {
log.fatal("数据库异常:" + e);
} catch (NoSuchFieldException e) {
log.fatal(e);
} catch (SecurityException e) {
log.fatal(e);
} catch (IllegalArgumentException e) {
log.fatal(e);
} catch (IllegalAccessException e) {
log.fatal(e);
} catch (InstantiationException e) {
log.fatal(e);
} finally {
Close(conn, ps, rs);
}
return list;
}
private static void setParameter(Object[] param) throws SQLException {
for (int i = 1; i <= param.length; i++) {
ps.setObject(i, param[i - 1]);
}
}
}