Insert values into database

Wednesday, March 3, 2010 Posted by Jagadeesh Manne
in this post we will call insertdetails method in servlet
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.UserBean;

/**
*
* @author Jagadeesh
*/
public class DBClass {
public Connection createConnection() throws ClassNotFoundException,SQLException
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.
getConnection("jdbc:mysql://localhost:3306/userdbase", "root", "root");
return connection;
}
//we get values from servlet by passing bean object to insertdetails method
public int insertDetails(UserBean bb) throws SQLException, ClassNotFoundException
{
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("insert into userdetails values(?,?,?,?)");
//set values to prepared statement object by getting values from bean object
pstmt.setString(1,bb.getUserName());
pstmt.setString(2,bb.getDateOfBirth());
pstmt.setString(3,bb.getEmail());
pstmt.setString(4,bb.getPhoneNo());
int i = pstmt.executeUpdate();
return i;

}
public List getAlldetails()throws SQLException, ClassNotFoundException
{
//here we will write code to get all records from database
Connection con = createConnection();
PreparedStatement pstmt = con.prepareStatement("select * from userdetails");
ResultSet rs = pstmt.executeQuery();
List list = new ArrayList();
while(rs.next())
{
UserBean ubean = new UserBean();
ubean.setUserName(rs.getString(1));
ubean.setDateOfBirth(rs.getString(2));
ubean.setEmail(rs.getString(3));
ubean.setPhoneNo(rs.getString(4));
list.add(ubean);

}
return list;
}
/* public UserBean getDetails(int uid)throws SQLException, ClassNotFoundException
{
//here we will write code to get a single record from database
}
public int UpateDetails(int uid)throws SQLException, ClassNotFoundException
{
//here we will write code to update a record
}*/
}


now call that insertdetails method in servlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UserBean;
import validation.ValidateForm;
import database.DBClass;
/**
*
* @author Jagadeesh
*/
public class ControllerServlet extends HttpServlet {

//declare values to get form values from jsp page
String userName;
String dateOfBirth;
String email;
String phoneNo;
String action;

UserBean bean = new UserBean();
ValidateForm validateform = new ValidateForm();
DBClass dbobject = new DBClass();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
//get the values from jsp page
userName = request.getParameter("userName");
dateOfBirth = request.getParameter("dateOfBirth");
email = request.getParameter("email");
phoneNo = request.getParameter("phoneNo");
action = request.getParameter("action");
if(action.equals("submit"))
{
//set values to bean.For this call below method
setValuesToBean();

//check all form values are valid or not. send bean object
UserBean checkedbean = validateform.validateData(bean);
if(!checkedbean.getIsValid())
{
//if data is invalid.set bean object in request and pass that request to
//insertupdate.jsp using forward
request.setAttribute("error",checkedbean);
RequestDispatcher rd = request.getRequestDispatcher("insertupdate.jsp");
rd.forward(request, response);
//now display errors in that jsp page
}
else
{
//using DBClass object call insertDetails method and pass bean object
dbobject.insertDetails(bean);
List list = dbobject.getAlldetails();
request.setAttribute("list", list);
//forward to insertupdate page using requestdispatcher
RequestDispatcher rd= request.getRequestDispatcher("view.jsp");
//display a message to client.store message in request object
//forwarding to jsp
rd.forward(request, response);

}
}
}
catch(Exception e)
{
out.println(e);
}
finally {
out.close();
}
}
//this method is used to setvalues to bean
public void setValuesToBean()
{
bean.setUserName(userName);
bean.setDateOfBirth(dateOfBirth);
bean.setEmail(email);
bean.setPhoneNo(phoneNo);

}


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


public String getServletInfo() {
return "Short description";
}

}

Labels:

Post a Comment