import java.sql.*; import java.util.*; import java.io.*; /** * update the log_table on the java account * with potentially several new strings and today's * date and time * * assumes: * * that the Oracle student database * is on cedar.humboldt.edu * * * that there is an account on that database * with the username java * whose password is stored in a (read-protected) * file named pwd-file.txt in the same directory * as this class' .class file * * @author Ann Burroughs * @author modified by Sharon Tuttle * @version 2015-11-16 **/ public class AddManyToLog { /** * if the command-line arguments are short enough, * tries to add a log_table row for each with each * argument and the current date (which includes * the current time as well) * * @param args the strings to serve as log_table usernames */ public static void main(String[] args) { // at least one command-line argument is required if (args.length < 1) { System.err.println("AddManyToLog: at least ONE command " + "argument is required!"); System.exit(1); } // if get here, TRY to insert rows! System.setProperty("java.net.preferIPv4Stack", "true"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); // get the password from a local file BufferedReader fromStream; String username = "java"; String password = ""; try { fromStream = new BufferedReader( new FileReader("pwd-file.txt")); password = fromStream.readLine(); fromStream.close(); } catch (FileNotFoundException exc) { System.out.println("AddManyToLog: Could not open " + "pwd-file.txt"); System.exit(1); } catch (IOException exc) { System.out.println("AddManyToLog: IOError: " + exc.getMessage()); } // connect to database on cedar Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@cedar.humboldt.edu:1521:student", username, password); // turn off JDBC's auto-commit default con.setAutoCommit(false); String insertPrepString = "insert into log_table " + "values " + "(?, sysdate)"; // for debugging, and here for demo, // not a bad idea to check out your // resulting command string System.out.println("insert command is: \n" + insertPrepString + "\n"); PreparedStatement insertPrepStmt = con.prepareStatement(insertPrepString); int numRowsInserted = 0; String nextName = ""; for (int i=0; i < args.length; i++) { nextName = args[i]; while (nextName.length() > 5) { System.out.println("TOO LONG: " + nextName + "!\n" + "type one <=5: "); Scanner input = new Scanner(System.in); nextName = input.next(); } insertPrepStmt.setString(1, nextName); numRowsInserted = insertPrepStmt.executeUpdate(); if (numRowsInserted == 1) { System.out.println("YAY! " + nextName + " row inserted!"); } else { System.out.println("OH NO! no row inserted"); } } // I feel safe committing these changes NOW con.commit(); insertPrepStmt.close(); con.close(); } // end of try-block // any exception thrown from my "outer" try // will be caught and reported catch (Exception exc) { System.out.println("AddManyToLog: unexpected exception: " + exc.getMessage()); } } // end main method } // end class AddManyToLog