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);
}
}
}


Read more »

Servlet Code to Delete information from SQL database Tomcat server

In this post we will illustrate how to delete 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.

Create a html form delinfo.html

<html>
<head>
<h1><u>student form</u></h1>
</head>
<body bgcolor="DarkSalmon">
<form name=Student method=POST action=http://localhost:8080/examples/servlet/delinfo>

<font face="Copperplate Gothic Bold">ID</font face>:<input type="text" name="t2" size="30"><p><br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>


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

import java.util.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class delinfo extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
try
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
String s2=req.getParameter("t2");
out.println("<html><body><h1>............................servlet deletion working..........:):).......</h1></body></html>");
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>");
if(con!=null)
{
out.println("<html><body><h4>connection established........</h4></body></html>");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("delete from studentinfo where id1='"+s2+"'");
out.println("<html><body><h4>deleted from database.......</h4></body></html>");
stmt.close();
}



con.close();
out.close();


}
catch(Exception e)
{}
}
}

Deleting information from SQL database
Deleting information from SQL database




Read more »

Servlet Code for Searching information from SQL database 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 & Insetion of data into SQL database.

Create a HTML page named searchinfo.html

<html>
<head>
<h1><u>student form</u></h1>
</head>
<body bgcolor="DarkSalmon">
<form name=Student method=POST action=http://localhost:8080/examples/servlet/searchinfo>

<font face="Copperplate Gothic Bold">ID</font face>:<input type="text" name="t2" size="30"><p><br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>


Now Create a Searchinfo.java which has the actual logic for seraching information and displaying it.


import java.util.*;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class searchinfo extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
try
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
String s2=req.getParameter("t2");
out.println("<html><body><h1>............................servlet retrieval working..........:):).......</h1></body></html>");
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>");
if(con!=null)
{
out.println("<html><body><h4>connection established........</h4></body></html>");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from studentinfo where id1='"+s2+"'");
out.println("<html><body><h4>retrieved from database.......</h4></body></html>");
ResultSetMetaData md=rs.getMetaData();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("retrieval servlet");
out.println("</head>");
out.println("<table border=1>");
out.println("<tr><td>student name</td><td>id</td><td>age</td><td>branch</td></tr>");
while(rs.next())
{
for(int i=1;i<md.getColumnCount();i++)
{
out.println("<tr><td>"+rs.getString(i++)+"</td><td>"+rs.getString(i++)+"</td><td>"+rs.getString(i++)+"</td><td>"+rs.getString(i++)+"</td></tr>");
out.println("***");
out.println("</table></body></html>");
}
}

rs.close();
stmt.close();
con.close();
out.close();

}
}
catch(Exception e)
{}
}
}

Searching information from SQL database
Searching information from SQL database

Read more »

Servlet code to insert data into MS SQL database using Tomcat Server

Unlike JSP and PHP Servlet is also server side scripting language. Servlet is Java based, Here this post we are going to illustrate how to add, delete , update and Search data from a form interacting with SQL database.
For consideration we will need tomcat server installed running on the system by default at port 8080 and sql database running on the system.

STEP 1:

Create database in Microsoft sql Query analyser .
Create table with 4 fields viz StudentName , ID , Age and Branch.
->Keep ID as  INT and set it as primary key
->keep StudentName as VARCHAR(100)
->keep age as INTEGER
->Keep Branch as VARCHAR(100)

STEP 2 :

This code is only for addition of information to the database

Create a html page as follows:

studentinfo.html

<html>
<head>
<h1><u>student form</u></h1>
</head>
<body bgcolor="DarkSalmon">
<form name=Student method=POST action=http://localhost:8080/examples/servlet/studentinfo>
<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 page named studentinfo.java which has actual logic to interact with SQL database and insert information.

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class studentinfo 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 running..........:):).......</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="insert into studentinfo values('"+s1+"','"+s2+"','"+s3+"','"+s4+"')";
out.println("<html><body><h3>inserting into 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);
}
}
}

inserting to SQL databse using servlet

Read more »