JDBC 工具類

openkk 12年前發布 | 36K 次閱讀 JDBC Java開發

(IBM某架構),在UE下一氣呵成,沒有任何多余代碼,甚至不用注釋都可以看得明白此段代碼用途,現在仔細看了,雖然很基本,但是仍舊覺著此段代碼很爽!貼出來,讓大家有時間的話,都大致看一下...

package com.yinhai.util;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement;

public class JdbcUtil { static{ String driver = "oracle.jdbc.driver.OracleDriver"; try{ Class.forName(driver); }catch(Exception e){ e.printStackTrace(); } } public static Connection getConnection(){ String url = "jdbc:oracle:thin:@192.192.192.239:1521:orcl"; String usr = "xajgyl"; String pwd = "xajgyl"; Connection con = null; try{ con = DriverManager.getConnection(url,usr,pwd); }catch(Exception e){ e.printStackTrace(); } return con; } public static void close(ResultSet rs, Statement stmt,Connection con){ try{ if(rs!=null) rs.close(); }catch(Exception ex){ ex.printStackTrace(); } try{ if(stmt!=null) stmt.close(); }catch(Exception ex){ ex.printStackTrace(); } try{ if(con!=null) con.close(); }catch(Exception ex){ ex.printStackTrace(); } }

public static void printRs(ResultSet rs){
  try{
    StringBuffer sb = new StringBuffer();
    ResultSetMetaData meta = rs.getMetaData();
    int cols = meta.getColumnCount();
    while(rs.next()){
      for(int i=1;i<=cols;i++){
        sb.append(meta.getColumnName(i)+"->");
        sb.append(rs.getString(i)+"  ");
      }
      sb.append("\n");
    }
    System.out.print(sb.toString());
  }catch(Exception e){
    e.printStackTrace();
  }
}

}</pre>


另外一個類:

 

package com.yinhai.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SQLTools
{
    public static void main(String[] args){
      Connection con = null;
      while((con=getConnection())==null){};
      try{
        con.setAutoCommit(false);
      }catch(Exception e){
        System.out.println(e.getMessage());
      }
      handleCommand(con);
      JdbcUtil.close(null,null,con);
      System.out.println("再見!");
    }

    private static void handleCommand(Connection con){
      String command = "";
      boolean flag = true;
      while(flag){
          command = getCommand();
          if("quit".equals(command)){
            flag = false;
          }else if("commit".equals(command) || "rollback".equals(command)){
            handleCommit(con,command);
          }else{
            handleSQL(con,command);
          }
    }
    }

    private static Connection getConnection(){
      Connection con = null;
      String url = "";
      String usr = "";
      String pwd = "";
      url = prompt("請輸入URL:");
    usr = prompt("請輸入用戶名:");
      pwd = prompt("請輸入密碼:");
      try{
        con = DriverManager.getConnection(url,usr,pwd);
      }catch(Exception e){
        System.out.println("連接錯誤:"+e.getMessage());
      }
      return con;
    }

    private static String getCommand(){
      StringBuffer sb = new StringBuffer();
      String command = "";
      String message = "SQL->";
    boolean flag = true;
      int c = 0;
      while(flag){
          if(c++!=0) message = c+"->";
        sb.append(prompt(message)+" ");
          command = sb.toString().trim();
          if(command.endsWith(";")){
            flag = false;
          }
      }
      return command.substring(0,command.length()-1).trim();
    }

    private static void handleCommit(Connection con,String command){
      try{
        if("commit".equals(command)){
            con.commit();
          }else{
            con.rollback();
          }
      }catch(Exception e){
        System.out.println("提交/回滾失敗:"+e.getMessage());
      }
    }

    private static void handleSQL(Connection con ,String command){
      PreparedStatement ps = null;
      ResultSet rs = null;
      try{
        ps = con.prepareStatement(command);
          if(ps.execute()){
            rs = ps.getResultSet();
            JdbcUtil.printRs(rs);
          }else{
            System.out.println("更新成功:"+ps.getUpdateCount()+" .");
          }
      }catch(Exception e){
        System.out.println("數據操作失敗:"+e.getMessage());
        try{
            if(con!=null)con.rollback();
          }catch(Exception ex){
            ex.printStackTrace();
          }
      }
    }

    private static String prompt(String message){
       BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
       System.out.print(message);
       String input = "";
       try{
         input = in.readLine();
       }catch(Exception e){
         System.out.println("IO錯誤:"+e.getMessage());
       }
       return input;
    }
}

 

 

 

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!