1. 程式人生 > >一個大牛寫的JDBC工具類

一個大牛寫的JDBC工具類

事情過去好久了...大概三年前

記著那個時候,此大牛(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();
	  }
	}
}


另外一個類:

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;
	}
}