/**This servlet get request from client and extract the process name, id, input file, output file, time sending, time done, type of job, and annonation from http request. All these parameters are used to access database by JDBC. This program can do the jobs of lookup, insertion, and deletion a process. Excepti on can be returned to client. API: void service( HttpServletRequest, HttpServletResponse ) throw ServletException, IOException void find( Stroage, Connection, ServletOutputStream ); void insert( Stroage, Connection, ServletOutputStream ); void updata( Stroage, Connection, ServletOutputStream ); void delete( Stroage, Connection, ServletOutputStream ); void destroy(); void close(); @author Gang Wei @version 1.0 **/ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class DBservlet extends HttpServlet implements SingleThreadModel { private Connection conn;//for connection with JDBC private ServletConfig config;//servlet config private ServletOutputStream out;//output stream to client /**Purpose:To get parameters from http request and access the database by calling JDBC method. Input: HttpServletReques, HttpServletResponse; Output:void; **/ public void service(HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { resp.setContentType( "text/html" ); out = resp.getOutputStream(); Stroage stroage; //get parameters from the http request. String search = req.getParameter( "search" );//fro search? String insert = req.getParameter( "insert" );//for insert? String update = req.getParameter( "update" );//for updata? String delete = req.getParameter( "delete" );//for delete? String job = req.getParameter( "job" );//job name of process String id = req.getParameter( "id" );//id String inputf, outputf, times, timed, type, ann; try{ out.println( ""); out.println( "" + "Client html" + "" ); out.println(""); //reload the client, and put the returning data near it. out.println(""); out.println(""); try { Class.forName("oracle.jdbc.driver.OracleDriver");//get drive. //get connection with JDBC conn = DriverManager.getConnection("jdbc:oracle:thin:@carver.npac.syr.edu:1521:europe" , "gangwei", "gangwei"); if( search.compareTo( "true" ) == 0 ){ //is for search stroage = new Stroage( job, id,null, null, null, null, null, null ); find( stroage, conn, out ); }//if else if( insert.compareTo( "true" ) == 0 ) { //for insert, and get time, input, output files, type, annonation of process. times = req.getParameter( "times" ); timed = req.getParameter( "timed" ); inputf = req.getParameter( "inputf" ); outputf = req.getParameter( "outputf" ); type = req.getParameter( "type" ); ann = req.getParameter( "ann" ); stroage = new Stroage( job, id, inputf, outputf, times, timed, type, ann ); insert( stroage, conn, out ); }//else if else if( update.compareTo( "true" ) == 0 ) { //updata the process. times = req.getParameter( "times" ); timed = req.getParameter( "timed" ); inputf = req.getParameter( "inputf" ); outputf = req.getParameter( "outputf" ); type = req.getParameter( "type" ); ann = req.getParameter( "ann" ); stroage = new Stroage( job, id, inputf, outputf, times, timed, type, ann); // binding input parameters to SQL call updata( stroage, conn, out ); }//else if else { stroage = new Stroage( job, id, null, null, null, null, null, null ); //delete the process delete( stroage, conn, out ); } out.println("
"); out.println(""); out.println( "" ); } catch ( SQLException e) { out.println("Error in dbase" + e.toString() ); out.println( "SQLState: " + e.getSQLState() ); out.println( "Message: " + e.getMessage() ); out.println( "Vender: " + e.getErrorCode() ); } }//ioexception catch (Exception ex) { out.println("ioexception "+ex.getMessage()); ex.printStackTrace(); } } /**Purpose:find the data in the database table and return to client Input:Stroage of object, Connection to JDBC of object, ServletOutputStream of object. Output:void **/ private void find( Stroage stroage, Connection conn, ServletOutputStream out ) throws IOException { try{ try{ //prepare to access JDBC PreparedStatement pstmt = conn.prepareStatement("select * from process_idtable where id = ?"); pstmt.setInt(1, Integer.parseInt( stroage.id )); // Execute the SQL, ResultSet is row(s) of data returned by database query ResultSet rs = null; rs = pstmt.executeQuery(); // return the fields of the first row of the result set int havedata = 0; //for checking if there is data in the table. while( rs.next() ){ havedata++; stroage.job = rs.getString( 1 ); stroage.id = Integer.toString( rs.getInt( 2 ) ); stroage.inputf = rs.getString( 3 ); stroage.outputf = rs.getString( 4 ); stroage.times = rs.getString( 5 ); stroage.timed = rs.getString( 6 ); stroage.type = rs.getString( 7 ); stroage.ann = rs.getString( 8 ); out.println( "" ); } if( havedata == 0 ) out.println("No data in the table." ); pstmt.close();//closeing the statement. } catch ( SQLException e) { out.println("Error in dbase" + e.toString() ); out.println( "SQLState: " + e.getSQLState() ); out.println( "Message: " + e.getMessage() ); out.println( "Vender: " + e.getErrorCode() ); } }//ioexception catch (Exception ex) { out.println("ioexception "+ex.getMessage()); ex.printStackTrace(); } } /**Purpose:insert a process into table Input:Stroage of object, Connection of object, ServletOutputStream of object Output:Void **/ private void insert( Stroage stroage, Connection conn, ServletOutputStream out ){ try{ try{ String s1 = "insert into process_idtable " + "(job, id, inputf, outputf, times, timed, type, annotation)" + "values ( ?,?,?,?,?,?,?,?)"; PreparedStatement pstmt = conn.prepareStatement( s1 );//prepare statement pstmt.setString(1, stroage.job); pstmt.setInt(2, Integer.parseInt( stroage.id )); pstmt.setString(3, stroage.inputf); pstmt.setString(4, stroage.outputf); pstmt.setString(5, stroage.times); pstmt.setString( 6, stroage.timed ); pstmt.setString( 7, stroage.type ); // Execute the SQL, ResultSet is integer. pstmt.setString( 8, stroage.ann ); int r = pstmt.executeUpdate( s1 ); pstmt.close(); out.println("Insert sucessed!"); }//try catch ( SQLException e) { out.println("Error in dbase" + e.toString() ); out.println( "SQLState: " + e.getSQLState() ); out.println( "Message: " + e.getMessage() ); out.println( "Vender: " + e.getErrorCode() ); } }//ioexception catch (Exception ex) { } } /**Purpose:updata a process already in the database Input:objects of Stroage, Connection, and ServletOutputStream. Output:void **/ private void updata( Stroage stroage, Connection conn, ServletOutputStream out ){ try{ try{ String s1 = "update process_idtable " + "set inputf = ?, outputf =?, times =?, timed = ?, type = ?, annotation = ?" + "where id = ? "; PreparedStatement pstmt = conn.prepareStatement( s1 ); pstmt.setString(1, stroage.inputf); pstmt.setString(2, stroage.outputf ); pstmt.setString( 3, stroage.times ); pstmt.setString( 4, stroage.timed ); pstmt.setString( 5, stroage.type ); pstmt.setString( 6, stroage.ann ); pstmt.setInt(7, Integer.parseInt( stroage.id )); // Execute the SQL, ResultSet is integer int r = pstmt.executeUpdate( s1 ); pstmt.close(); out.println( "Update: " + stroage.job + "Sucessed!" ); }//try catch ( SQLException e) { out.println("Error in dbase" + e.toString() ); out.println( "SQLState: " + e.getSQLState() ); out.println( "Message: " + e.getMessage() ); out.println( "Vender: " + e.getErrorCode() ); } }//ioexception, try catch (Exception ex) { } } /**Purpose:delete a process in the database. Input:object of ServletOutputStream, Stroage, nad Connection. Output:void **/ private void delete( Stroage stroage, Connection conn, ServletOutputStream out ){ try{ try{ // binding input parameters to SQL call String s1 = "delete from process_idtable " + "where id = ?"; PreparedStatement pstmt = conn.prepareStatement( s1 ); pstmt.setInt(1, Integer.parseInt( stroage.id )); // Execute the SQL, ResultSet is integer int r = pstmt.executeUpdate( s1 ); pstmt.close(); out.println( "Deleting is suceessed!" ); } catch ( SQLException e) { out.println("Error in dbase" + e.toString() ); out.println( "SQLState: " + e.getSQLState() ); out.println( "Message: " + e.getMessage() ); out.println( "Vender: " + e.getErrorCode() ); } }//ioexception, try catch (Exception ex) { } } /**Purpose:destroy the servlet Input:void Output; void **/ public void destroy() { close(); } /**Purpose:close connection with database Input:void Output:void **/ private void close() { try { conn.close(); } catch (Exception e) {} } }