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