sqlHelper类及其配置文件

package com.zz.utils;

import java.beans.Statement;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;



public class sqlHelper {
//定义需要的变量
	
	private static Connection ct = null; // 声明Statement对象的实例
	private static ResultSet rs = null; // 声明ResultSet对象的实例
	private static PreparedStatement ps=null;
	private static CallableStatement cs=null;
	
	private static String dbClassName ="";//定义保存数据库驱动的变量
	private static String dbUrl ="";
	private static String dbUser ="";
	private static String dbPwd = "";
	
	private static Properties  pp= null; // 创建并实例化Properties对象的实例
	private static FileInputStream fis=null;
	//加载驱动
	static {
		try {
			//从conneDB.properties中读取配置信息
			pp=new Properties();
			fis=new FileInputStream("connDB.properties");
			pp.load(fis);
			dbClassName = pp.getProperty("dbClassName"); // 获取数据库驱动
			dbUrl = pp.getProperty("dbUrl"); // 获取URL
			dbUser = pp.getProperty("dbUser"); // 获取登录用户
			dbPwd = pp.getProperty("dbPwd"); // 获取密码
			Class.forName("org.gjt.mm.mysql.Driver");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//得到链接
	public static Connection getConnection() {
		try {
			ct=DriverManager.getConnection(dbUrl, dbUser, dbPwd);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ct;
	}
	//先写一个update/delete /insert
	/*
	 * 功能:执行查询语句
	 */
	//sql格式 :update表名 set字段名=?where字段=?
	//paramaters应该是{“abc”,22}
		public static void excuteUpdate(String sql ,String[] parameters) {
			try { // 捕捉异常
				ct = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
				ps=ct.prepareStatement(sql);
				//给问号赋值
				if(parameters!=null){
					for(int i=0;i<parameters.length;i++){
						ps.setString(i+1, parameters[i]);
					}
				}
				//执行
				ps.executeUpdate();
			
			} catch (SQLException ex) {
				System.err.println(ex.getMessage()); // 输出异常信息
				throw new RuntimeException(ex.getMessage());//
			}finally{
				close(rs,  ps, ct);
			}
			
		}
		//分页过程?????
		
		//调用有返回值的存储过程
		//sql call 过程(???)
		public static ResultSet  callProl2(String sql,String []inparameters ,Integer [] outparameters){
			try {
				ct=getConnection();
				cs=ct.prepareCall(sql);
				//给问号赋值
				if(inparameters!=null){
					for (int i=0;i<inparameters.length;i++){
						cs.setObject(i+1, inparameters[i]);
					}
				}
				if(outparameters!=null){
					for (int i=0;i<outparameters.length;i++){
						cs.registerOutParameter(inparameters.length+1+i, outparameters[i]);
					}
				}
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}finally{
				//不需要关闭
			}
			return null;
		}
		//调用无返回值存储过程{call 过程(???)}
		public static void callProl(String sql,String []parameters){
			try {
				ct=getConnection();
				cs=ct.prepareCall(sql);
				//给问号赋值
				if(parameters!=null){
					for (int i=0;i<parameters.length;i++){
						cs.setObject(i+1, parameters[i]);
					}
				}
				cs.execute();
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}finally{
				close(rs, cs, ct);
			}
		}
		//统一 的select
		public static ResultSet executeQuery(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]);
					}
					
				}
                                rs=ps.executeQuery();} catch (Exception e) {
				System.err.println(e.getMessage()); // 输出异常信息
				throw new RuntimeException(e.getMessage());//
			}
			return rs;
		}
		//如果有多个update、delete、insert
		public static void excuteUpdate2(String sql[] ,String[][] parameters) {
			try {
				//核心
				//1、获得链接
				ct=getConnection();
				//用户可能创建多个sql语句
				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) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
				// TODO: handle exception
			}
			
		}
		
		/*
		 * 功能:关闭数据库的连接
		 */
		public static void close(ResultSet rs,PreparedStatement ps,Connection ct) {
			try { // 捕捉异常
				if (rs != null) { // 当ResultSet对象的实例rs不为空时
					rs.close(); // 关闭ResultSet对象
				}
				if (ps != null) { // 当Statement对象的实例stmt不为空时
				ps.close();
				}
				if (ct != null) { // 当Connection对象的实例conn不为空时
					ct.close(); // 关闭Connection对象
				}
			} catch (Exception e) {
				e.printStackTrace(System.err); // 输出异常信息
			}
		}
}
配置文件connDB.properties如下
dbClassName=org.gjt.mm.mysql.Driver
dbUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK
dbUser=root
dbPwd=admin

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值