简易的plsql工具

import java.sql.*; import java.io.*; public class DBTools { public static void main(String[] args) throws Exception { Connection con = null; // 1.登录数据库 while ((con = login()) == null) { } // 2.处理用户命令 process(con); // 3.bye-bye try { con.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("再见!"); } static { try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection login() { Connection con = null; String url = prompt("请输入url:"); String user = prompt("请输入用户名:"); String pwd = prompt("请输入密码:"); try { con = DriverManager.getConnection(url, user, pwd); } catch (Exception e) { e.printStackTrace(); } return con; } public static void process(Connection con) { boolean flag = true; String command = ""; while (flag) { command = getCommand(); if ("quit".equals(command)) { flag = false; } else { processSql(con, command); } } } public static String getCommand() { String command = ""; StringBuffer sb = new StringBuffer(); String message = "->"; int i = 0; while (!command.endsWith(";")) { if (i++ > 0) message = i + "->"; sb.append(" " + prompt(message)); command = sb.toString().trim(); } return command.substring(0, command.length() - 1); } public static void processSql(Connection con, String sql) { PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); boolean flag = ps.execute(); int i = 0; if (flag) { rs = ps.getResultSet(); System.out.println("rs->" + rs); } else { i = ps.getUpdateCount(); System.out.println("更新成功-" + i); } } catch (Exception e) { System.out.println("执行SQL失败!"); } finally { JdbcUtil.close(rs, ps, null); } } public static String prompt(String message) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print(message); String command = ""; try { command = in.readLine(); } catch (Exception e) { e.printStackTrace(); } return command; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值