使用以下的包装类都需要自己有JDBC的驱动jar包:

如 mysql-connector-java-5.1.26-bin.jar(5.1.26代表的是版本序列号)

 

一、JDBC的封装:(java连接MySQL)

  1 import java.sql.*;
  2 
  3 import utils.GlobalConstant;
  4 
  5 public class JDBCUtil {
  6 
  7     private JDBCUtil() {
  8 
  9     }
 10 
 11     /**
 12      * 获取JDBC的数据库连接
 13      */
 14     public static Connection getConnection() {
 15         Connection conn = null;
 16         try {
 17             Class.forName(GlobalConstant.CONNECTION_DRIVER);
 18             conn = DriverManager.getConnection(GlobalConstant.CONNECTION_URL, GlobalConstant.CONNECTION_USER,
 19                     GlobalConstant.CONNECTION_PASSWORD);
 20         } catch (Exception e) {
 21             e.printStackTrace();
 22         }
 23         /*
 24          * try { conn = DataSourceService.getConnection(); } catch (SQLException
 25          * e) { e.printStackTrace(); }
 26          */
 27         return conn;
 28     }
 29 
 30     private static void close(ResultSet rs, Statement sm, Connection conn) {
 31         try {
 32             if (rs != null)
 33                 rs.close();
 34             if (sm != null)
 35                 sm.close();
 36             if (conn != null)
 37                 conn.close();
 38         } catch (Exception e) {
 39             e.printStackTrace();
 40         }
 41     }
 42 
 43     /**
 44      * 关闭数据库的结果集
 45      * 
 46      * @param rs
 47      *            需要关闭的结果集
 48      */
 49     public static void close(ResultSet rs) {
 50         try {
 51             if (rs != null)
 52                 rs.close();
 53         } catch (Exception e) {
 54             e.printStackTrace();
 55         }
 56     }
 57 
 58     /**
 59      * 将一个标准的sql查询语句在数据库中查询,并且返回一个对应的结果集。
 60      * 
 61      * @param sql
 62      *            标准的sql查询语句
 63      */
 64     public static ResultSet doQuery(String sql) {
 65         Connection conn = getConnection();
 66         if (conn == null)
 67             return null;
 68         Statement sm = null;
 69         ResultSet rs = null;
 70         try {
 71             sm = conn.createStatement();
 72             rs = sm.executeQuery(sql);
 73             return rs;
 74         } catch (SQLException e) {
 75             e.printStackTrace();
 76         } finally {
 77             if (rs == null && sm != null) {
 78                 close(null, sm, conn);
 79             }
 80             // close(null, sm, conn);这句肯定不行
 81         }
 82         return rs;
 83     }
 84 
 85     /**
 86      * 将一个更新语句和对应的参数进行查询,并返回一个整形结果
 87      * 
 88      * @param sql
 89      *            数据库中的更新语句
 90      * @param paras
 91      *            sql中对应的参数
 92      */
 93     public static int doUpdate(String sql, String[] paras) {
 94         Connection conn = getConnection();
 95         if (conn == null)
 96             return 0;
 97         PreparedStatement psm = null;
 98         int result = 0;
 99         try {
100             psm = conn.prepareStatement(sql);
101             for (int i = 0; i < paras.length; i++) {
102                 psm.setString(i + 1, paras[i]);
103             }
104             result = psm.executeUpdate();
105         } catch (Exception e) {
106             e.printStackTrace();
107         } finally {
108             close(null, psm, conn);
109         }
110         return result;
111     }
112 }
JDBCUtils

相关文章: