• 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

Help in connecting to oracle10 database using struts

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am new to struts & trying to connect oracle database,so i added following code in struts-config.xml

<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property value="oracle.jdbc.driver.OracleDriver" property="driverClassName"/>
<set-property value="jdbc:oracle:thin:@10.88.176.155:1521" property="url"/>
<set-property value="oimuser_jadhavj" property="username"/>
<set-property value="password" property="password"/>
</data-source>
</data-sources>

Then in action class i am trying to get connection from datasource so after searching in google i found that we have to use

dataSource = getDataSource(request);//problem
myConnection = dataSource.getConnection();
Statement stmt=myConnection.createStatement();
ResultSet rst=stmt.executeQuery("select username from test");

But i cannot fing getDataSource() method,so anyone can please guide how to work this out or any one has link where struts & database
examples are mentioned.

Thank-You.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


and



Above are the methods available in struts 1.x Action class.

The getDataSource() method is the part of struts's Action class, You can directly call this method inside any of your Project class which is extending Struts's org.apache.struts.action.Action class.

I think this may help you.
 
Rahul B. Shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mahendra,
Thanks for reply & Please have a look at my code & i think i have imported all necessary packages ,but still not able to work it out

package freshstrutsaction;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;

import freshstrutsform.LoginForm;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class LoginAction extends Action
{
private String usrName="";

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
{
LoginForm loginForm = (LoginForm)form;
usrName = loginForm.getUserName();
System.out.println(usrName);
if(loginForm.getUserName().equalsIgnoreCase(loginForm.getPassword()))
{
return mapping.findForward("success");
}
else
{
return mapping.findForward("failure");
}
}

public void createConnection()
{
DataSource dataSource;
Connection con = null;
try
{
dataSource = getDataSource(request);//problem

}
catch(SQLException sqle)
{

}
}
}
 
Mahendra Suda
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The problem in above code, You are passing 'request' object, But form where you are getting this object? either you directly use inside the execute method and pass the HttpServletRequest object to getDataSource() method Or Pass the HttpServletRequest object to your createConnection() method..

After refactoring your code the method createConnection() should look like this.


 
Rahul B. Shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mahendra,
I tried the way which you suggested & it shows message as "The method getDataSource(HttpServletRequest) is undefined for the type LoginAction" i.e it cannot find getDataSource() method & i guess i gave imported wrong package (import javax.sql.DataSource),but
other packages which has datasource are javax.activation & sun.jdbc.odbc.ee ,so which to use ?
Thanks for replying.
 
Mahendra Suda
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javax.sql.DataSource is the correct one., What version of Struts you are using?
 
Rahul B. Shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mahendra,
For Database Connection I have created following java file

InitDbase.java

package freshstrutsaction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class InitDbase
{
private Connection con = null;

public InitDbase()
{
createDBConnection();
}

public void createDBConnection()
{
String serverName,portNo,sid,url,userName,psswd;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("<<<Classes Loaded>>>");
serverName="10.88.176.155";
portNo="1521";
sid="TRAINING";
url="jdbc:oracle:thin:@" + serverName + ":" + portNo + ":" + sid;
userName="oimuser_jadhavj";
psswd="password";
System.out.println("<<<"+url+">>>");

con = DriverManager.getConnection(url,userName,psswd);
// con = DriverManager.getConnection("jdbc:oracle:thin:@10.88.176.155:1521:TRAINING","oimuser_jadhavj","password");
if(con == null)
{
MessageBox("Could Not Connect To Database");
System.out.println("<<<Could Not Connect To Database>>>");
}
else
{
MessageBox("Connected To Database");
System.out.println("<<<Connected To Database>>>");
}
}
catch(ClassNotFoundException cnfe)
{
MessageBox("ClassNotFound..."+cnfe.toString());
}
catch(SQLException sqle)
{
MessageBox("SQLException :-"+sqle.toString());
}
}

public void MessageBox(Object obj)
{
JOptionPane.showMessageDialog(null,obj.toString());
}

public Connection getCon()
{
return con;
}
public void setCon(Connection conn)
{
this.con = conn;
}
}
---------------------------------------------------------------------------------------------------------------------------------------

With above code i can establish connection with MySQL & if I use Oracle then it does not connects ,it comes at line
con = DriverManager.getConnection(url,userName,psswd);
and then nothing happens,but when i tried same credentials of oracle with java application, it connects,but for above application for struts
it does not connect & i have ojdb14.jar in lib folder .
 
Mahendra Suda
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code seems to correct only, Could you trace the exception you are getting and paste it here.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic