wood burning stoves 2.0*
The moose likes JDBC and the fly likes jsp & database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "jsp & database" Watch "jsp & database" New topic
Author

jsp & database

chauhan prakash
Greenhorn

Joined: Oct 08, 2006
Posts: 6
Hi,

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.


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

<body>
<center>
<font color = "blue" size ="6">
a listing of books
</font>
</center>
<br><br>
<%@ 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>
</tr>

<%

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.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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(rs.next()) {
System.out.println("after result set");
%>
<tr><td><%=rs.getString("Title")%> </td>
<td><%=rs.getString("Author")%> </td>
<td><%=rs.getInt("Price")%> </td>
</tr>
<%
}
%>

</table>

<%
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}//end of try block

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


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

catch (Exception e)
{
out.print(e.getMessage());
System.out.println("general exception");
}
%>
</body>
</html>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

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] [Bear's FrontMan] [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=?
pSObject.setString(1,subject)

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
Greenhorn

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: 3703
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: jsp & database
 
Similar Threads
save checked value in checkbox to database
when i retrive data from access it showing empty page in tomcat
Number Format Exception
reading data from jsp page to another jsp page using request.getParameter
small question for a e-store problem