JDBC

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;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值