iLeichun

当前位置: 首页 > J2EE

Jdbc操作

分类:J2EE   来源:网络   时间:2010-10-28 23:30:21
  1. package cn.org.jshuwei.j2ee.util;
     

  2.  
  3. import java.sql.Connection;
     
  4. import java.sql.DriverManager;
     
  5. import java.sql.ResultSet;
     
  6. import java.sql.ResultSetMetaData;
     
  7. import java.sql.SQLException;
     
  8. import java.sql.Statement;
     

  9.  
  10. /**
     
  11. *
     
  12. * Jdbc操作的工具类
     
  13. *
     
  14. * @author huwei(jshuwei.org.cn)
     
  15. * @since 1.0
     
  16. *
     
  17. */
     
  18. public class JdbcUtil {
     
  19.         static {
     
  20.                 try {
     
  21.                         Class.forName("oracle.jdbc.driver.OracleDriver");
     
  22.                         Class.forName("com.mysql.jdbc.Driver");
     
  23.                         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
     
  24.                 } catch (ClassNotFoundException e) {
     
  25.                         e.printStackTrace();
     
  26.                 }
     
  27.         }
     

  28.  
  29.         /**
     
  30.          * 得到Connection
     
  31.          *
     
  32.          * @since 1.0
     
  33.          * @param dbType
     
  34.          *            数据库类型(oracle/mysql)
     
  35.          * @return 返回Connection
     
  36.          */
     
  37.         public static Connection getConnection(String dbType) {
     
  38.                 String url = "";
     
  39.                 String user = "";
     
  40.                 String password = "";
     
  41.                 if (dbType.equals("oracle")) {
     
  42.                         url = "jdbc:oracle:thin:@localhost:6666:XE";
     
  43.                         user = "jshuwei";
     
  44.                         password = "123456";
     
  45.                 }
     
  46.                 if (dbType.equals("mysql")) {
     
  47.                         url = "jdbc:mysql://localhost:3306/test";
     
  48.                         user = "jshuwei";
     
  49.                         password = "123456";
     
  50.                 }
     
  51.                 if (dbType.equals("sqlServer")) {
     
  52.                         url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_test";
     
  53.                         user = "jshuwei";
     
  54.                         password = "123456";
     
  55.                 }
     
  56.                 try {
     
  57.                         return DriverManager.getConnection(url, user, password);
     
  58.                 } catch (SQLException e) {
     
  59.                         e.printStackTrace();
     
  60.                 }
     
  61.                 return null;
     
  62.         }
     

  63.  
  64.         /**
     
  65.          * 打印记录集对象
     
  66.          *
     
  67.          * @since 1.0
     
  68.          * @param rs
     
  69.          *            需要打印的记录集对象
     
  70.          */
     
  71.         public static void printRs(ResultSet rs) {
     
  72.                 if (rs == null) {
     
  73.                         System.out.println("ResultSet is null!");
     
  74.                         return;
     
  75.                 }
     
  76.                 try {
     
  77.                         ResultSetMetaData md = rs.getMetaData();
     
  78.                         int cols = md.getColumnCount();
     
  79.                         for (int i = 1; i <= cols; i++) {
     
  80.                                 // 列名,类型编号,类型名称
     
  81.                                 System.out
     
  82.                                                 .println(md.getColumnName(i) + "-->"
     
  83.                                                                 + md.getColumnType(i) + "-->"
     
  84.                                                                 + md.getColumnTypeName(i));
     
  85.                         }
     
  86.                         System.out.println("=========================================");
     
  87.                         while (rs.next()) {
     
  88.                                 for (int i = 1; i <= cols; i++) {
     
  89.                                         System.out.println(md.getColumnName(i) + "="
     
  90.                                                         + rs.getString(i) + " ");
     
  91.                                 }
     
  92.                         }
     
  93.                 } catch (SQLException e) {
     
  94.                         e.printStackTrace();
     
  95.                 }
     
  96.         }
     

  97.  
  98.         /**
     
  99.          * 释放资源
     
  100.          *
     
  101.          * @since 1.0
     
  102.          * @param rs
     
  103.          *            需要释放的记录集对象
     
  104.          * @param stmt
     
  105.          *            需要释放的Statement对象
     
  106.          * @param con
     
  107.          *            需要释放的连接对象
     
  108.          */
     
  109.         public static void release(ResultSet rs, Statement stmt, Connection con) {
     
  110.                 if (rs != null)
     
  111.                         try {
     
  112.                                 rs.close();
     
  113.                         } catch (Exception e) {
     
  114.                         }
     
  115.                 if (stmt != null)
     
  116.                         try {
     
  117.                                 stmt.close();
     
  118.                         } catch (Exception e) {
     
  119.                         }
     
  120.                 if (con != null)
     
  121.                         try {
     
  122.                                 con.close();
     
  123.                         } catch (Exception e) {
     
  124.                         }
     
  125.         }
     

  126.  
  127.         /**
     
  128.          * 释放资源
     
  129.          *
     
  130.          * @since 1.0
     
  131.          * @param o
     
  132.          *            需要释放的对象
     
  133.          */
     
  134.         public static void release(Object o) {
     
  135.                 try {
     
  136.                         if (o instanceof ResultSet) {
     
  137.                                 ((ResultSet) o).close();
     
  138.                         } else if (o instanceof Statement) {
     
  139.                                 ((Statement) o).close();
     
  140.                         } else if (o instanceof Connection) {
     
  141.                                 ((Connection) o).close();
     
  142.                         }
     
  143.                 } catch (SQLException e) {
     
  144.                         e.printStackTrace();
     
  145.                 }
     
  146.         }
     
  147. }
更多