sql helper 连接mysql_JDBC操作mysql数据库(SqlHelper类封装)

packagecom.mqs.process;importjava.io.FileInputStream;import java.sql.*;importjava.util.Properties;public classSqlHelper {//定义需要的变量

private static Connection ct=null;//在大多数情况下,使用PreparedStatement来替代Statement//这样可以防止sql注入

private static PreparedStatement ps=null;private static ResultSet rs=null;private static CallableStatement cs=null;public staticConnection getCt() {returnct;

}public staticPreparedStatement getPs() {returnps;

}public staticResultSet getRs() {returnrs;

}//连接数据库参数

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.propertis文件中读取配置信息

pp = newProperties();

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("driver");

}catch(Exception e) {//TODO: handle exception

}finally{try{

fis.close();

}catch(Exception e) {

e.printStackTrace();

}

fis=null;

}

}//得到连接

public staticConnection getConnection(){try{

ct=DriverManager.getConnection(url,username,password);

}catch(Exception e) {//TODO: handle exception

}returnct;

}//统一的select

public staticResultSet executeQuery(String sql,String parameters[]){try{

ct=getConnection();

ps=ct.prepareStatement(sql);if(parameters!=null&&!parameters.equals("")){for(int i=0;i

ps.setString(i+1,parameters[i]);

}

}

rs=ps.executeQuery();

}catch(Exception e) {

e.printStackTrace();throw newRuntimeException(e.getMessage());

}finally{//close(rs,ps,ct);

}returnrs;

}//先写一个update/insert/delete

public static voidexecuteUpdate(String sql,String [] parameters){//1、创建一个ps

try{

ct=getConnection();

ps=ct.prepareStatement(sql);//给?赋值

if(parameters!=null){for(int i=0;i

ps.setString(i+1,parameters[i]);

}

}//执行

ps.executeUpdate();

}catch(Exception e){

e.printStackTrace();//开发阶段//抛出异常,抛出运行异常,可以给调用该函数的函数一个选择//可以处理,也可以放弃处理

throw newRuntimeException(e.getMessage());

}finally{//关闭资源

close(rs,ps,ct);

}

}//如果有多个update/delete/insert[需要考虑事务]

public static voidexecuteUpdate2(String sql[],String [][]parameters){try{//1、获得链接

ct=getConnection();//因为这时,用户传入的可能是多个sql语句

ct.setAutoCommit(false);for(int i=0;i

ps=ct.prepareStatement(sql[i]);for(int j=0;j

ps.setString(j+1, parameters[i][j]);

}

ps.executeUpdate();

}

}

ct.commit();

}catch(Exception e) {

e.printStackTrace();//开发阶段//回滚

try{

ct.rollback();

}catch(Exception e1) {

e1.printStackTrace();

}//抛出异常,抛出运行异常,可以给调用该函数的函数一个选择//可以处理,也可以放弃处理

throw newRuntimeException(e.getMessage());

}finally{

close(rs,ps,ct);

}

}//分页问题

public staticResultSet executeQuery2(){return null;

}//调用存储过程//sql 像{call 过程(?,?,?)}

public static voidcallPro1(String sql,String []parameters){try{

ct=getConnection();

cs=ct.prepareCall(sql);//?号赋值

if(parameters!=null){for(int i=0;i

cs.setObject(i+1, parameters[i]);

}

}

cs.execute();

}catch(Exception e) {

e.printStackTrace();throw newRuntimeException(e.getMessage());

}finally{

close(rs,cs,ct);

}

}//调用存储过程,有返回Result//未完成,见《韩顺平.2011最新j2ee视频教程.jdbc第4讲.超强SqlHelper工具类封装(2)》

public staticResultSet callPro2(String sql,String []inparameters,String []outparameters){try{

ct=getConnection();

cs=ct.prepareCall(sql);if(inparameters!=null){for(int i=0;i

cs.setObject(i+1,inparameters[i]);

}

}

}catch(Exception e) {//TODO: handle exception

}finally{

}return null;

}//关闭资源的函数

public static voidclose(ResultSet rs,Statement ps,Connection ct){if(rs!=null){try{

rs.close();

}catch(Exception e) {

e.printStackTrace();

}

rs=null;

}if(ps!=null){try{

ps.close();

}catch(Exception e) {

e.printStackTrace();

}

ps=null;

}if(ct!=null){try{

ct.close();

}catch(Exception e) {

e.printStackTrace();

}

ct=null;

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值