Struts Application- Java DataBase Connectivity
Aim:
This post illustrate how to connect struts application with mysql database.
Problem:
1). Create NStudent database in mysql
2). Create NStudent table in mysql
3). To insert the data into the NStudent table in mysql.
Procedure:
Step_1:
Create NStudent Database
use create database database_name;
We use database name as NStudent. Step_2:
Use database name
Use keyword is used to use already created database.
Step_3:
Create NStudent Table
Use Create table table_name(stu_name varchar(40),stu_rollno int);
Step_4:
Create Struts Project (I have used project name as NStudent)
Already I have explained how to create Struts Project using netbean.
Step_5:
Create Folder for
1). Actionforms
2). Actions
3). Vo
4). Bl
5). Dbcon
Step_6:
Create java class for dbcon.
Select dbcon and right click and New and Other. Where you select java under categories and java class under file types and click Next . where New java class window will open. You give java class name.(I have used as Dbcon) and Finish.
You insert below code in Dbcon
package dbcon;
import java.sql.*;
public class Dbcon
{
public static Connection getConnection()throws SQLException
{
Connection con=null;
Statement stmt=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(Exception e)
{
System.out.println("Class Load Error");
}
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost/NStudent","root","sure");
}
catch(Exception e)
{
System.out.println("Connection Create Error");
}
return con;
}
}
Step_7:
Create actionform
I have used studentForm as actionform
Already I have explained how to create actionforms in Strtus application-Fisrt
Hello World.
You insert student_name and rollno;
Create getter and setter for student_name and rollno
package actionforms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class studentForm extends org.apache.struts.action.ActionForm
{
private String student_name;
private int rollno;
public int getRollno()
{
return rollno;
}
public void setRollno(int rollno)
{
this.rollno = rollno;
}
public String getStudent_name()
{
return student_name;
}
public void setStudent_name(String student_name)
{
this.student_name = student_name;
}
}
Step_8:
Create vo(Value Object)
Right click on vo floder.
New and other and java under categories and select java under file types.
You give java class name.(I have used as stuvo).
Finish.
You insert
Private String student_name;
Private int rollno;
Create setter and getter method for student_name and rollno.
Like below.
package vo;
import java.io.Serializable;
public class stuvo implements Serializable
{
private String student_name;
private int rollno;
public int getRollno()
{
return rollno;
}
public void setRollno(int rollno)
{
this.rollno = rollno;
}
public String getStudent_name()
{
return student_name;
}
public void setStudent_name(String student_name)
{
this.student_name = student_name;
}
}
Step_9:
Create bl(Business Logic)
Right click on bl folder.
Select new and other and java and java class.
package bl;
import dbcon.Dbcon;
import vo.stuvo;
import java.sql.*;
public class stubl
{
Connection con=null;
Statement stmt=null;
String msg="";
String sql="";
public String insert(stuvo vo)
{
try
{
con=Dbcon.getConnection();
}
catch(Exception e)
{
System.out.println("Connection Creating Error");
}
try
{
stmt=con.createStatement();
sql="insert into NStudent values('"+vo.getStudent_name()+"','"+vo.getRollno()+"')";
int i=stmt.executeUpdate(sql);
if(i==1)
{
msg="success";
}
else
{
msg="failure";
}
}
catch(Exception e)
{
System.out.println("Error");
}
return msg;
}
}
Step_10:
Create action class.
I have already explained how to create action class.
I have used studentAction as actionform
I have used /student as Action path.
package actions;
import actionforms.studentForm;
import bl.stubl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vo.stuvo;
public class studentAction extends org.apache.struts.action.Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
studentForm sform=(studentForm)form;
stuvo vo=new stuvo();
vo.setStudent_name(sform.getStudent_name());
vo.setRollno(sform.getRollno());
stubl bl=new stubl();
String msg=bl.insert(vo);
if(msg.equals("success"))
{
return mapping.findForward("success");
}
else
return mapping.findForward("failure");
}
}
Step_11:
Configure Struts-config.xml
Step_12:
Create jsp files.
Home.jsp
< %@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" % >
< @ taglib uri="/WEB-INF/struts-html.tld" prefix="html" % >
< @ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" % >
< html >
< head >
< title > JSP Page < /title >
< /head >
< body >
< html:form action="/student" >
Student_name: < html:text property="student_name"/ >
< br >
RollNo: < html:text property="rollno"/ >
< br >
< html:submit value="OK"/ >
< /html:form >
< /body >
< /html >
success.jsp
< html >
< head >
< title > JSP Page < /title >
< /head >
< body >
Success
< /body >
< /html >
Step_13:
Right click on NStudent Project and select properties.
Project Properties-NStudent window will open.
Under categories
Select Libraries
In the Right side
Click Add JAR/Folder
And select mysql-connector (You shoud know where you stored mysql-connector)
You can Download mysql-conector from mysql-connector
And click OK.
Step_14:
Run Home.jsp
Conclusion:
From this Post we have understood how to use jdbc and how to insert date into the table .
You have any query
Post comments.