Java的SqlHelper 工具类


context.xml中添加以下内容
<Resource name="jdbc/TestDB" auth="Container" 
    		   type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="hufan.88" 
               driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://127.0.0.1:3306/hoofan"/>

import java.sql.*;
import java.util.ArrayList;
import javax.naming.*;
import javax.sql.DataSource;


public class SqlHelper {
	private static DataSource ds;
	
	static{
		try {
			//1.获取命名上下文接口
			Context context = new InitialContext();
			//2.根据名称查询服务器上的DataSource  代表前缀: java:/comp/env  
			ds = (DataSource)context.lookup("java:/comp/env/jdbc/TestDB");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取数据库连接
	 * @return Connection
	 * */
	public static Connection getConnection(){
		try {
			return ds.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 关闭资源
	 * @param Connection con,Statement stm,ResultSet rs
	 * */
	public static void close(ResultSet rs,PreparedStatement ps,Connection ct){
		try {
			if(rs!=null)rs.close();
			if(ps!=null)ps.close();
			if(ct!=null)ct.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public ArrayList executeQuery(String sql,String[] arr){
		Connection ct=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ct=getConnection();
			ps=ct.prepareStatement(sql);
			if(arr!=null&&!arr.equals("")){
				for(int i=0;i<arr.length;i++){
					ps.setString(i+1, arr[i]);
				}
			}
			rs=ps.executeQuery();
			ArrayList al=new ArrayList();
			ResultSetMetaData rsmd=rs.getMetaData();
			int column=rsmd.getColumnCount();
			while(rs.next()){
				Object[] ob=new Object[column];
				for(int i=0;i<ob.length;i++){
					ob[i]=rs.getObject(i+1);
				}
				al.add(ob);
			}
			return al;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			close(rs,ps,ct);
		}
	}
	public void executeUpdate(String sql,String arr[]){
		Connection ct=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ct=getConnection();
			ps=ct.prepareStatement(sql);
			if(arr!=null&&!arr.equals("")){
				for(int i=0;i<arr.length;i++){
					ps.setString(i+1, arr[i]);
				}
			}
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
package com.utils;
import java.sql.*;
import java.util.*;
public class SqlHelper {
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private Connection getConnection(){
		try {
			return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hufan","root","hufan.88");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public static void close(ResultSet rs,PreparedStatement ps,Connection ct){
		try {
			if(rs!=null)rs.close();
			if(ps!=null)ps.close();
			if(ct!=null)ct.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public ArrayList executeQuery(String sql,String[] arr){
		Connection ct=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ct=getConnection();
			ps=ct.prepareStatement(sql);
			if(arr!=null&&!arr.equals("")){
				for(int i=0;i<arr.length;i++){
					ps.setString(i+1, arr[i]);
				}
			}
			rs=ps.executeQuery();
			ArrayList al=new ArrayList();
			ResultSetMetaData rsmd=rs.getMetaData();
			int column=rsmd.getColumnCount();
			while(rs.next()){
				Object[] ob=new Object[column];
				for(int i=0;i<ob.length;i++){
					ob[i]=rs.getObject(i+1);
				}
				al.add(ob);
			}
			return al;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			close(rs,ps,ct);
		}
	}
	public void executeUpdate(String sql,String arr[]){
		Connection ct=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			ct=getConnection();
			ps=ct.prepareStatement(sql);
			if(arr!=null&&!arr.equals("")){
				for(int i=0;i<arr.length;i++){
					ps.setString(i+1, arr[i]);
				}
			}
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}


你可能感兴趣的:(Java的SqlHelper 工具类)