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 »

Implementing ellipse Program using computer Graphics C++ Code

This Program illustrates how to draw a circle without using the direct function. The Program is written in C++ language using  Microsoft Developer Studio , Format Version 6.00.This part of programming belongs to basic Computer graphics.Use MS visual studio 6.0 with MFC application.

PROGRAM:
#include "stdafx.h"
#include "ellipse.h"

#include "ellipseDoc.h"
#include "ellipseView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
#define ROUND(a)((int)(a+0.5))
static char THIS_FILE[] = __FILE__;
#endif

void ellipseMidPoint(int Xcenter,int Ycenter,int Rx,int Ry,CDC* pDC,COLORREF color)
{
	int Rx2=Rx*Rx;
	int Ry2=Ry*Ry;
	int twoRx2=2*Rx2;
	int twoRy2=2*Ry2;
	int p;
	int x=0;
	int y=Ry;
	int px=0;
	int py=twoRx2*y;
    void ellipsePlotPoints(int,int,int,int,CDC* pDC,COLORREF color);
	
	ellipsePlotPoints(Xcenter,Ycenter,x,y,pDC,color);
    
	p=ROUND(Ry2-(Rx2*Ry)*(0.25*Rx2));
	
	while(px<py)
	{
		x++;
		px+=twoRy2;
		if(p<0)
			p+=Ry2*px;
		else
		{
			y--;
			py-=twoRx2;
			p+=Ry2+px-py;
		}
		ellipsePlotPoints(Xcenter,Ycenter,x,y,pDC,color);
	}
	p=ROUND(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
	while(y>0)
	{
		y--;
	py-=twoRx2;
	if(p>0)
		p+=Rx2-py;
	else
	{
		x++;
		px+=twoRy2;
		p+=Rx2-py+px;
	}
	ellipsePlotPoints(Xcenter,Ycenter,x,y,pDC,color);
}
}
void ellipsePlotPoints(int Xcenter,int Ycenter,int x,int y,CDC* pDC,COLORREF color)
{
	pDC->SetPixel(Xcenter+x, Ycenter+y,color);
	pDC->SetPixel(Xcenter-x, Ycenter+y,color);
	pDC->SetPixel(Xcenter+x, Ycenter-y,color);
	pDC->SetPixel(Xcenter-x, Ycenter-y,color);
}
void CEllipseView::OnDraw(CDC* pDC)
{
	CEllipseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
		COLORREF color=RGB(0,0,0);
	ellipseMidPoint(150,70,13,50,pDC,color);

	// TODO: add draw code for native data here
}


OUTPUT:



Read more »

Implementing Missionaries and cannibals problem in C++ Program code

INTRODUCTION:

In the missionaries and cannibals problem(AI), three missionaries and three cannibals must cross a river using a boat which can carry at most two people, under the constraint that, for both banks, if there are missionaries present on the bank, they cannot be outnumbered by cannibals (if they were, the cannibals would eat the missionaries.) The boat cannot cross the river by itself with no people on board.The Program is implemented using C++ Programming language.

PROGRAM:

#include<iostream>
#include<list>
#include<stack>
using namespace std;
struct StateSTR
{
  int Mlhs;        //nr missionaries on LHS    of river
  int Clhs;        //nr cannibals    on LHS    of river  
  int pos;         //boat on LHS (0) or RHS(1) of river
  int Mrhs;        //nr missionaries on RHS    of river
  int Crhs;        //nr cannibals    on RHS    of river
  StateSTR *parent;//pointer to parent state
  int opUsed;
  bool operator==(const StateSTR & rhs) const
  {return ((Mlhs == rhs.Mlhs) && (Clhs == rhs.Clhs) &&
            (Mrhs == rhs.Mrhs) && (Crhs == rhs.Crhs) &&
            (pos  == rhs.pos));
  }
};
ostream & operator<< (ostream & out, const StateSTR & s)
{out << "Mlhs:" << s.Mlhs << endl;
  out << "Clhs:" << s.Clhs << endl;    
  out << "Boat:" << s.pos  << endl;  
  out << "Mrhs:" << s.Mrhs << endl;
  out << "Crhs:" << s.Crhs << endl << endl;      
  return out;
}
bool validState(StateSTR *S)
{if(( (*S).Clhs < 0) || ( (*S).Clhs > 3)) return false;
  if(( (*S).Crhs < 0) || ( (*S).Crhs > 3)) return false;
  if(( (*S).Mlhs < 0) || ( (*S).Mlhs > 3)) return false;
  if(( (*S).Mrhs < 0) || ( (*S).Mrhs > 3)) return false;
  if(( (*S).pos != 0) && ( (*S).pos!= 1))  return false;  
  if( (( (*S).Clhs >  (*S).Mlhs) && ( (*S).Mlhs > 0)) ||
      (( (*S).Crhs >  (*S).Mrhs) && ( (*S).Mrhs > 0)) ) 
    return false;
  return true;
}


//          m c side(0=left,1=right)

//case 0: C(0,1,0) --> carry one cannibal    to LHS
//case 1: C(0,2,0) --> carry two cannibals   to LHS
//case 2: C(1,0,0) 
//case 3: C(2,0,0) 
//case 4: C(1,1,0) 
//case 5: C(0,1,1) 
//case 6: C(0,2,1) 
//case 7: C(1,0,1)  --> carry one missionary to RHS
//case 8: C(2,0,1) 
//case 9: C(1,1,1) 
StateSTR * nextState(StateSTR *Z, const int j)
{StateSTR * S = new StateSTR();
  (*S)         = (*Z);
  (*S).opUsed  = j;
  switch (j)
  {
    case 0: {  (*S).pos  -= 1; 
               (*S).Mlhs += 0;
               (*S).Clhs += 1;
               (*S).Mrhs -= 0;
               (*S).Crhs -= 1;}
            break;
    case 1: {  (*S).pos  -= 1;
               (*S).Mlhs += 0;
               (*S).Clhs += 2;
               (*S).Mrhs -= 0;
               (*S).Crhs -= 2;}
            break;  
    case 2: {  (*S).pos  -= 1;
               (*S).Mlhs += 1;
               (*S).Clhs += 0;
               (*S).Mrhs -= 1;
               (*S).Crhs -= 0;}
            break;  
    case 3: {  (*S).pos  -= 1;
               (*S).Mlhs += 2;
               (*S).Clhs += 0;
               (*S).Mrhs -= 2;
               (*S).Crhs -= 0;}
            break;  
    case 4: {  (*S).pos  -= 1;
               (*S).Mlhs += 1;
               (*S).Clhs += 1;
               (*S).Mrhs -= 1;
               (*S).Crhs -= 1;}
            break;  
    case 5: {  (*S).pos  += 1;
               (*S).Mrhs += 0;
               (*S).Crhs += 1;
               (*S).Mlhs -= 0;
               (*S).Clhs -= 1;}
            break;  
    case 6: {  (*S).pos  += 1;
               (*S).Mrhs += 0;
               (*S).Crhs += 2;
               (*S).Mlhs -= 0;
               (*S).Clhs -= 2;}
            break;  
    case 7: {  (*S).pos  += 1;
               (*S).Mrhs += 1;
               (*S).Crhs += 0;
               (*S).Mlhs -= 1;
               (*S).Clhs -= 0;}
            break;              
    case 8: {  (*S).pos  += 1;
               (*S).Mrhs += 2;
               (*S).Crhs += 0;
               (*S).Mlhs -= 2;
               (*S).Clhs -= 0;}
            break;                          
    case 9: {  (*S).pos  += 1;
               (*S).Mrhs += 1;
               (*S).Crhs += 1;
               (*S).Mlhs -= 1;
               (*S).Clhs -= 1;}
            break;                                      
  }
  return S;
}

bool notFound(StateSTR *Y, list<StateSTR *>  OPEN,

                           list<StateSTR *>  CLOSED)
{                          
  list<StateSTR*>::iterator itr1 = OPEN.begin();
  list<StateSTR*>::iterator itr2 = CLOSED.begin();
  for(; itr1 != OPEN.end()  ; itr1++)
    if( (*(*itr1)) == (*Y) ) break;
  for(; itr2 != CLOSED.end(); itr2++)
    if( (*(*itr2)) == (*Y) ) break;

  if( (itr1 == OPEN.end()) && (itr2 == CLOSED.end()) )

    return true;                           
  return false;                        
}                          
                                                                            
void addChildren(list<StateSTR *> & OPEN, 
                 list<StateSTR *> & CLOSED,
                 StateSTR      * Y )
{
  StateSTR *tState;
  for(int i = 0; i < 10; i++)
  {
    tState = nextState(Y, i);
    if( (validState(tState)) && 
                             (notFound(tState, OPEN, CLOSED)) )
    {
      (*tState).parent = Y;
      OPEN.push_front(tState);
    }
    else
      delete tState;
  }
  return;
}

void printOP(int n)

{
  switch (n)
  {
    case 0:  cout << "C(0,1,0)" << endl; break;
    case 1:  cout << "C(0,2,0)" << endl; break;
    case 2:  cout << "C(1,0,0)" << endl; break;
    case 3:  cout << "C(2,0,0)" << endl; break;
    case 4:  cout << "C(1,1,0)" << endl; break;
    case 5:  cout << "C(0,1,1)" << endl; break;
    case 6:  cout << "C(0,2,1)" << endl; break;
    case 7:  cout << "C(1,0,1)" << endl; break;
    case 8:  cout << "C(2,0,1)" << endl; break;
    case 9:  cout << "C(1,1,1)" << endl; break;
  }
}

int main() 

{
cout<<"\nMISSIONARIES AND CANNIBALS";
  bool searchResult = false;
  stack<int> opsUsed;
  StateSTR START    = {3,3,0,0,0,NULL,-1};
  StateSTR GOAL     = {0,0,1,3,3,NULL};
  StateSTR *X;
  StateSTR *tempState;
  list<StateSTR *> OPEN;
  list<StateSTR *> CLOSED;
  OPEN.push_front(&START);
  
  while(!OPEN.empty())
  {
    X = OPEN.front(); //stack-like operation
    OPEN.pop_front(); //
    if ((*X) == GOAL)
    {
      searchResult = true;
      break;
    }
    else
    { 
      addChildren(OPEN, CLOSED, X);
      CLOSED.push_back(X);
    }
  }
  //Display results
  if(searchResult == true)
  {
    cout << endl << "PATH" << endl;
    for(StateSTR * p = X; p!= NULL; p = (*p).parent)
      opsUsed.push((*p).opUsed);
  }
  while(!opsUsed.empty())
  {
    printOP(opsUsed.top());
    opsUsed.pop();
  }
  cout << endl;
  return 0;
}

Read more »

Implementing Symmetric Key Encryption technique Using C++ program

INTRODUCTION:

Symmetric Key encryption technique uses only one key to encrypt/decrypt a message. This program is implemented in C++ .

PROGRAM:

#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{ 
 int key1,key2;
 char s,wish;
 int choice;
 printf("\nExpt No.3 \tSYMMETRIC ENCRYPTION AND DECRYPTION\t18/02/2013");
 do {
 cout<<"\n1:Sender side\n2:Receiver side\n3:exit(0)";
 cout<<"\nEnter your choice:";
 cin>>choice;
 
 printf("\nAt Sender side:");
 printf("\nEnter the key:");
 scanf("%d",&key1);

 printf("\nAt Receiver side:");
 printf("\nEnter the key:");
 scanf("%d",&key2);
 printf("\nEnter the message:");
  s=getchar();
 
 if(key1==key2)
 {
  printf("\nKeys Match..you can proceed communication");

 }
 else
 {
  printf("\nKeys do not match...connection lost.....");
  exit(0);
 }
 switch(choice)
 {
 case 1:
 

  //  printf("\nEncrypted text is: ");
    while(s!='\n')
    {
     if(s==' ')
      putchar(s);
     else
     {
      putchar(s+key1);
     }
     s=getchar();
    }
    putchar(s);
    break;
    case 2:
     
     while(s!='\n')
     {
      if(s==' ')
       putchar(s);
      else{
       putchar(s-key2);
      }
      s=getchar();
     }
     putchar(s);
     break;
    default:
     exit(0);
 }
    printf("\nDo you want to continue communnication:?(y or Y)");
    scanf("%c",&wish);
    } while(wish=='y'||wish=='Y');
  return 0;
}



OUTPUT:


Expt No.3       SYMMETRIC ENCRYPTION AND DECRYPTION     18/02/2013
1:Sender side
2:Receiver side
3:exit(0)
Enter your choice:1

At Sender side:
Enter the key:4

At Receiver side:
Enter the key:4 symmetric

Enter the message:
Keys Match..you can proceed communication w}qqixvmg

Do you want to continue communnication:?(y or Y)y

1:Sender side
2:Receiver side
3:exit(0)
Enter your choice:2

At Sender side:
Enter the key:4

At Receiver side:
Enter the key:4 w}qqixvmg

Enter the message:
Keys Match..you can proceed communication symmetric

Do you want to continue communnication:?(y or Y)n
Read more »