package com.whty.util;
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.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
public class JdbcSource {
private static Logger logger = Logger.getLogger(JdbcSource.class);
public static Connection getConnection() throws SQLException {
Context envContext;
Connection conn = null;
try {
envContext = (Context) new InitialContext()
.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/R2"); // 查找配置
conn = ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
}
return conn;
}
public static void free(ResultSet rs, Statement st, Connection con) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
} finally {
try {
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void updatecollection(String sqlStr) throws Exception {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = getConnection();
st = con.createStatement();
st.executeUpdate(sqlStr);
} finally {
free(rs, st, con);
}
}
public static List<Map<String, String>> createStatement(String sqlStr) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
try {
// 创建一个jdbc声明
con = getConnection();
st = con.createStatement();
// 执行查询
rs = st.executeQuery(sqlStr);
ResultSetMetaData rsmd = rs.getMetaData();
rsmd = rs.getMetaData();
int len = rsmd.getColumnCount();
while (rs.next()) {
Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i <= len; i++) {
map.put(rsmd.getColumnName(i), rs.getString(i));
}
list.add(map);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
free(rs, st, con);
}
return list;
}
public static List<Map<String, String>> updateStatement(String sqlStr) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
try {
con = getConnection();
st = con.createStatement();
st.executeQuery(sqlStr);
} catch (SQLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
free(rs, st, con);
}
return list;
}
public static ResultSet executeQuery(String sql) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
// 创建一个jdbc声明
con = getConnection();
st = con.createStatement();
// 执行查询
rs = st.executeQuery(sql);
return rs;
} catch (SQLException e) {
e.printStackTrace();
} finally {
//free(rs, st, con);
}
return rs;
}
public static List<Map<String, String>> createStatement(String sql,List str) {
Connection con=null;
PreparedStatement pre = null;
ResultSet rs = null;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
try {
// 创建一个jdbc声明
con = getConnection();
pre=con.prepareStatement(sql);
for(int i=0;i<str.size();i++){
pre.setString(i+1, (String)str.get(i));
}
// 执行查询
rs = pre.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
rsmd = rs.getMetaData();
int len = rsmd.getColumnCount();
while (rs.next()) {
Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i <= len; i++) {
map.put(rsmd.getColumnName(i), rs.getString(i));
}
list.add(map);
}
} catch (SQLException e){
logger.info("select error",e);
} finally {
free(rs, pre, con);
}
return list;
}
public static Boolean updatesStatement(String sql,List str){
Boolean TF =false;
Connection con=null;
PreparedStatement pre = null;
ResultSet rs = null;
try{
con = getConnection();
pre=con.prepareStatement(sql);
for(int i=0;i<str.size();i++){
pre.setString(i+1, (String)str.get(i));
}
pre.executeUpdate();
TF = true;
}catch(Exception e){
logger.info("update",e);
TF =false;
e.printStackTrace();
}finally {
free(rs, pre, con);
}
return TF;
}
}
JDBC
最新推荐文章于 2022-04-18 13:59:27 发布