/**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 Project2_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();
Project2_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 name = req.getParameter( "name" );
String job = req.getParameter( "job" );//job name of process
String id = req.getParameter( "id" );//id
String inputf, outputf, type, ann;
long times, timed;
try{
out.println( "");
out.println( "
" + "Project2_Client html" + "" );
out.println("");
//reload the client, and put the returning data near it.
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( "ture" ) == 0 ){
//is for search
stroage = new Project2_Stroage( name, job, id,null, null, 0, 0, null, null );
find( stroage, conn, out );
}//if
else if( insert.compareTo( "ture" ) == 0 ) {
//for insert, and get time, input, output files, type, annonation of process.
times = Long.parseLong( req.getParameter( "times" ) );
timed = Long.parseLong( req.getParameter( "timed" ) );
inputf = req.getParameter( "inputf" );
outputf = req.getParameter( "outputf" );
type = req.getParameter( "type" );
ann = req.getParameter( "ann" );
stroage = new Project2_Stroage( name, job, id, inputf, outputf, times, timed, type, ann );
insert( stroage, conn, out );
}//else if
else if( update.compareTo( "ture" ) == 0 ) {
//updata the process.
times = Long.parseLong( req.getParameter( "times" ) );
timed = Long.parseLong( req.getParameter( "timed" ) );
inputf = req.getParameter( "inputf" );
outputf = req.getParameter( "outputf" );
type = req.getParameter( "type" );
ann = req.getParameter( "ann" );
stroage = new Project2_Stroage( name, job, id, inputf, outputf, times, timed, type, ann);
// binding input parameters to SQL call
updata( stroage, conn, out );
}//else if
else {
stroage = new Project2_Stroage( name, job, id, null, null, 0, 0, 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( Project2_Stroage stroage, Connection conn, ServletOutputStream out ) {
PreparedStatement pstmt;
try{
try{
try{
out.println( stroage.name + " " + stroage.id );
ResultSet rs = null;
//prepare to access JDBC
if( !(stroage.name.equals( null )) && stroage.job.equals( null ) && stroage.id.equals( null ) ){
// if( not(stroage.name.equals( null )) && stroage.job.equals( null ) && stroage.id.equals( null ) && stroage.job == "" && stroage.id == "" ){
out.println( stroage.name +" " + stroage.job + " " +stroage.id + " " );
pstmt = conn.prepareStatement("select * from project2_table where name = ? order by times");
pstmt.setString( 1, stroage.name );
// Execute the SQL, ResultSet is row(s) of data returned by database query
rs = pstmt.executeQuery(); }
else if( !(stroage.name.equals( "" )) && !(stroage.job.equals( null )) && stroage.id.equals( "" )){
if( stroage.job.indexOf( "*" ) != -1 )
stroage.job.replace( '*', '%' );
if( stroage.job.indexOf( "?" ) != -1 )
stroage.job.replace( '?', '%' );
pstmt = conn.prepareStatement("select * from project2_table where name = ? and job = ? order by times");
pstmt.setString( 1, stroage.name );
pstmt.setString( 2, stroage.job );
// Execute the SQL, ResultSet is row(s) of data returned by database query
rs = pstmt.executeQuery();
out.println( stroage.name +" " + stroage.job + " " +stroage.id );
}//else if
else if( stroage.name.equals( "" ) && stroage.id.equals( "" )){
if( stroage.job.indexOf( "*" ) != -1 )
stroage.job.replace( '*', '%' );
if( stroage.job.indexOf( "?" ) != -1 )
stroage.job.replace( '?', '%' );
pstmt = conn.prepareStatement("select * from project2_table where job = ? order by times");
pstmt.setString( 1, stroage.job );
// Execute the SQL, ResultSet is row(s) of data returned by database query
rs = pstmt.executeQuery(); }//else if
else {
pstmt = conn.prepareStatement("select * from project2_table where id = ? order by times");
pstmt.setInt( 1, Integer.parseInt( stroage.id ));
// Execute the SQL, ResultSet is row(s) of data returned by database query
rs = pstmt.executeQuery();
}
out.println( stroage.id );
// 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.name = rs.getString( 1 );
stroage.job = rs.getString( 2 );
stroage.id = Integer.toString( rs.getInt( 3 ) );
stroage.inputf = rs.getString( 4 );
stroage.outputf = rs.getString( 5 );
Date date = rs.getDate( 6 );
stroage.times = date.getTime();
out.println( stroage.times );
Date dat = rs.getDate( 7 );
stroage.timed = dat.getTime();
stroage.type = rs.getString( 8 );
stroage.ann = rs.getString( 9 );
out.println( "" );
out.println( "- " + "User Name: " + stroage.name );
out.println( "
- " + "Job: " + stroage.job );
out.println( "
- " + "ID: " + stroage.id );
out.println( "
- " + "Input File: " + stroage.inputf );
out.println( "
- " + "Output File: " + stroage.outputf);
/*
int month = date.getMonth() + 1;
int date_ = date.getDate();
int year = date.getYear() + 1900;
out.println( "
- " + year );
int hour = date.getHours();
out.println( "
- " + hour);
int minute = date.getMinutes();
out.println( "
- " + minute );
StringBuffer expres = new StringBuffer( );
expres.append(month);
expres.append("/");
expres.append(date_);
expres.append("/");
expres.append(year);
expres.append(" ");
expres.append(hour);
expres.append(":");
expres.append(minute);
String expres = new String( month + "/" + date_ + "/" + year +" " + hour + ":" + minute );
out.println( "
- " + "Time Sent: " + expres.toString() );
out.println( "
- " + "Time Done: " + expres.toString() ); */
out.println( "
- " + "Time Sent: " + date.toString() );
out.println( "
- " + "Time Done: " + dat.toString() );
out.println( "
- " + "Type: " + stroage.type);
out.println( "
- " + "Annotation: " + stroage.ann);
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.toString());
ex.printStackTrace();
}
}
catch (Exception ext ){}
}
/**Purpose:insert a process into table
Input:Stroage of object, Connection of object, ServletOutputStream of object
Output:Void
**/
private void insert( Project2_Stroage stroage, Connection conn, ServletOutputStream out ){
try{
try{
String s1 = "insert into project2_table " + "( name,job, id, inputf, outputf, times, timed, type, annotation)" + "values ( ?,?,?,?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement( s1 );//prepare statement
pstmt.setString( 1, stroage.name );
pstmt.setString( 2, stroage.job);
pstmt.setInt( 3, Integer.parseInt( stroage.id ));
pstmt.setString( 4, stroage.inputf);
pstmt.setString( 5, stroage.outputf);
pstmt.setDate( 6, new Date( stroage.times ) );
pstmt.setDate( 7, new Date( stroage.timed ) );
pstmt.setString( 8, stroage.type );
pstmt.setString( 9, stroage.ann );
// Execute the SQL, ResultSet is integer.
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( Project2_Stroage stroage, Connection conn, ServletOutputStream out ){
try{
try{
String s1 = "update project2_table " + "set inputf = ?, outputf =?, times =?, timed = ?, type = ?, annotation = ?" + "where name = ? and job = ?";
PreparedStatement pstmt = conn.prepareStatement( s1 );
pstmt.setString(1, stroage.inputf);
pstmt.setString(2, stroage.outputf );
pstmt.setDate( 3, new Date( stroage.times ) );
pstmt.setDate( 4, new Date( stroage.timed ) );
pstmt.setString( 5, stroage.type );
pstmt.setString( 6, stroage.ann );
pstmt.setString( 7, stroage.name );
pstmt.setString( 8, stroage.job );
// 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( Project2_Stroage stroage, Connection conn, ServletOutputStream out ){
try{
try{
// binding input parameters to SQL call
String s1 = "delete from project2_table " + "where name = ? and job = ?";
PreparedStatement pstmt = conn.prepareStatement( s1 );
pstmt.setString(1, stroage.name );
pstmt.setString(2, stroage.job );
// 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) {}
}
}