Searching my database
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Simon_Cunnell
Posted On:   Thursday, January 23, 2003 03:02 AM

View Records Hi, I am trying to produce a jsp that searches my database. I would like the user to type in the surname of a person that they are looking for and retrieve the relevent information about them. I can connect to the database with no problem, producing a table of all the people and thier details. I am not sure if i need to use a Java bean to perform this task. If so how do I go about doing this as I dont have any experience in this field! My current code is as follows: <%@ page language="java" import="java.sql.*" import="javax.swing.*" import="java.awt.*" import="java.util.*"%> Members Details Page    More>>
View Records

Hi,


I am trying to produce a jsp that searches my database. I would like the user to type in the surname of a person that they are looking for and retrieve the relevent information about them. I can connect to the database with no problem, producing a table of all the people and thier details. I am not sure if i need to use a Java bean to perform this task. If so how do I go about doing this as I dont have any experience in this field! My current code is as follows:



<%@ page language="java" import="java.sql.*" import="javax.swing.*" import="java.awt.*" import="java.util.*"%>








Members Details Page












<% Connection con = null;

String URL = "jdbc:odbc:Woodbridge GC";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(URL,"","");
Statement statement = con.createStatement();

if ( request.getParameter("name") != null )
{
ResultSet rs = statement.executeQuery("SELECT * from MembersJSP WHERE Surname='name'");
ResultSetMetaData rsmd = rs.getMetaData(); // Meta data contains column names etc
int num = rsmd.getColumnCount();

out.println(" ");

for (int col = 1 ; col <= num; col++)
{
out.println(" ");
}

out.println(" ");

while (rs.next())
{
out.println(" ");

String linkStr = "";
for (int col = 1 ; col <= num; col++)
{
out.println(" ");
}

out.println(" ");
}

rs.close();

}

%>

");
out.println(rsmd.getColumnName(col));

out.println("
");

out.println(rs.getString(col));
out.println("






As you can see I have tried to use the 'name' variable and add it to the query to call the relevent people but this is not working and the table remains blank.

Many thanks

Simon Cunnell

   <<Less

Re: Searching my database

Posted By:   Anonymous  
Posted On:   Thursday, January 23, 2003 07:20 AM

1. Please use the "pre" tag with code.

2. You are making this awfully complicated for nothing.

3. I stand to be corrected, but I don't believe you can get info from a record set after it is closed.

4. You should be able to accomplish what you want with a single "for" statement. And why bother naming the record set columns dynamically (it seems to me to add needless complexity)?

4. Why not generate your code dynamically? In the case of a combo box, for example,


....
while ( rs.next()) {

%>
..option value=<%= rs.getInt(1)%>><%=rs.getString(2)+ " --- " + rs.getInt(3) + " " + rs.getString(4)%><%

} // end while
...



You can get exactly the same effect by using out.println to print your HTML code (eg "..td>", etc as well as the results from the record set.

hope this is helpful

J Smith

Aylmer, PQ

About | Sitemap | Contact