超强sqlhelper

来源:互联网 发布:网络剧宣传发行方案 编辑:程序博客网 时间:2024/04/28 00:12
package com.zl.sql;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.concurrent.Callable;


public class SqlHelper {
//定义需要的变量
private static Connection ct=null;
 public static Connection getCt() {
return ct;
}
public static PreparedStatement getPs() {
return ps;
}
public static ResultSet getRs() {
return rs;
}
// 在大多数情况下用PreparedStatement代替Satement防止注入漏洞
 private static PreparedStatement ps=null;
 //private static Statement st=null;
 private static ResultSet rs=null;
 private static CallableStatement cs=null;
 //连接数据库参数
 private static String url="";
 private static String username="";
 private static String driver="";
 private static String password="";
 private static Properties pp=null;
 private static FileInputStream fis=null;
 //加载驱动,只需要一次
 static
 {
try {
//从dbinfo.properties读取配置信息
pp=new Properties();
fis=new FileInputStream("dbinfo.properties");
pp.load(fis);
url=pp.getProperty(url);
username=pp.getProperty(username);
driver=pp.getProperty(driver);
password=pp.getProperty(password);
Class.forName(url);
 
} 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 (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ct;
 } //调用存储过程有返回值Resultset
 //sql call 过程(?,?,?)
 public static CallableStatement callPro2(String sql,String[] inparemeters,Integer[] outparemeters)
 {
try {
ct=getConnection();
    cs=ct.prepareCall(sql);
    if(inparemeters!=null)
    {
    for(int i=0;i<inparemeters.length;i++)
    {
    cs.setObject(i+1, inparemeters[i]);
    }
    }
   //给out参数赋值
    if(outparemeters!=null)
    {
    for(int i=0;i<outparemeters.length;i++)
    {
    cs.registerOutParameter(inparemeters.length+1+i, outparemeters[i]);
    }
    }
    cs.execute();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}finally
{
//暂时不关闭
}
return cs;
 }
 
 //调用存储过程
 //sql 象{call 过程(?,?,?)}
 public static void callPro1(String sql,String[] paremeters)
 {
try {
ct=getConnection();
cs=ct.prepareCall(sql);
//?赋值
if(paremeters!=null)
{
for(int i=0;i<paremeters.length;i++)
{
cs.setObject(i+1, paremeters[i]);
}
}
cs.execute();

} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException(e.getMessage());
}finally
{
close(rs,cs,ct);
}
 }
 //统一的select
 public static ResultSet executrQuery(String sql,String[] paremeters)
 {
try {
ct=getConnection();
ps=ct.prepareStatement(sql);
if(paremeters!=null&&!paremeters.equals(""))
{
for(int i=0;i<paremeters.length;i++)
{
ps.setString(i+1, paremeters[i]);
}
}
rs=ps.executeQuery();

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
//抛出 运行异常,可以给调用函数的函数一个选择,也可以处理也可以放弃处理
throw new RuntimeException(e.getMessage());
}finally
{
//关闭资源
//close(rs,ps,ct);
}
return rs;
 }
 
 //如果有多个 update /delete /insert(需要考虑到事务)
 public static void excuteUpdate2(String[] sql,String[][] paremeters )
 {
try {
//核心
//1获得连接
ct=getConnection();
//因为这是用户传入的可能是多个sql语句
ct.setAutoCommit(false);
for(int i=0;i<sql.length;i++)
{if(paremeters[i]!=null)
{
ps=ct.prepareStatement(sql[i]);
for(int j=0;j<paremeters[i].length;j++)
{
ps.setString(j+1, paremeters[i][j]);
}
ps.executeUpdate();
}
 
}
ct.commit();
 
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
//抛出 运行异常,可以给调用函数的函数一个选择,也可以处理也可以放弃处理
throw new RuntimeException(e.getMessage());
}finally
{
//关闭资源
close(rs,ps,ct);
}
 }
//先写一个 update /delete /insert
 //sql格式:update 表名 set 字段名=?where 字段名=?;
 //paremeter应该是{"abc",23}
 public void excuteUpdate(String sql,String[] paremeters )
 {
//创建ps
try {
ct=getConnection();
ps=ct.prepareStatement(sql);
//给?赋值
if(paremeters!=null)
{
for(int i=0;i<paremeters.length;i++)
{
ps.setString(i+1, paremeters[i]);
}
}
   ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//抛出 运行异常,可以给调用函数的函数一个选择,也可以处理也可以放弃处理
throw new RuntimeException(e.getMessage());

}finally
{
//关闭资源
close(rs,ps,ct);
}
 }
 //关闭资源的函数
 public static void close(ResultSet rs,Statement ps,Connection ct)
 {
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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();
}
}
 }

}
原创粉丝点击