Servlet Code to Update records in SQL database using Tomcat server

In this post we will illustrate how to Search and display information from database.
This post is continued from previous post have a look on how to do:

1. Database settings and Insetion of data into SQL database.
2. Searching information from Database
3. Deleting informstion from database

Create a html form upinfo.html

<html>
<head>
<h1><u>student form</u></h1>
</head>
<body bgcolor="DarkSalmon">
<form name=Student method=POST action=http://localhost:8080/examples/servlet/upinfo>
<font face="Copperplate Gothic Bold">STUDENT NAME</font face>:<input type="text" name="t1" size="30"><p><br>
<font face="Copperplate Gothic Bold">ID</font face>:<input type="text" name="t2" size="30"><p><br>
<font face="Copperplate Gothic Bold">AGE</font face>:<input type="text" name="t3" size="30"><p><br>
<font face="Copperplate Gothic Bold">BRANCH</font face>:<input type="text" name="t4" size="30"><p><br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>


Now Create a up.java which has the actual logic for deleting information from database.

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class upinfo extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
doGet(req,res);
}
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
String s1=req.getParameter("t1");
String s2=req.getParameter("t2");
String s3=req.getParameter("t3");
String s4=req.getParameter("t4");
out.println("<html><body><h1>............................servlet updating..........:):).......</h1></body></html>");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
out.println("<html><body><h4>connectiopn established........</h4></body></html>");
String query="update studentinfo set name1='"+s1+"',age='"+s3+"',branch='"+s4+"'where id1='"+s2+"'";
out.println("<html><body><h3>updating from database.......</h3></body></html>");
Statement stmt=con.createStatement();
out.println("<h2>:):):)</h2>");
ResultSet rs=stmt.executeQuery(query);
out.println("<html><body><h4>thank you for registrating.......</h4></body></html>");
con.close();
}
catch(Exception e)
{
System.out.println("Error in login:"+e);
}
}
}


0 comments:

Post a Comment

If You Are Asking Some Question On This Comment Then Click On Subscribe by email Link