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;
}
}
简易的plsql工具
最新推荐文章于 2025-11-28 23:59:13 发布
363

被折叠的 条评论
为什么被折叠?



