• 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: DB Connection

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I want to connect to DB for extracting data from oracle DB. Please guide me how to connect to DB to retrieve data?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there !

Try this link and let me know if is of any good to you.
http://struts.apache.org/struts-doc-1.1/faqs/database.html

Jose Cardoso Jr
 
Lalit Vora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I am getting same error every time This is my struts config.xml

I am getting Error HTTP 404 servlet action not available. If i am not using dtasource and changing action class my application runs without datasource and gets hard coded value from getquote method. I want value from db


<struts-config>

<data-sources>
<data-source
type="org.apache.common.dbcp.BasicDataSource">

<set-property property="driverClassName"
vlaue="oracle.jdbc.driver.OracleDriver"/>

<set-property property="url"
vlaue="jdbc racle:thin:
@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myhost)(PORT=1521))(CONNECT_DATA=(SID=orcl)))"/>


<set-property property="username"
vlaue="scott"/>
<set-property property="password"
vlaue="tiger"/>

</data-source>
</data-sources>

<form-beans>

<form-bean name="lookupForm"
type="ch03.LookupForm"/>

</form-beans>


<action-mappings>

<action path="/Lookup"
type="ch03.LookupAction"
name="lookupForm">

<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>

</action-mappings>
<message-resources parameter="MessageResources" />

</struts-config>


This is my action class

public class LookupAction extends Action
{
public Double getQuote(String symbol,
HttpServletRequest request)
throws Exception

{

Double price=null;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
DataSource dataSource=null;

try
{

dataSource = getDataSource(request);
conn= dataSource.getConnection();
stmt= conn.createStatement();
rs= stmt.executeQuery("select * from stocks where" + "symbol='" + symbol +"'");

if (rs.next())
{
double tmp=0;
tmp = rs.getDouble("price");

price= new Double(tmp);
System.err.println("price:" +price);

}

else
{
System.err.println("Symbol not found returning null");
}

}

catch(SQLException e)
{
System.err.println(e.getMessage());
}

finally
{
if (rs!= null)
{

try
{
rs.close();
}

catch (SQLException sqle)
{
System.err.println(sqle.getMessage());
}
rs=null;
}

if (stmt!=null)
{

try{ stmt.close(); }

catch (SQLException sqle)
{
System.err.println(sqle.getMessage());
}
stmt=null;
}

if (conn!=null)
{
try{ conn.close(); }

catch (SQLException sqle)
{
System.err.println(sqle.getMessage());
}
conn=null;
}


}



return price;
}


public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception{


Double price=null;
String target=new String("success");

if(form !=null)
{

LookupForm lookupForm = (LookupForm) form;

String symbol = lookupForm.getSymbol();


price = getQuote(symbol,request);

}

if(price == null)
{
target = new String("failure");
}

else
{
request.setAttribute("PRICE",price);
}

return(mapping.findForward(target));
}


}



Please Guide me its very urgent. I also want to know do we require any driver to place anywhere? Also without using struts config can i directly connect to DB? Please help me

Thanks
Lalit
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<struts-config>

<data-sources>
<data-source
type="org.apache.common.dbcp.BasicDataSource">

<set-property property="driverClassName"
vlaue="oracle.jdbc.driver.OracleDriver"/>

<set-property property="url"
vlaue="jdbc racle:thin:
@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myhost)(PORT=1521))(CONNECT_DATA=(SID=orcl)))"/>

Hi,

Place the classes12.jar in the classpath (if you are using oralce).

change the struts-config.xml in <data-source> section, like this.

<set-property property="url" value="jdbc racle:@thin:myhost:1521 rcl/>

hope it will work.
reply
    Bookmark Topic Watch Topic
  • New Topic