SqlHelper工具类

package com.wsx.utils;
import java.io.*;
import java.util.*;
import java.sql.*;
public class SqlHelper {
 //定义需要的变量
	private static Connection ct=null;
	private static PreparedStatement ps=null;
	private static ResultSet rs=null;
	//连接数据库参数
	private static String url="";
	private static String username="";
	private static String driver="";
	private static String password="";
	
	private static Properties pp=null;
	private static InputStream fis=null;
	//加载驱动,只需要一次
	static{
		//从dbinfo.properties文件中读取配置文件
		try {
			pp=new Properties();
//			fis=new FileInputStream("dbinfo.properties");
			
			//当我们使用java web的时候,读取文件要使用类加载器
			fis=SqlHelper.class.getClassLoader().getResourceAsStream("dbinfo.properties");
			pp.load(fis);
			url=pp.getProperty("url");
			username=pp.getProperty("username");
			driver=pp.getProperty("driver");
			password=pp.getProperty("password");
			Class.forName(driver);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//finally{
//			try {
//				fis.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		fis=null;
	}
	//得到连接
	public static Connection getConnection(){
		try{
			ct=DriverManager.getConnection(url,username,password);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return ct;
	}
	public static ResultSet executeQuery(String sql,String [] parameters) {
		try {
			ct=getConnection();
			ps=ct.prepareStatement(sql);
			if(parameters!=null&&!parameters.equals("")){
				for(int i=0;i<parameters.length;i++){
					ps.setString(i+1, parameters[i]);
				}
			}
			rs=ps.executeQuery();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
			// TODO: handle exception
		}finally{
			
			
		}
		return rs;
	}
	//如果有多个事务 update/delete/insert [需要考虑事务]
	public static void executeUpdate2(String sql[],String[][] parameters){
		try {
			ct=getConnection();
			ct.setAutoCommit(false);
			for(int i=0;i<sql.length;i++){
				if(parameters[i]!=null){
					ps=ct.prepareStatement(sql[i]);
					for(int j=0;j<parameters[i].length;j++){
						ps.setString(j+1, parameters[i][j]);
					}
					ps.executeUpdate();
				}
			}
			ct.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			try {
				ct.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				
				e1.printStackTrace();
			}
			throw new RuntimeException(e.getMessage());
		
		}finally{
			close(rs,ps,ct);
		}
	}
	public static void executeUpdate(String sql,String[] parameters){
		try {
			ct=getConnection();
			ps=ct.prepareStatement(sql);
			if(parameters!=null){
				for(int i=0;i<parameters.length;i++){
					ps.setString(i+1, parameters[i]);
				}
			}
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
			// TODO: handle exception
		}finally{
			
		}
	}
	public static void close(ResultSet rs,PreparedStatement ps,Connection ct){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			rs=null;
			}
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ps=null;
		}if(ct!=null){
			try {
				ct.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		ct=null;
	}
	public static Connection getCt(){
		return ct;
	}
	public static PreparedStatement getPs(){
		return ps;
	}
	public static ResultSet getRs(){
		return rs;
	}
	
}

以下是dbinfo.properties的内容

url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username= foking
driver= oracle.jdbc.driver.OracleDriver
password= foking

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值