Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP and Java Classes

Status
Not open for further replies.

javoine

Programmer
May 22, 2003
25
US
Hi,

I'm an ASP programmer who is making the change to JSP and I'm starting to get pressed for time on a project. I've worked through several tutorials and I've used Pure JSP to connect to my databases and work with them, it is suggested in all the reading that the database connection, etc...be established in a Java class instead of directly in the JSP page, but I can't find any pertinent or even simple examples and I'm starting to get rushed in my learning.

Here is what I'd like an example of if anyone knows how or where to find it:

Java Class

establish connection to a database and throw an error if it doesnt connect.

JSP Page
Extract data, write data to the database created by the class.

I've written a Java Class that connects to the database sucessfully, but I cant figure out how to use it in JSP. I've tried to write code to instantiate the class, but then can't seem to access the connection inside it or anything?

The JSP page has similar code to this:

%
NameofJavaClass GetData; (where getdata is my object variable)
GetData = new NameofJavaClass();
%>

PLEASE help give me some direction, I'm just looking for a simple example on JSP accessing connection info from a JAva Class.

Thanx
 
I appreciate the help, but I'm not using a javabean...I know a javabean is a class, but i'm creating a java class which looks like this:

package TestPkg;

import java.io.*;
import java.sql.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;


public class Tests {

/** Creates a new instance of Tests */
public Tests(HttpServletRequest request, HttpServletResponse response, JspWriter out)
throws UnavailableException {

Connection con;

try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection("jdbc:postgresql://localhost/TestDB", "Administrator","");
}
catch (Exception e) {
throw new UnavailableException(e.getMessage());
}
}
}

Then I want a JSP page to access the connection that was created.

So i import the package to my jsp page and create an instance of the class...then...i dont know how to access the conneciton?

any simple examples of how this is done?

thanx again for any help.
 
Oh, OK ... at the moment you do all the work in the constructor ... which by definition doesn't return anything.

Try :

<code>
I appreciate the help, but I'm not using a javabean...I know a javabean is a class, but i'm creating a java class which looks like this:

package TestPkg;

import java.io.*;
import java.sql.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;


public class Tests {

/** Creates a new instance of Tests */
public Tests(HttpServletRequest request, HttpServletResponse response, JspWriter out)
throws UnavailableException {
// set up some stuff if you need to here
}

public Tests() {
//default contstructor
}

public Connection getConnection() {
Connection con;

try {
Class.forName(&quot;org.postgresql.Driver&quot;);
con = DriverManager.getConnection(&quot;jdbc:postgresql://localhost/TestDB&quot;, &quot;Administrator&quot;,&quot;&quot;);
}
catch (Exception e) {
throw new UnavailableException(e.getMessage());
}
}
}
</code>


Then in your JSP page, you initialise your object, and call the method to get the connection and return it.

Tests dbconn = new Tests();
Connection conn = dbconn.getConnection();
 
ok, thanks for the tip, but after making this change, I receive this error:

TestPkg/Tests.java [31:1] unreported exception javax.servlet.UnavailableException; must be caught or declared to be thrown
throw new UnavailableException(e.getMessage());

so what am i doing wrong? The code I have has been modified to look like the code suggested to me in this thread?

thanx again for any help.
 
It wouldn't compile because in the method getConnection() you throw UnavailableException. If you modify it so that method throws that exception, you'll be OK ... try the below code !


import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public class Tests {

/** Creates a new instance of Tests */
public Tests(HttpServletRequest request, HttpServletResponse response, JspWriter out)
throws UnavailableException {
// set up some stuff if you need to here
}

public Tests() {
//default contstructor
}

public Connection getConnection() throws UnavailableException {
Connection con;

try {
Class.forName(&quot;org.postgresql.Driver&quot;);
con = DriverManager.getConnection(&quot;jdbc:postgresql://localhost/TestDB&quot;, &quot;Administrator&quot;,&quot;&quot;);
return con;
}
catch (Exception e) {
throw new UnavailableException(e.getMessage());
}
}
}
 
sedj,

thanx...you're a life saver. now that i see it, it completely makes sense...so thanks for the helping hand and showing me the error in my ways...

-prec'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top