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 »

JSP Java Code to Search record in SQL database using Apache server


searchstud.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<br></br>
<h2><center>enter stuent details</center></h2>
<form name="searchstud"action="searchstudinfo.jsp"method="post">
<table border="2"align=center>
<tr><td>
<b>enter the id u want to search:</b>
<input type="text"name="id"/>
<br/>
<br/>
</td></tr>
</table>
<center>
<input type="submit"value="submit"/>
<input type="reset"value="reset"/>
</form>
</body>
</html>

searchstudinfo.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<table>
<tr>
<th>name</th>
<th>id</th>
<th>age</th>
<th>branch</th>
</tr>
<%
Connection con=null;
Statement stmt=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
stmt=con.createStatement();
String s2=request.getParameter("id");
%>
<%

ResultSet res=stmt.executeQuery("select * from studentinfo where id1='"+s2+"'");
while(res.next())
{
%>
<tr>
<td><%=res.getString("name1")%></td>
<td><%=res.getString("id1")%></td>
<td><%=res.getString("age")%></td>
<td><%=res.getString("branch")%></td>
</tr>
<%
}
%>
</table>
</body>
</html>


OUTPUT:

Read more »

JSP Java code to Update Record in SQL database using Tomcat server

upstud.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<br></br>
<h2><center>enter stuent details</center></h2>
<form name="upstud"action="upstudinfo.jsp"method="post">
<table border="2"align=center>
<tr><td>
<b>student name:</b>
<input type="text"name="name"/>
<br/>
<br/>
</td></tr>
<tr><td>
<b>id:</b>
<input type="text"name="id"/>
<br/>
<br/>
</td></tr>
<tr><td>
<b>age:</b>
<input type="text"name="age"/>
<br/>
<br/>
</td></tr>
<tr><td>
<b>branch:</b>
<input type="text"name="branch"/>
<br/>
<br/>
</td></tr>
</table>
<center>
<input type="submit"value="submit"/>
<input type="reset"value="reset"/>
<input type="button"value="delete"/>
</form>
</body>
</html>

upstudinfo.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%
Connection con=null;
Statement stmt=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
try
{
String s1=request.getParameter("name");
String s2=request.getParameter("id");
String s3=request.getParameter("age");
String s4=request.getParameter("branch");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
stmt=con.createStatement();
stmt.executeUpdate("update studentinfo set name1='"+s1+"',age='"+s3+"',branch='"+s4+"'where id1='"+s2+"'");
%>
data updated successfully...........
<%
stmt.close();
stmt=null;
}
finally
{
if(con!=null)
{
con.close();
}
}
%>
</body>
</html>

OUTPUT:


Read more »

JSP Java code to Delete Record using SQL and apache server

delstud.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<br></br>
<h2><center>enter stuent details</center></h2>
<form name="delstud"action="delstudinfo.jsp"method="post">
<table border="2"align=center>
<tr><td>
<b>id:</b>
<input type="text"name="id"/>
<br/>
<br/>
</td></tr>
</table>
<center>
<input type="submit"value="submit"/>
<input type="reset"value="reset"/>
<input type="button"value="delete"/>
</form>
</body>
</html>


delstudinfo.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%
Connection con=null;
Statement stmt=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
try
{

String s2=request.getParameter("id");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
stmt=con.createStatement();
stmt.executeUpdate("delete from studentinfo where id1='"+s2+"'");
%>
data deleted successfully...........
<%
stmt.close();
stmt=null;
}
finally
{
if(con!=null)
{
con.close();
}
}
%>
</body>
</html>

OUTPUT:


Read more »

JSP Java Code to Post Data to SQL database using Tomcat Server

JSP is widely used Java based server side scripting language . Here in this tutorial I am going to give you code which  will post data from a html form. before you run through code ensure that the tomcat server is installed or you can download it directly from the official website also download and install Microsoft server 2000.Go to the database console and

1.Create  database named "student1".

2.Create table with four fields NAME,ID,AGE and BRANCH , keep ID as primary key.

First we began with creating a html form. For consideration here we are going to create a form for entering student details such as NAME,ID,AGE and BRANCH. The following is the code for form . Just name it addstud.jsp . After submit button is pressed  then the details are forwarded to addstudinfo.jsp which connects to the database and enters the data into mysql database.

