SqlHelper

 

java操作数据库小工具

DBUtil类,主要是操作数据连接池

  1. package com.tig.util;  
  2.   
  3. import java.io.InputStream;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.sql.Statement;  
  9. import java.util.Properties;  
  10.   
  11. public class DBUtil {  
  12.       
  13.     //定义链接所需要的变量  
  14.     private static Connection con = null;  
  15.     private static PreparedStatement ps = null;  
  16.     private static ResultSet rs = null;  
  17.       
  18.     //定义链接数据库所需要的参数  
  19.     private static String url = "";  
  20.     private static String username = "";  
  21.     private static String driver="";  
  22.     private static String password="";  
  23.       
  24.     //定义读取配置文件所需要的变量  
  25.     private static Properties pp = null;  
  26.     private static InputStream fis = null;  
  27.       
  28.     /** 
  29.      * 加载驱动 
  30.      */  
  31.     static {  
  32.         try {  
  33.             //从dbinfo.properties配置文件中读取配置信息  
  34.             pp = new Properties();  
  35.             fis = DBUtil.class.getClassLoader().getResourceAsStream("com/tig/util/dbinfo.properties");  
  36.               
  37.             pp.load(fis);  
  38.             url = pp.getProperty("url");  
  39.             username = pp.getProperty("username");  
  40.             driver=pp.getProperty("driver");  
  41.             password=pp.getProperty("password");  
  42.               
  43.             //加载驱动  
  44.             Class.forName(driver);  
  45.               
  46.         } catch (Exception e) {  
  47.             System.out.println("驱动加载失败!");  
  48.             e.printStackTrace();  
  49.         } finally {  
  50.             try {  
  51.                 fis.close();  
  52.             } catch (Exception e) {  
  53.                 e.printStackTrace();  
  54.             }  
  55.               
  56.             fis = null; //垃圾回收自动处理  
  57.         }  
  58.           
  59.     }  
  60.       
  61.     /** 
  62.      * 得到Connection链接 
  63.      * @return Connection 
  64.      */  
  65.     public static Connection getConnection() {  
  66.           
  67.         try {  
  68.             //建立连接  
  69.             con = DriverManager.getConnection(url, username, password);  
  70.               
  71.         } catch (Exception e) {  
  72.             System.out.println("数据库链接失败!");  
  73.             e.printStackTrace();  
  74.         }  
  75.           
  76.         return con;  
  77.     }  
  78.       
  79.     /** 
  80.      * 统一的资源关闭函数 
  81.      * @param rs 
  82.      * @param ps 
  83.      * @param ct 
  84.      */  
  85.     public static void close(ResultSet rs,Statement ps, Connection con){  
  86.           
  87.         if(rs != null) {  
  88.             try {  
  89.                 rs.close();  
  90.             } catch (Exception e) {  
  91.                 e.printStackTrace();  
  92.             }  
  93.         }  
  94.         if(ps != null) {  
  95.             try {  
  96.                 ps.close();  
  97.             } catch (Exception e) {  
  98.                 e.printStackTrace();  
  99.             }  
  100.         }  
  101.         if(con != null) {  
  102.             try {  
  103.                 con.close();  
  104.             } catch (Exception e) {  
  105.                 e.printStackTrace();  
  106.             }  
  107.         }  
  108.     }  
  109.       
  110. }  

