File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes jsp & database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "jsp & database" Watch "jsp & database" New topic

jsp & database

chauhan prakash

Joined: Oct 08, 2006
Posts: 6

I have created the following table in MSAccess & displaying all records on the browser successfully(jsp file).

TitleAuthor Price
javamurach 400
ckanitkar 350
sqlkevin 450

Now I am calling the particular record of the table through a separate html file, which has 1 textfiled & 1 search button. When I insert the record in the textfield "java" & click on submit button only the "heading tag is displaying. That is

Title Author Price

It is not retrieving any data from the database. Find my jsp code as follows.I think so problem might be with query. Can anybody please help me out.

Thanks in advance.

<head> <title> an example of a simple model </title></head>

<font color = "blue" size ="6">
a listing of books
<%@ page import = "java.sql.*"%>

<table width = "100%" border="2" bgcolor="silver">
<tr> <th width = "50%"> Title </th>
<th width = "25%"> Author </th>
<th width = "25%"> Price </th>


String subject = request.getParameter("name"); //where name is the value of the textfield of html page.

try {
String query = "SELECT * FROM table1 where Title='subject'";
//table1 is the table name.

System.out.println("before class"); //breakpoint used.
Connection con = DriverManager.getConnection("Jdbc:Odbc:prakashdsn");
System.out.println("after Class");
Statement stmt = con.createStatement();
System.out.println("before query");
ResultSet rs = stmt.executeQuery(query);

System.out.println("after query");
while( {
System.out.println("after result set");
<tr><td><%=rs.getString("Title")%> </td>
<td><%=rs.getString("Author")%> </td>
<td><%=rs.getInt("Price")%> </td>


}//end of try block

catch(ClassNotFoundException c) {
System.out.println("class not found exception");

catch(SQLException s) {
System.out.println("sql exception");

catch (Exception e)
System.out.println("general exception");
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

Doing queries in a JSP? Bad form.

In any case, this has been moved to the JDBC forum since the question is not about JSP.

[Asking smart questions] [About Bear] [Books by Bear]
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425

Should have been

I suggest using PreparedStatement. Search this forum or google for how to use prepared statements
nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
why are you using the Statement object
try the same with preparedStatement
SELECT * FROM table1 where Title=?

anyway accessing the database through jsp is a very bad design
any db access has to the made then do it through servlets and store the
records in beans an for display purpose use the jsp

i think therefore i am
chauhan prakash

Joined: Oct 08, 2006
Posts: 6
I change the query as per your suggestion & I am getting the output. Anyway thanks a lot.

I just started learning jsp. Let me complete servlet then i must try this example as per your design pattern.

Thanks once again.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

DB code inside a JSP is bad, except possibly, when this is a homework assignment. In a professional application this would lead to [insert many bad things here] but if this is a school project, most teachers/professors wouldn't care (or notice).

Like professional software development, it helps to know your audience.

[OCA 8 Book] [Blog]
Josh Juneau
Ranch Hand

Joined: Jun 16, 2004
Posts: 86
I recommend trying out the JSF framework. I really enjoy using it as it helps to keep front end JSP pages clean, and all of the "Java code" goes into classes.

I also started out with JSP, but once I learned about JSF I began using it for every web application I develop in Java.

Database Administrator/Application Developer
I agree. Here's the link:
subject: jsp & database
It's not a secret anymore!