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; } }
本文由用户 openkk 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!