Here is a code I have written for a test long time back, may be
of your help... create the database tables, I suppose you are
well versed with SQL.
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This is a simple example of an HTTP Servlet that uses the HttpSession
* class
*
* Note that in order to gaurentee that session response headers are
* set correctly, the session must be retrieved before any output is
* sent to the client.
*/
public class database extends HttpServlet {
private String name;
private String password;
private String call_no ;
private String title;
private String author;
private String publisher;
private String year_published;
private String query;
private boolean call_nof;
private boolean titlef;
private boolean authorf;
private boolean publisherf;
private boolean year_publishedf ;
private PrintWriter out;
private HttpServletResponse res;
private HttpSession session;
private HttpServletRequest req;
private String common;
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
this.session = session;
this.req = req;
this.res = res;
session = req.getSession(true);
// set content type and other response header fields first
res.setContentType("text/html");
name = req.getParameter("name");
password = req.getParameter("password");
call_no = req.getParameter("call_no");
title = req.getParameter("title");
author = req.getParameter("author");
publisher = req.getParameter("publisher");
year_published = req.getParameter("year_published");
common = new String("<html><title>Database query result</title><body
bgcolor=white> <b><font color=\"#993300\"<font
size=+1></font></font></b><p><b><font color=\"#993300\"><font size=+1>Database query
page :</font></font></b><br> <br> <form method=GET name=\"Search\"
action =\"http://www.yourdomain.com:your port/servlet/database\">SQL*Plus
User
name:<INPUT type=text name=\"name\" value=\""+name+"\" size=20 maxlength=30>
<br><br>SQL*Plus password : <INPUT type=password name=\"password\"
value=\""+password+"\" size=20 maxlength=30>
<table BORDER COLS=3 WIDTH=\"100%\" NOSAVE ><tr><td><b><i>
<font color=\"#006600\">Call_No</font></i></b></td>
<td><b><i><font color=\"#006600\">
<INPUT type=text name=\"call_no\" value=\"\" size=20 maxlength=30></font></i>
</b></td></tr><tr><td><b><i><font color=\"#006600\">Title</font>
</i></b></td><td><b><i><font color=\"#006600\">
<INPUT type=text name=\"title\" value=\"\" size=20 maxlength=30>
</font></i></b></td></tr><tr><td><b><i><font color=\"#006600\">
Author</font></i></b></td><td><b><i><font color=\"#006600\">
<INPUT type=text name=\"author\" value=\"\" size=20 maxlength=30></font>
</i></b></td></tr><tr><td><b><i><font color=\"#006600\">Publisher</font>
</i></b></td><td><b><i><font color=\"#006600\"><INPUT type=text
name=\"publisher\" value=\"\" size=20 maxlength=30></font></i></b>
</td></tr><tr><td><b><i><font color=\"#006600\">Year_Published</font></i>
</b></td><td><b><i><font color=\"#006600\"><INPUT type=text
name=\"year_published\" value=\"\" size=20 maxlength=30></font></i>
</b></td></tr></table></table><b><i><font color=\"#006600\">
<INPUT type=submit name=\"submit\"></font></i></b></form><p><b>
<font color=\"#993300\"><font size=+1>Result:</font></font></b>");
out = res.getWriter();
try{
ExecuteQuery();
}
catch(java.sql.SQLException e){}
}
private void ExecuteQuery() throws IOException, java.sql.SQLException
{
String query1 = new String();
call_nof = false;
titlef = false;
authorf = false;
publisherf = false;
year_publishedf = false;
if(!call_no.equals(""))
call_nof = true;
if(!title.equals(""))
titlef = true;
if(!author.equals(""))
authorf = true;
if(!publisher.equals(""))
publisherf = true;
if(!year_published.equals(""))
year_publishedf = true;
query1 = "select call_no, title, author, publisher, year_published from book ";
//where ";
query =" ";
if(call_nof == false && titlef == false && authorf == false
&& publisherf == false && year_publishedf == false)
query = query;
else
{
query+= " where ";
if(call_nof == true)
query+= " call_no = '"+call_no+"'";
if((call_nof==false)&&(titlef == true))
query+= " title = '"+title+"'";
else if((call_nof==true)&&(titlef == true))
query+= " and title = '"+title+"'";
if((authorf == true) && ((call_nof == true) || (titlef == true)))
query+= " and author = '"+author+"'";
else if((authorf == true) && (call_nof == false) && (titlef == false))
query+= "author = '"+author+"'";
if((publisherf == true) && ((authorf == true) || (call_nof == true) ||
(titlef == true)))
query+= " and publisher = '"+publisher+"'";
else if((publisherf == true) && (authorf == false) && (call_nof ==
false) && (titlef == false))
query+= " publisher = '"+publisher+"'";
if((year_publishedf == true)&&((authorf == true) || (call_nof == true)
|| (titlef == true) ||(publisherf == true)))
query+= " and year_published = '"+year_published+"'";
else if((year_publishedf == true) && (publisherf == false) && (authorf
== false) && (call_nof == false) && (titlef == false))
query+= " year_published = '"+year_published+"'";
}
// then write the data of the response
out.println(common);
try{
DataBaseServer dbs = new DataBaseServer(name,password);
dbs.BookIndex(query1+query, out);
}
catch(java.sql.SQLException e)
{
out.println("<p><font color= red>There is a problem in this DataBase
transaction " + e+"</font></p>") ;
}
out.println("<font size =+1 color = blue>The query generated from your request
is: </font><br><font size =+1 color = brown> Select * from book "+ query +";</font>");
out.println("</body></html>");
out.close();
}
public String getServletInfo()
{
return "The servlet running. Design & Code: Sandip Debnath";
}
}
Sandip
--------------------------------------------------------------------------
On Tue, 5 Sep 2000, Vijay Deshpande wrote:
> Date: Tue, 5 Sep 2000 11:50:31 +0530
> From: Vijay Deshpande <[EMAIL PROTECTED]>
> Reply-To: A mailing list for discussion about Sun Microsystem's Java
Servlet API Technology. <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: servlet database connectivity
>
> Hi friends,
> I'm Vijay. I'm currently working on a project which involves web server on one
> machine & a database server on different machine. I want to develope a servlet
> for accessing database. but i am facing problems in that. Can anybody guide me
> on how i can go ahead in this??
>
> thanks,
> Vijay
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html