001: import java.net.*;
002: import java.sql.*;
003: import java.io.*;
004: import java.util.*;
005: 
006: class ExecSQL
007: {
008:    /**
009:       Executes all SQL statements in a file
010:       @param args 
011:       <ul>
012:       <li>args[0]: the property file for the database connection</li>
013:       <li>args[1]: the file with SQL statements</li>
014:       </ul>
015:     */
016:    public static void main (String args[])
017:    {   
018:       try
019:       {  
020:          if (args.length == 0)
021:          {   
022:             System.out.println
023:                ("Usage: ExecSQL propertyFile [statementFile]");
024:             System.exit(0);
025:          }
026:          Connection con = getConnection(args[0]);
027:          Statement stmt = con.createStatement();
028:          
029: 
030:          String tableName = "";
031: 
032:          Reader reader;
033:          if (args.length > 1) 
034:             reader = new FileReader(args[1]);
035:          else
036:             reader = new InputStreamReader(System.in);
037:          BufferedReader in = new BufferedReader(reader);
038: 
039: 
040:          String line;
041:          while ((line = in.readLine()) != null)
042:          {
043:             boolean hasResultSet = stmt.execute(line);
044:             if (hasResultSet)
045:                showResultSet(stmt);
046:          }
047: 
048:          in.close();
049:          stmt.close();
050:          con.close();
051:       }
052:       catch (SQLException ex)
053:       {  
054:          System.out.println ("SQLException:");
055:          while (ex != null)
056:          {  
057:             System.out.println ("SQLState: "
058:                + ex.getSQLState());
059:             System.out.println ("Message:  "
060:                + ex.getMessage());
061:             System.out.println ("Vendor:   "
062:                + ex.getErrorCode());
063:             ex = ex.getNextException();
064:             System.out.println ("");
065:          }
066:       }
067:       catch (IOException ex)
068:       {  
069:          System.out.println("IOException: " + ex);
070:          ex.printStackTrace ();
071:       }
072:    }
073: 
074:    /**
075:       Opens a database connection
076:       @param fileName the name of the property file that contains the
077:       database driver, url, username and password
078:       @return the connection to the database
079:     */
080:    public static Connection getConnection(String fileName)
081:       throws SQLException, IOException
082:    {  
083:       Properties props = new Properties();
084:       FileInputStream in = new FileInputStream(fileName);
085:       props.load(in);
086: 
087:       String drivers = props.getProperty("jdbc.drivers");
088:       if (drivers != null)
089:          System.setProperty("jdbc.drivers", drivers);
090:       String url = props.getProperty("jdbc.url");
091:       String username = props.getProperty("jdbc.username");
092:       String password = props.getProperty("jdbc.password");
093:       
094:       return
095:          DriverManager.getConnection(url, username, password);
096:    }
097: 
098:    public static void showResultSet(Statement stmt) 
099:       throws SQLException
100:    { 
101:       ResultSet rs = stmt.getResultSet();
102:       ResultSetMetaData rsmd = rs.getMetaData();
103:       int columnCount = rsmd.getColumnCount();
104:       while (rs.next())
105:       {  
106:          for (int i = 1; i <= columnCount; i++)
107:          {  
108:             if (i > 1) System.out.print(", ");
109:             System.out.print(rs.getString(i));
110:          }
111:          System.out.println();
112:       }
113:       rs.close();
114:    }
115: }