addstud.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<br></br>
<h2><center>enter stuent details</center></h2>
<form name="addstud"action="addstudinfo.jsp"method="post">
<table border="2"align=center>
<tr><td>
<b>student name:</b>
<input type="text"name="name"/>
<br/>
<br/>
</td></tr>
<tr><td>
<b>id:</b>
<input type="text"name="id"/>
<br/>
<br/>
</td></tr>
<tr><td>
<b>age:</b>
<input type="text"name="age"/>
<br/>
<br/>
</td></tr>
<tr><td>
<b>branch:</b>
<input type="text"name="branch"/>
<br/>
<br/>
</td></tr>
</table>
<center>
<input type="submit"value="submit"/>
<input type="reset"value="reset"/>
<input type="button"value="delete"/>
</form>
</body>
</html>

addstudinfo.jsp

<%@page session="true"import="java.io.*"%>
<html>
<body bgcolor="#d0d0d0">
<br></br>
<br></br>
<%@page import="java.sql.*"%> //imports 
<%@page import="java.util.*"%>
<%
Connection con=null; 
Statement stmt=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","username","password");
try
{
String s1=request.getParameter("name");
String s2=request.getParameter("id");
String s3=request.getParameter("age");
String s4=request.getParameter("branch");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:student1","sa","sa");
stmt=con.createStatement(); //opening a connection
stmt.executeUpdate("insert into studentinfo(name1,id1,age,branch)values('"+s1+"','"+s2+"','"+s3+"','"+s4+"')");
%>
//inserting into database

data enterd successfully...........
<%
stmt.close();//close conncetion
stmt=null;
}
finally
{
if(con!=null)
{
con.close();
}
}
%>
</body>
</html>




Keep both the files in the tomcat server and then run it from the browser on port 8000 (or as configured on the system)
.
Read more »

Rolling ball Computer Animation Program using C++ VB 6.0

The Animation program is Rolling Ball over a horizontal line . The ball start rolling at the start of the line and moves till the end of line length.The program is implemented using C++ MS Visual studio , MFC application.

PROGRAM:
                                             
void CAnimation1View::OnDraw(CDC* pDC)
{
	CAnimation1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
    POINT ball[2]={25,150,75,100};
	pDC->Ellipse(ball[0].x,ball[0].y,ball[1].x,ball[1].y);
	
	for(int i=0;i<600;i++)
	{
		pDC->FloodFill(50,50,RGB(100,100,100));
		pDC->Ellipse(ball[0].x++,ball[0].y,ball[1].x++,ball[1].y);
		pDC->MoveTo(10,150);
	pDC->LineTo(600,150);
		Sleep(1);
	}

}

OUTPUT:


Read more »

Computer Animation Bouncing Ball Program using MFC C++ code

Basic Computer Animation Program using MS Visual Studio 6.0 with MFC application.Here in this program we make use of flood fill concept . The function is very simple indicating just coordinates which has to be displayed on the screen.

 PROGRAM:
void CBouncingballView::OnDraw(CDC* pDC)
{
	CBouncingballDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
POINT ball[2]={25,150,75,100};
	pDC->Ellipse(ball[0].x,ball[0].y,ball[1].x,ball[1].y);
	for(int k=0;k<600;k++)
	{
		pDC->FloodFill(50,50,RGB(100,100,100));
        pDC->MoveTo(10,150);
	    pDC->LineTo(600,150);
		for(int i=0;i<250;i++)
		{
		for(int i=0;i<250;i++)
	{
		pDC->FloodFill(50,50,RGB(100,100,100));
		pDC->Ellipse(ball[0].x,ball[0].y++,ball[1].x,ball[1].y++);
	    pDC->MoveTo(10,400);
	    pDC->LineTo(600,400);
	}
	for(int j=250;j>=1;j--)
    {	
		pDC->FloodFill(50,50,RGB(100,100,100));
		pDC->Ellipse(ball[0].x++,ball[0].y--,ball[1].x++,ball[1].y--);
        pDC->MoveTo(10,400);
	    pDC->LineTo(600,400);
	    Sleep(1);
	}
		}
	}
}


OUTPUT:



Read more »