sqlplus.jar命令行下连接Mysql、SQLServer、Oracle小工具
使用了一个btc-ascii-table小工具,可以像mysql客户端一样方便的管理数据库。自带mysql、sqlserver2000、2005+、oracle数据库驱动,可以在命令行下直连这几种数据库。连接的时候需要自行指定对应的数据库的驱动URL哦,百度下jdbc或者参考我以前的文章。
交互shell方式:
直接执行命令方式:
java -jar sqlplus.jar "com.mysql.jdbc.Driver" "jdbc:mysql://localhost:3306/mysql" "root" "c123456" "select version()"
import java.io.BufferedReader; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import com.bethecoder.ascii_table.ASCIITable; import com.bethecoder.ascii_table.impl.JDBCASCIITableAware; import com.bethecoder.ascii_table.spec.IASCIITableAware; public class SqlPlus { private static Connection conn = null; public static void getConnection(String driver,String url,String user,String pass) { try { Class.forName(driver); conn = DriverManager.getConnection(url,user,pass); } catch (Exception e) { e.printStackTrace(); } } public static void executeSQL(String sql) { try { if(!sql.isEmpty()){ try { IASCIITableAware asciiTableAware = new JDBCASCIITableAware(conn, sql); ASCIITable.getInstance().printTable(asciiTableAware); } catch (Exception e) { System.out.println(e.toString()); } } System.out.print(conn.getMetaData().getDatabaseProductName()+"> "); String str = new BufferedReader(new InputStreamReader(System.in)).readLine(); if(!"exit".equalsIgnoreCase(str)){ executeSQL(str); }else{ return; } } catch (Exception e) { System.out.println(e.toString()); } } public static void main(String[] args) { try { if(args.length == 5){ getConnection(args[0],args[1],args[2],args[3]); IASCIITableAware asciiTableAware = new JDBCASCIITableAware(conn, args[4]); ASCIITable.getInstance().printTable(asciiTableAware); }else{ System.out.print("Driver:"); String driver = new BufferedReader(new InputStreamReader(System.in)).readLine(); System.out.print("URL:"); String url = new BufferedReader(new InputStreamReader(System.in)).readLine(); System.out.print("username:"); String user = new BufferedReader(new InputStreamReader(System.in)).readLine(); System.out.print("password:"); String passwd = new BufferedReader(new InputStreamReader(System.in)).readLine(); getConnection(driver,url,user,passwd); System.out.print("\r\n"+conn.getMetaData().getDatabaseProductName()+"> "); String sql = new BufferedReader(new InputStreamReader(System.in)).readLine(); if(conn != null){ executeSQL(sql); } } } catch (Exception e) { e.printStackTrace(); } } }
下载地址:sqlplus.jar