• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

struts with jdbc example

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 111
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remove the extra 'r' from the end of your driver name. The oracle.jdbc.driver.OracleDriver class will work.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic