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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

servlet and java bean 1

Status
Not open for further replies.

maxpower1

Programmer
Jul 29, 2001
488
0
0
US
how can I call a method in java bean(that does connection to the database) to retrieve data from a servlet?

DO I need to extend the javabean class from the servlet? Sounds like a poor design.

thanks
 
>> DO I need to extend the javabean class from the servlet?

A Servlet is just a Java class so you can use other classes in it just like any other Java class.



-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
thanks pete! you cleared my beleagured mind. any samples? else, I think I know how to proceed.
 
>> any samples?

I don't really do any JSP developement at home so my samples are all at the office. If you have installed Tomcat there are many samples that are installed under the web site. I think the directory name is "Examples" or something like that.

-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
to get connection you can write a bean, which does it the sample for it i'll give it connects to a datasource and you use Jrun 3.1 or 4 for that.
-----------------------------
package com.sysarris.model;

import java.sql.*;
import java.util.*;
import java.io.*;
import javax.sql.*;
import javax.naming.*;



public class SysConnector
{
private Connection conn;
private String msgconn;


public SysConnector()
{
}



public Connection getConnection()
{
try
{
if(conn!=null)
{

if(conn.isClosed())
{
InitialContext ctx = new InitialContext();
// Look up data source in InitialContext.
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/ldsdatasrc");
conn = ds.getConnection();

}
}
else
{
InitialContext ctx = new InitialContext();
// Look up data source in InitialContext.
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/ldsdatasrc");
conn = ds.getConnection();

}

}catch(Exception e)
{
}

return conn;
}



}
------------------------------
use this for connection and than u can use query to get the result set from bean and than use it in the JSp page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top