This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Struts and the fly likes Help in connecting to oracle10 database using struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Help in connecting to oracle10 database using struts" Watch "Help in connecting to oracle10 database using struts" New topic
Author

Help in connecting to oracle10 database using struts

Rahul B. Shah
Greenhorn

Joined: Jan 17, 2011
Posts: 24
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="jdbcracle: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.
Mahendra Suda
Greenhorn

Joined: Apr 22, 2011
Posts: 18


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.

http://www.java-connect.com
Rahul B. Shah
Greenhorn

Joined: Jan 17, 2011
Posts: 24
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

Joined: Apr 22, 2011
Posts: 18


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

Joined: Jan 17, 2011
Posts: 24
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

Joined: Apr 22, 2011
Posts: 18
javax.sql.DataSource is the correct one., What version of Struts you are using?
Rahul B. Shah
Greenhorn

Joined: Jan 17, 2011
Posts: 24
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="jdbcracle:thin:@" + serverName + ":" + portNo + ":" + sid;
userName="oimuser_jadhavj";
psswd="password";
System.out.println("<<<"+url+">>>");

con = DriverManager.getConnection(url,userName,psswd);
// con = DriverManager.getConnection("jdbcracle: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

Joined: Apr 22, 2011
Posts: 18
The code seems to correct only, Could you trace the exception you are getting and paste it here.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help in connecting to oracle10 database using struts