aspose file tools*
The moose likes Struts and the fly likes struts with jdbc example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts with jdbc example" Watch "struts with jdbc example" New topic
Author

struts with jdbc example

subhash rao
Greenhorn

Joined: Feb 17, 2005
Posts: 3
Hi

I am learning to struts frame work.
Can any body post simple struts with jdbc example.
Like where i have to define the database connection and which class i have to mention for data base related things.If possible pls send me the step wise code .
sanat
Ranch Hand

Joined: Dec 08, 2004
Posts: 111

Hi,

Take a look at this article.
http://struts.apache.org/faqs/database.html

JCE: The link above is out of date. Start with the Struts FAQ: How To Access A Database. There are links at the bottom of the page to numerous newsgroup discussions on this topic. Also try searching this forum, as this topic seems to come up often.
subhash rao
Greenhorn

Joined: Feb 17, 2005
Posts: 3
Hi

I am getting following exception when i execute the struts with jdbc .


<Apr 27, 2003 3:58:28 PM IST> <Error> <HTTP> <101018> <[ServletContext(id=821981
0,name=SimpleStruts,context-path=/SimpleStruts)] Servlet failed with ServletExce
ption
javax.servlet.UnavailableException: Initializing application data source org.apa
che.struts.action.DATA_SOURCE
at org.apache.struts.action.ActionServlet.initModuleDataSources(ActionSe
rvlet.java:1089)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:471)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at weblogic.servlet.internal.ServletStubImpl$ServletInitAction.run(Servl
etStubImpl.java:1075)
at weblogic.security.service.SecurityServiceManager.runAs(SecurityServic
eManager.java:744)
at weblogic.servlet.internal.ServletStubImpl.createServlet(ServletStubIm
pl.java:899)
at weblogic.servlet.internal.ServletStubImpl.createInstances(ServletStub
Impl.java:833)
at weblogic.servlet.internal.ServletStubImpl.prepareServlet(ServletStubI
mpl.java:773)
at weblogic.servlet.internal.WebAppServletContext.preloadServlet(WebAppS
ervletContext.java:2763)
at weblogic.servlet.internal.WebAppServletContext.preloadServlets(WebApp
ServletContext.java:2708)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAp
pServletContext.java:2681)
at weblogic.servlet.internal.HttpServer.preloadResources(HttpServer.java
:552)
at weblogic.servlet.internal.WebService.preloadResources(WebService.java
:476)
at weblogic.t3.srvr.ServletInitRunner$1.run(ServletInitRunner.java:50)
at weblogic.security.service.SecurityServiceManager.runAs(SecurityServic
eManager.java:744)
at weblogic.t3.srvr.ServletInitRunner.run(ServletInitRunner.java:46)
at java.lang.Thread.run(Thread.java:479)
>




I make entries struts-config.xml for data source

<data-sources>
<!-- configuration for commons BasicDataSource -->
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="oracle.jdbc.driver.OracleDriverr" />
<set-property
property="url"
value="jdbc racle:thin:@22.12.2.10:1521:fc"/>
<!-- value="jdbc ostgresql://localhost/mydatabase" />-->
<set-property
property="username"
value="work" />
<set-property
property="password"
value="work" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="validationQuery"
value="SELECT COUNT(*) FROM IP" />
</data-source>
</data-sources>



java code
----------
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
javax.sql.DataSource dataSource;
java.sql.Connection myConnection=null;

try {
dataSource = getDataSource(request);
System.out.println("dataSource :"+ dataSource);
myConnection = dataSource.getConnection();
System.out.println("myConnection:"+ myConnection);

} catch (SQLException sqle) {
getServlet().log("Connection.process", sqle);
} finally {
try {
myConnection.close();
} catch (SQLException e) {
getServlet().log("Connection.close", e);
}
}


whats wrong wth the above code .I am using oracle 9i database
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Are you sure your driverClassName is valid?

My setting for Oracle 9i is this:

<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="oracle.jdbc.OracleDriver" />
<set-property property="url" value="jdbc racle:thin:@p3:1521 DB" />
<set-property property="username" value="scott"/>
<set-property property="password" value="tiger"/>
</data-source>
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Remove the extra 'r' from the end of your driver name. The oracle.jdbc.driver.OracleDriver class will work.


James Carman, President<br />Carman Consulting, Inc.
ashwin bala
Ranch Hand

Joined: Dec 07, 2009
Posts: 38
hey guys i ve done all connections in public Actionforward execute() in action class.. i can retrieve the one set of values from database and display it the jsp page
public Actionforward execute(Http...........)
{
HttpSession session;

String result="";
while(rs.next())
{
result=rs.getString("name");
}
session.setAttribute("customername",result);
}

in jsp i use getAttribute("customername"); to get the one value from action class.. but i can use it only fr single value please give me a solutions for set of 25 records.. coz i cannot use set and getattrbutes 25 times..
ashwin bala
Ranch Hand

Joined: Dec 07, 2009
Posts: 38
ashwin bala wrote:hey guys i ve done all connections in public Actionforward execute() in action class.. i can retrieve the one set of values from database and display it the jsp page
public Actionforward execute(Http...........)
{
HttpSession session;

String result="";
while(rs.next())
{
result=rs.getString("name");
}
session.setAttribute("customername",result);
}

in jsp i use getAttribute("customername"); to get the one value from action class.. but i can use it only fr single value please give me a solutions for set of 25 records for example around 25 customernames.. coz i cannot use set and getattrbutes 25 times..
mdhar nayani
Greenhorn

Joined: Jul 05, 2013
Posts: 1
Hi Ashwin, i understand your problem. For your better understand why can't you try a new program here: i found a program with good explanation using Struts and JDBC. You can go through this.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts with jdbc example