SqlHelper类

  1. package com.tig.util;  
  2.   
  3. import java.sql.CallableStatement;  
  4. import java.sql.Connection;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.ResultSetMetaData;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. public class SqlHelper {  
  12.       
  13.     private static Connection con = null;  
  14.       
  15.     private static PreparedStatement ps = null;  
  16.       
  17.     private static ResultSet rs = null;  
  18.       
  19.     private static CallableStatement cs = null;  
  20.       
  21.       
  22.     /** 
  23.      * 提供查询方法 
  24.      * @param sql sql语句 
  25.      * @param parameters 给问号赋值的参数组 
  26.      * @return {@link ArrayList} 
  27.      */  
  28.     public static ArrayList executeQuery(String sql, String[] parameters) {  
  29.           
  30.         ArrayList al = new ArrayList();  
  31.           
  32.         try {  
  33.             con = DBUtil.getConnection();  
  34.             ps = con.prepareStatement(sql);  
  35.               
  36.             //给sql语句中的问号赋值  
  37.             if (parameters != null) {  
  38.                 for (int i = 0; i < parameters.length; i++) {  
  39.                     ps.setObject(i+1, parameters[i]);  
  40.                 }  
  41.             }  
  42.               
  43.             rs = ps.executeQuery();  
  44.               
  45.             //得到结果集(rs)的结构  
  46.             ResultSetMetaData rsmd = rs.getMetaData();  
  47.               
  48.             //通过rsmd可以得到该结果集有多少列  
  49.             int columnNum = rsmd.getColumnCount();  
  50.   
  51.             //从rs中取出数据,并且封装到ArrayList中  
  52.             while (rs.next()) {  
  53.                   
  54.                 Object []objects = new Object[columnNum];  
  55.                 for(int i = 0; i < objects.length; i++) {  
  56.                     objects[i] = rs.getObject(i + 1);  
  57.                 }  
  58.                   
  59.                 al.add(objects);  
  60.             }  
  61.               
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.         } finally {  
  65.             DBUtil.close(rs, ps, con);  
  66.         }  
  67.           
  68.         return al;  
  69.     }  
  70.       
  71.     /** 
  72.      * 提供统一的插入/删除/更新方法 
  73.      * @param sql sql语句 
  74.      * @param parameteres 给问号赋值的参数组 
  75.      * @return 
  76.      */  
  77.     public static boolean executeUpdate(String sql,String[] parameteres) {  
  78.           
  79.         boolean success = false;  
  80.           
  81.         try {  
  82.               
  83.             con = DBUtil.getConnection();  
  84.             ps = con.prepareStatement(sql);  
  85.               
  86.             //给问号赋值  
  87.             if (parameteres != null) {  
  88.                 for (int i = 0; i < parameteres.length; i++) {  
  89.                     ps.setString(i + 1, parameteres[i]);  
  90.                 }  
  91.             }  
  92.               
  93.             //执行动作,如果返回“1” 则为操作成功  
  94.             if (ps.executeUpdate() == 1) {  
  95.                 success = true;  
  96.             }  
  97.               
  98.         } catch (Exception e) {  
  99.             e.printStackTrace();  
  100.         } finally {  
  101.             DBUtil.close(rs, ps, con);  
  102.         }  
  103.           
  104.         return success;  
  105.     }  
  106.       
  107.     /** 
  108.      * 提供统一的插入/删除/更新方法[需要考虑事物] 
  109.      * @param sql 
  110.      * @param parameters 
  111.      */  
  112.     public static void executeUpdate(String sql[], String[][] parameters){  
  113.           
  114.         try {  
  115.             con = DBUtil.getConnection();  
  116.               
  117.             //sql命令的提交由应用程序负责,程序必须调用commit或者rollback方法  
  118.             con.setAutoCommit(false);  
  119.               
  120.             for (int i = 0; i < sql.length; i++) {  
  121.                 if (parameters[i] != null) {  
  122.                     ps = con.prepareStatement(sql[i]);  
  123.                     for (int j = 0; j < parameters[i].length; i++){  
  124.                         ps.setString(j + 1, parameters[i][j]);  
  125.                     }  
  126.                     ps.executeUpdate();  
  127.                 }  
  128.             }  
  129.             con.commit();  
  130.               
  131.         } catch (Exception e) {  
  132.             e.printStackTrace();  
  133.               
  134.             //回滚操作  
  135.             try {  
  136.                 con.rollback();  
  137.             } catch (Exception ex) {  
  138.                 ex.printStackTrace();  
  139.             }  
  140.             throw new RuntimeException(e.getMessage());  
  141.               
  142.         } finally {  
  143.             DBUtil.close(rs, ps, con);  
  144.         }  
  145.     }  
  146.       
  147. }  

dbinfo.properties 配置文件.主要配置一下数据库的名字,用户名,密码

  1. url=jdbc:mysql://localhost:3306/studentinfo  
  2. username=root  
  3. driver=com.mysql.jdbc.Driver  
  4. password=1234 
原文地址:https://www.cnblogs.com/wbs19950305/p/8654997.html