SqlHelper工具类

来源:互联网 发布:matlab置乱函数算法 编辑:程序博客网 时间:2024/05/14 06:27
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 blocke.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 exceptione.printStackTrace();try {ct.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.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 blocke.printStackTrace();}rs=null;}if(ps!=null){try {ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}ps=null;}if(ct!=null){try {ct.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.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

原创粉丝点击