Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to retrieve data from table and display in jsp???

843835Sep 12 2002 — edited Sep 12 2002
hello all..
this is my jsp code named testview.jsp :

<html>
<head>
<title>Connecting to database</title>
</head>
<body>

<%@page import="java.lang.*"%>
<%@page import="java.util.*"%>
<%@page import="Viewtable" %>

<jsp:useBean id="view" class="Viewtable" scope="session" />
<jsp:setProperty name="view" property="*" />

<% view.connect(); %>

</body>
</html>

and this is my java code named Viewtable.java

import java.sql.*;
import java.util.*;
import java.io.*;
import java.lang.*;

public class Viewtable{

String error;
Connection con;
Statement st;
ResultSet rs;
String qty;
String id;


// define the data source for the driver
String sourceURL = "jdbc:postgresql://localhost/b2bscm";
String username = "user";
String password = "psswd";

public Viewtable() {

}

public void connect() throws ClassNotFoundException,SQLException,
Exception {
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(sourceURL,username,password);
System.out.println("Connected to Database");
}
catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
System.out.println("Exception=" + cnfe);
throw new ClassNotFoundException(error);
}
catch (SQLException cnfe) {
error = "SQLException: Could not connect to database.";
System.out.println("Execption = " + cnfe);
throw new SQLException(error);
}
catch (Exception e) {
error = "Exception: An unknown error occurred while connecting "
+ "to database.";
throw new Exception(error);
}
}

// end method connect

public void getrow() throws SQLException {

System.out.println("see what happen" );
String query = "SELECT PRODID,QUANTITY FROM INVENTORY";
st = con.createStatement();
rs = st.executeQuery(query);
while (rs.next()) {
qty = rs.getString("QUANTITY");
id = rs.getString("PRODID");
System.out.println(qty + " " );
}
}

public void disconnect() throws SQLException {
try {
if ( con != null ) {
con.close();
}
}
catch (SQLException sqle) {
error = ("SQLException: Unable to close the database connection.");
throw new SQLException(error);
}
}
}
// end method disconnect} End class


the problem is..i don't know how to return the value of the select table from Viewtable.java to my jsp file...

anybody can help me??

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2002
Added on Sep 12 2002
1 comment
468 views