日期:2014-05-16 浏览次数:20470 次
事情过去好久了...大概三年前
记着那个时候,此大牛(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 co