Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp & database

 
chauhan prakash
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Should have been



I suggest using PreparedStatement. Search this forum or google for how to use prepared statements
 
nelson christos
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chauhan prakash
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Josh Juneau
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic