public static void GenerateCustomerBill(Connection conn) { // We intended to produce the bills over certain time periods but // we will offer an option to check just the last 3 months. try { int c_id = BooksAThousand.getIntFromShell("Enter customer ID: "); Statement statement = conn.createStatement(); ResultSet rs = statement.executeQuery("select address, name from customer where customer_id = " + c_id); rs.next(); String address = rs.getString(1); String name = rs.getString(2); rs = statement.executeQuery( "select customer_id, isbn, quantity, price, customer_order_date" + " from customer_order where customer_id=" + c_id + " and is_paid='N'"); float total = 0; if (rs.next()) { Date date; SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm"); System.out.println("\n" + address); System.out.println("\nDear " + name + ", \nBelow are your unpaid orders. Pay up.\n"); System.out.printf( "%-10s %-20s %-5s %-8s %-11s\n", "ISBN", "Date", "Quant", "Price", "Total"); do { total += rs.getInt(3) * rs.getFloat(4); date = rs.getDate(5); System.out.printf( "%-10s %-20s %-5d $%-7.2f $%-10.2f \n", rs.getInt(2), sdf.format(date), rs.getInt(3), rs.getFloat(4), rs.getInt(3) * rs.getFloat(4)); } while (rs.next()); System.out.printf("Total: $%.2f\n", total); } else { System.out.println("This user has no unpaid orders"); } } catch (Throwable e) { e.printStackTrace(); } }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Statement question; String query; ResultSet answer; connect(); try { query = "SELECT * FROM PILOT WHERE Address ='" + request.getParameter("city") + "'"; question = link.createStatement(); answer = question.executeQuery(query); PrintWriter pen; response.setContentType("text/html"); pen = response.getWriter(); pen.println("<HTML>"); pen.println("<HEAD> <TITLE> Answer </TITLE> </HEAD>"); pen.println("<BODY>"); while (answer.next()) { String pN = answer.getString("PilotNumber"); String lN = answer.getString("LastName"); String fN = answer.getString("FirstName"); String ad = answer.getString("Address"); float sa = answer.getFloat("Salary"); float pr = answer.getFloat("Premium"); Date hD = answer.getDate("HiringDate"); if (answer.wasNull() == false) { pen.println("<P><B> Pilot : </B>" + lN + " " + fN); pen.println("<P><B> ---Reference : </B>" + pN); pen.println("<P><B> ---Address : </B>" + ad); pen.println("<P><B> ---Salary : </B>" + sa); pen.println("<P><B> ---since : </B>" + hD); if (pr > 0) pen.println("<P><B> ---Premium : </B>" + pr); else pen.println("<P><B> ---No premium </B>"); } } pen.println("</BODY>"); pen.println("</HTML>"); answer.close(); question.close(); link.close(); } catch (SQLException e) { System.out.println("Connection error: " + e.getMessage()); } }
public static void main(String[] argv) throws Exception { String[] sArray = new String[8]; long time = System.currentTimeMillis(); try { FileInputStream fstream = new FileInputStream("system.in"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); int i = 0; while ((sArray[i] = br.readLine()) != null) { sArray[i] = sArray[i].split("=")[1].trim(); System.out.println(sArray[i]); i++; } in.close(); } catch (Exception e) { } System.out.println("-------- Oracle JDBC Connection Testing ------"); try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.out.println("Where is your Oracle JDBC Driver?"); e.printStackTrace(); return; } System.out.println("Oracle JDBC Driver Registered!"); Connection connection = null; try { connection = DriverManager.getConnection( "jdbc:oracle:thin:@//oracle1.cise.ufl.edu:1521/orcl", sArray[0], sArray[1]); } catch (SQLException e) { System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return; } ResultSet rset = null; if (connection != null) { System.out.println(" -|||- "); Process p = Runtime.getRuntime() .exec("sqlplus " + sArray[0] + "@orcl/" + sArray[1] + " @adwords.sql"); p.waitFor(); // CallableStatement cstmt; System.out.println(" ||| "); System.out.println( "Time taken in Milliseconds (establishing connection): " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); Process proc3 = Runtime.getRuntime() .exec( "sqlldr " + sArray[0] + "/" + sArray[1] + "@orcl DATA=Keywords.dat CONTROL=Keywords.ctl LOG=Keywords.log"); proc3.waitFor(); Process proc1 = Runtime.getRuntime() .exec( "sqlldr " + sArray[0] + "/" + sArray[1] + "@orcl DATA=Advertisers.dat CONTROL=Advertisers.ctl LOG=Advertiser.log"); proc1.waitFor(); Process proc2 = Runtime.getRuntime() .exec( "sqlldr " + sArray[0] + "/" + sArray[1] + "@orcl DATA=Queries.dat CONTROL=Queries.ctl LOG=Queries.log"); proc2.waitFor(); System.out.println(" ||| "); System.out.println( "Time taken in Milliseconds (loading dat): " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); CallableStatement callableStatement = null; String storeProc = "{call sq(?,?,?,?,?,?)}"; try { callableStatement = connection.prepareCall(storeProc); callableStatement.setInt(1, Integer.parseInt(sArray[2])); callableStatement.setInt(2, Integer.parseInt(sArray[3])); callableStatement.setInt(3, Integer.parseInt(sArray[4])); callableStatement.setInt(4, Integer.parseInt(sArray[5])); callableStatement.setInt(5, Integer.parseInt(sArray[6])); callableStatement.setInt(6, Integer.parseInt(sArray[7])); callableStatement.executeUpdate(); } catch (SQLException e) { System.out.println(e.getMessage()); } /*callableStatement = connection.prepareCall("{call sq( " + Integer.parseInt(sArray[2]) + ", " + Integer.parseInt(sArray[3]) + ", " + Integer.parseInt(sArray[4]) + ", " + Integer.parseInt(sArray[5]) + ", " + Integer.parseInt(sArray[6]) + ", " + Integer.parseInt(sArray[7]) + " ) }");*/ System.out.println(" ||| "); System.out.println( "Time taken in Milliseconds (sql procedure): " + (System.currentTimeMillis() - time)); time = System.currentTimeMillis(); FileWriter fw = null; File file = null; PrintWriter pw = null; Statement resStmt = null; ResultSet resRs = null; int qid = 0; int rank; for (int i = 0; i < 6; i++) { file = new File("system.out." + (i + 1)); try { if (!file.exists()) file.createNewFile(); else file.delete(); fw = new FileWriter(file.getPath(), true); } catch (Exception e) { System.out.println(e.getMessage()); } pw = new PrintWriter(fw); resStmt = connection.createStatement(); resRs = resStmt.executeQuery( "SELECT * FROM OUTPUT" + (i + 1) + " order by qid asc, adrank asc"); while (resRs.next()) { qid = resRs.getInt("QID"); rank = (int) resRs.getFloat("ADRANK"); int advertiserId = resRs.getInt("ADVERTISERID"); float balance = resRs.getFloat("BALANCE"); float budget = resRs.getFloat("BUDGET"); StringBuffer resStr = new StringBuffer(); resStr.append(qid + "," + rank + "," + advertiserId + "," + balance + "," + budget); // System.out.println(resStr.toString()); pw.println(resStr.toString()); } pw.close(); } } else { System.out.println("Failed to make connection!"); } System.out.println( "Time taken in Milliseconds (file write): " + (System.currentTimeMillis() - time)); }