Struts 2.x – Oracle Database Connection

In this example, I have mentioned each and every point clear that you need to implement in order to use database server such as Oracle or MySQL with your Struts 2.x web applications without the use of JPA. This is simple and widely used techniques of database connection in case you are not aware of JPA for a database connection or you don’t want to use JPA.

I am here describing each and every point:

1. Create a Struts 2.x project using the suitable IDE of your choice (Here I’m using Netbeans IDE 7.2). If you are not aware of how to create projects in Struts 2.x using Netbeans as IDE? click on the link.

2. Writer the following code in your index.jsp page(create it if not present):

<%@taglib uri=”/struts-tags” prefix=”s” %>
<%@page contentType=”text/html” pageEncoding=”UTF-8″%>

Hello World!

< /html>

3. Now make its entry into struts.xml file which is present under configurations option.
“-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
“http://struts.apache.org/dtds/struts-2.0.dtd”>
<struts>
<!– Configuration for the default package. –>
<package name=”default” extends=”struts-default”>
<action name=”saveme” class=”action.SaveMe”>
<result name=”success”>index.jsp</result>
<result name=”input”>index.jsp</result>
</action>
</package>
</struts>
4.  Create a package action under source packages option. Create the following java class there.

 —–   SaveMe.java       ——

package action;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class SaveMe {
private String name;
private String mobile;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMobile() {
return mobile;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String execute(){
Connection con=null;
try{
try{
con=DBCon.getConnection();
PreparedStatement pst=(PreparedStatement) con.prepareStatement(“INSERT INTO table_name VALUES (?, ?)”);
pst.setString(1, name);
pst.setString(2, mobile);
pst.executeUpdate();
}finally{
con.close();
}
}catch(Exception e){
e.printStackTrace();
}
return “success”;
}
}

 ———DBCon.java———

package action;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
class DBCon {
private static Connection con;
static{
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
}catch(Exception e){
e.printStackTrace();
}
}

public static Connection getConnection() throws Exception {
if((con==null)||(con.isClosed())){
con=(Connection) DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”, “Oracle_user_name”, “pasword”);
return con;
}
return con;
}

}

 That’s it what we need to do. Please make sure you have installed Oracle database server and you have the username and password of the default database which is me and have created a table there with two columns of varchar type i.e. name and mobile. Please update table_name, Oracle_user_name, and password in the java code
Get through my previous Struts post if you are creating a web application in struts for the first time. Because in order to develop Struts 2.x Web application you need to have an IDE e.g: Netbeans IDE, its plugins to support Struts 2.x, and ojdbc14.jar should be placed inside the lib folder in case you are using oracle as a database server. The plugins and the jar file can be downloaded from here or http://oxyzium.blogspot.in/p/downloads.html 
To download Netbeans IDE visit www.netbeans.org