import java.sql.*; // for JDBC import java.io.*; // for file i/o /** * update a log table and print to the screen how many * rows the log table now has * * assumes that the Oracle student database is on * cedar.humboldt.edu, and that there is an account * on that database with username java and password * that you know and have stored in a file pwd-file.txt * in the same directory as this class' .class file * * @author Ann Burroughs * @author (modified by) Sharon Tuttle * @version 3-13-13 */ public class UpdateLog { /** * update a log table, and print to the screen * how many rows are now in this log table * * @param args not used */ public static void main(String args[]) { // turn off looking at IPv6 stack to avoid 2 minute // wait for V6 timeout System.setProperty("java.net.preferIPv4Stack", "true"); try { // load driver Class.forName("oracle.jdbc.driver.OracleDriver"); String username = "java"; String password = getPassword(); // create Connection object con Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@cedar:1521:student", username, password); //------ // try to insert into log table //------ Statement insertStmt = con.createStatement(); String insertSql = "insert into log_table " + "values " + "('" + username + "', sysdate)"; // debugging tip: // not a bad idea to print out your SQL string // if you want/need to double-check it System.out.println("insert is: \n" + insertSql + "\n"); int numInserted = insertStmt.executeUpdate(insertSql); if (numInserted == 1) { System.out.println("YAY! added row to " + "log_table!"); } else { System.out.println("No update? executeUpdate " + "returned: " + numInserted); } printNumRows(con, "log_table"); // close Statement and Connection objects insertStmt.close(); con.close(); } // end of try block // any exception thrown within the try-block above will be // caught and reported catch (Exception e) { System.out.println(e); } } // end main /** * reads the java account password from the file pwd-file.txt, * assumed to be in the same directory as this class * * @return the password read */ private static String getPassword() { BufferedReader fromStream; String password = ""; try { fromStream = new BufferedReader( new FileReader("pwd-file.txt")); password = fromStream.readLine(); fromStream.close(); } catch (FileNotFoundException exc) { System.out.println( "Could not open: " + "pwd-file.txt"); } catch (IOException exc) { System.out.println( "IOError: " + exc.getMessage()); } return password; } /** * for the given database connection, print to * the screen the number of rows in the given table * * @param thisCon the database connection to be used * @param desiredTable the table whose rows are * to be counted */ private static void printNumRows(Connection thisCon, String desiredTable) { try { Statement numRowsStmt = thisCon.createStatement(); String numRowsQuery = "select count(*) " + "from " + desiredTable; ResultSet rS = numRowsStmt.executeQuery(numRowsQuery); rS.next(); System.out.println("Table " + desiredTable + " has " + rS.getInt(1) + " row(s). " ); numRowsStmt.close(); } catch (Exception exc) { System.out.println(exc.toString()); } } } // end UpdateLog class