[java]代码库
package com.dtl.rmgr.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class DB {
private static String url;
private static String username;
private static String password;
private static String classDriver;
static {
try {
InputStream in = DB.class.getResourceAsStream("/jdbc.properties");
Properties prop = new Properties();
prop.load(in);
in.close();
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
classDriver = prop.getProperty("classDriver");
Class.forName(classDriver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// static {
// try {
// url = "jdbc:oracle:thin:@localhost:1521:oratlq";
// username = "rmgr";
// password = "rmgr";
// classDriver = "oracle.jdbc.OracleDriver";
// Class.forName(classDriver);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
// 获取一个连接
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
// 关闭一个连接
public static void closeConn(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 执行增,删,改
public static int update(Connection conn, String sql, Object... parames) {
int count = 0;
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
for (int i = 0; i < parames.length; i++) {
pstmt.setObject(i + 1, parames[i]);
}
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
public static int update(String sql, Object... parames) {
Connection conn = null;
try {
conn = getConn();
return update(conn, sql, parames);
} finally {
closeConn(conn);
}
}
// 执行查询 找到则放回lists 否则为空
public static <T> List<T> query(Connection conn, String sql,
IResultSet<T> irs, Object... parames) {
List<T> lists = null;
try {
lists = new ArrayList<T>();
PreparedStatement pstmt = conn.prepareStatement(sql);
for (int i = 0; i < parames.length; i++) {
pstmt.setObject(i + 1, parames[i]);
}
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
// 读取一行放到lists 集合中
T t = irs.readRow(rs);
lists.add(t);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lists;
}
public static <T> List<T> query(String sql, IResultSet<T> irs,
Object... parames) {
Connection conn = null;
List<T> lists = null;
try {
conn = getConn();
lists = query(conn, sql, irs, parames);
} finally {
DB.closeConn(conn);
}
return lists;
}
public static int getMaxId(String sql) {// 获取数据库中department表的最大id
int id = 1;
Connection conn = DB.getConn();
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
id += rs.getInt(1);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return id;
}
}