1 4 package smallsql.tools; 5 6 import java.io.*; 7 import java.sql.*; 8 import java.util.Properties ; 9 10 import javax.swing.JOptionPane ; 11 12 import smallsql.database.*; 13 14 15 18 public class CommandLine { 19 20 21 public static void main(String [] args) throws Exception { 22 System.out.println("SmallSQL Database command line tool\n"); 23 Connection con = new SSDriver().connect("jdbc:smallsql", new Properties ()); 24 Statement st = con.createStatement(); 25 if(args.length>0){ 26 con.setCatalog(args[0]); 27 } 28 System.out.println("\tVersion: "+con.getMetaData().getDatabaseProductVersion()); 29 System.out.println("\tCurrent database: "+con.getCatalog()); 30 System.out.println(); 31 System.out.println("\tUse the USE command to change the database context."); 32 System.out.println("\tType 2 times ENTER to execute any SQL command."); 33 34 StringBuffer command = new StringBuffer (); 35 BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 36 while(true){ 37 try { 38 String line; 39 try{ 40 line = input.readLine(); 41 }catch(IOException ex){ 42 ex.printStackTrace(); 43 JOptionPane.showMessageDialog( null, "You need to start the command line of the \nSmallSQL Database with a console window:\n\n java -jar smallsql.jar\n\n" + ex, "Fatal Error", JOptionPane.OK_OPTION); 44 return; 45 } 46 if(line == null){ 47 return; } 49 if(line.length() == 0 && command.length() > 0){ 50 boolean isRS = st.execute(command.toString()); 51 if(isRS){ 52 printRS(st.getResultSet()); 53 } 54 command.setLength(0); 55 } 56 command.append(line).append('\n'); 57 } catch (Exception e) { 58 command.setLength(0); 59 e.printStackTrace(); 60 } 61 } 62 63 } 64 65 66 private static void printRS(ResultSet rs) throws SQLException { 67 ResultSetMetaData md = rs.getMetaData(); 68 int count = md.getColumnCount(); 69 for(int i=1; i<=count; i++){ 70 System.out.print(md.getColumnLabel(i)); 71 System.out.print('\t'); 72 } 73 System.out.println(); 74 while(rs.next()){ 75 for(int i=1; i<=count; i++){ 76 System.out.print(rs.getObject(i)); 77 System.out.print('\t'); 78 } 79 System.out.println(); 80 } 81 } 82 } 83 | Popular Tags |