Created
October 2, 2013 10:35
-
-
Save swapnil-webonise/6791787 to your computer and use it in GitHub Desktop.
Inserting Record In Database Using Servlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package studentMain; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import net.sf.json.JSONObject; | |
import org.json.JSONArray; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.PreparedStatement; | |
public class GapSpecificationDetailsUpdate extends HttpServlet | |
{ | |
private static final long serialVersionUID = 1L; | |
Connection conn; | |
ResultSet rs; | |
PreparedStatement ps; | |
public GapSpecificationDetailsUpdate() | |
{ | |
super(); | |
conn = ConnectionClassForMyProjectWindow.getConnection(); | |
} | |
public void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException | |
{ | |
try | |
{ | |
String nameId = request.getSession().getAttribute("userid").toString(); | |
String FromYear = request.getParameter("txtFromYear"); | |
String ToYear = request.getParameter("txtToYear"); | |
String Reason = request.getParameter("txtReason"); | |
String switchFlag = request.getParameter("txtCheckFlag"); | |
String selectQuery=" select sgd.student_Id,sgd.from_year,sgd.to_year,sgd.gap_description "+ | |
" from student_gap_description sgd "+ | |
" where sgd.student_Id='"+nameId+"'"; | |
String insertQuery = " insert into student_gap_description values(?,?,?,?) "; | |
ps = conn.prepareStatement(selectQuery); | |
rs=ps.executeQuery(); | |
PrintWriter out=response.getWriter(); | |
rs.last(); | |
int size=rs.getRow(); | |
rs.beforeFirst(); | |
if(switchFlag.compareTo("1")==0) | |
{ | |
JSONArray jArray = new JSONArray (); | |
jArray.put(size); | |
while(rs.next()) | |
{ | |
JSONObject jObj = new JSONObject(); | |
jObj.put("from_year",rs.getString("from_year")); | |
jObj.put("to_year",rs.getString("to_year")); | |
jObj.put("gap_description",rs.getString("gap_description")); | |
jArray.put(jObj); | |
} | |
out.print(jArray); | |
} | |
else | |
{ | |
ps = conn.prepareStatement(insertQuery); | |
ps.setString(1, nameId); | |
ps.setString(2, FromYear); | |
ps.setString(3, ToYear); | |
ps.setString(4, Reason); | |
int cnt = ps.executeUpdate(); | |
System.out.println(cnt); | |
out.print(cnt); | |
} | |
} | |
catch(Exception e) | |
{ | |
System.out.println(e); | |
} | |
} | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | |
{ | |
this.process(request, response); | |
} | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | |
{ | |
this.process(request, response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment