Home>

1. View:

<%@page contenttype="text/html;charset=gb2312"%>
<%@page import="java.sql. *"%>
<html><style type="text/css">
<!-
body {
  background-color:#99ccff;
}
->
</style>
<body>
<font color="#ffffff">
<center>
<%connection con;
class.forname ("com.mysql.jdbc.driver");
con=drivermanager.getconnection ("jdbc:mysql://localhost:3306/student", "root", "123456");
statement sql;
resultset rs;
try
{
sql=con.createstatement ();
rs=sql.executequery ("select * from student");
out.print ("<table border>");
out.print ("<tr><td colspan=5 align=center>candidate data</td></tr>");
out.print ("<tr><td colspan=5 align=center><a href =" add.jsp "target =" _ self ">Add candidate information</a></td><;/tr>");
out.print ("<tr>");
out.print ("<td width=50>" + "name");
out.print ("<td width=100>" + "Age");
out.print ("<td width=100>" + "birth date");
out.print ("<td width=100 colspan=2>" + "operation");
out.print ("</tr>");
while (rs.next ())
{out.print ("<tr>");
out.print ("<td>" + rs.getstring (2) + "</td>");
out.print ("<td>" + rs.getstring (3) + "</td>");
out.print ("<td>" + rs.getstring (4) + "</td>");
string idstr=rs.getstring (1);
out.print ("<td><a href =" delete.jsp?id="+ idstr +" ">delete</a></td>");
out.print ("<td><a href =" update.jsp?id="+ idstr +" ">Modify</a></td>");
out.print ("</tr>");
}
out.print ("</table>");
con.close ();
}
catch (sqlexception e1)
{
out.print ("sql exception !!!");
}
%>
</center>
</body>
</html>

2. add:

<%@page contenttype="text/html;charset=gb2312"%>
<html><head>
<style type="text/css">
<!-
body {
  background-image:url ();
  background-color:#ccccff;
}
.style5 {font-family:"courier new", courier, monospace;font-size:14px;}
.style6 {
  font-family:"courier new", courier, monospace;
  font-size:24px;
}
->
</style>
<meta http-equiv="content-type" content="text/html;charset=gb2312"></head>
<body>
<font size=2>
<p align="center">Add candidate information</p>
<center>
<form action="insert.jsp" name=form>
<table>
  <tr><td height="36"><span>Name:</span></td>
  <td><input name="name" type=text size="15"></td></tr>
  <tr>
   <td height="36"><span>Age:</span></td>
  <td><input name="age" type=text size="15"></td></tr>
  <tr>
   <td height="36"><span>birth date:</span></td>
  <td><input name="birth" type=text size="15"></td></tr>
 </table>
 <table width="165">
  <tr><td width="42" wnameth="42"><input type=submit name="g" value="add"></td>
   <td width="28" wnameth="50"></td>
   <td width="42" wnameth="50"><input type="reset" name="h" value="reset"></td>
   <td width="33" wnameth="42"></td>
  </tr>
</table>
</form></center>
</body></html>

3. delete delete:

<%@page contenttype="text/html;charset=gb2312"%>
<%@page import="java.sql. *"%>
<html>
<head>
<title>delete operation</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312"><style type="text/css">
<!-
body {
  background-color:#ffccff;
}
->
</style></head>
<body>
<center>
<%connection con;
class.forname ("com.mysql.jdbc.driver");
con=drivermanager.getconnection ("jdbc:mysql://localhost:3306/student", "root", "123456");
statement stmt;
statement s;
resultset rs;
string id=request.getparameter ("id");
try
{
stmt=con.createstatement ();
string sql="delete from student whereselect * from student");
out.print ("<table border>");
out.print ("<tr><td colspan=5 align=center>candidate data</td></tr>");
out.print ("<tr><td colspan=5 align=center><a href =" add.jsp "target =" _ self ">Add candidate information</a></td><;/tr>");
out.print ("<tr>");
out.print ("<td width=50>" + "name");
out.print ("<td width=100>" + "Age");
out.print ("<td width=100>" + "birth date");
out.print ("<td width=100 colspan=2>" + "operation");
out.print ("</tr>");
while (rs.next ())
{out.print ("<tr>");
out.print ("<td>" + rs.getstring (2) + "</td>");
out.print ("<td>" + rs.getstring (3) + "</td>");
out.print ("<td>" + rs.getstring (4) + "</td>");
string idstr=rs.getstring (1);
out.print ("<td>" + idstr + "</td>");
out.print ("<td><a href =" delete.jsp?id="+ idstr +" ">delete</a></td>");
out.print ("<td><a href =" update.jsp?id="+ idstr +" ">Modify</a></td>");
out.print ("</tr>");
}
out.print ("</table>");
con.close ();
}
catch (sqlexception e1)
{
out.print ("sql exception !!!");
}
%>
</center>
</body>
</html>

4. update example 1:

<%@page contenttype="text/html;charset=gb2312" language="java" import="java.sql. *" Errorpage=""%>
<! Doctype html public "-//w3c //dtd xhtml 1.0 transitional //en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>untitled document</title>
<style type="text/css">
<!-
body {
  background-color:#ffccff;
}
->
</style></head>
<body>
<%string id=request.getparameter ("id");
 connection con;
 string name=null;
 string age=null;
 string birth=null;
 string id1=null;
class.forname ("com.mysql.jdbc.driver");
con=drivermanager.getconnection ("jdbc:mysql://localhost:3306/student", "root", "123456");
statement sql;
resultset rs;
try
{
sql=con.createstatement ();
rs=sql.executequery ("select * from student");
while (rs.next ())
{
 name=rs.getstring (2);
 age=rs.getstring (3);
 birth=rs.getstring (4);
 id1=rs.getstring (1);
}
con.close ();
}
catch (sqlexception e1)
{
out.print ("sql exception !!!");
}
%>
<center>
<form action="update2.jsp">
<center>
 <p></p>
 <p>Name:
  <input name="name" type="text" size="15" value="<%= name%>">
 </p>
 <p>
 age:
  <input name="age" type="text" size="15" value="<%= age%>">
 </p>
 <p>Date of birth:
  <input name="birth" type="text" size="15" value="<%= birth%>">
  <input name="id1" type="hidden" value="<%= id1%>" />
</p>
 <p><input name="g" type="submit" value="modify"<input name="h" type="reset" value="reset"</p>
</form>
</center>
</body>
</html>

5. update Example 2:

<%@page contenttype="text/html;charset=gb2312"%>
<%@page import="java.sql. *"%>
<%
string name=request.getparameter ("name");
string age=request.getparameter ("age");
string birth=request.getparameter ("birth");
string id1=request.getparameter ("id1");
system.out.println (id1);
connection con=null;
try
{
class.forname ("com.mysql.jdbc.driver");
con=drivermanager.getconnection ("jdbc:mysql://localhost:3306/student", "root", "123456");
statement sql;
sql=con.createstatement ();
string sql2="update student set name =" "+ name +" ", age =" "+ age +" ", birth =" "+ birth +" "wherechakan.jsp">View</a>

6. insert

<%@page contenttype="text/html;charset=gb2312"%>
<%@page import="java.sql. *"%>
<%
string name=request.getparameter ("name");
string age=request.getparameter ("age");
string birth=request.getparameter ("birth");
connection con=null;
try
{
class.forname ("com.mysql.jdbc.driver");
con=drivermanager.getconnection ("jdbc:mysql://localhost:3306/student", "root", "123456");
statement sql;
sql=con.createstatement ();
string sql2="insert into student (name, age, birth) values ​​(" "+ name +" "," "+ age +" "," "+ birth +" ")";
system.out.print (sql2);
int s=sql.executeupdate (sql2);
}
catch (exception e) {
system.out.println (e);
}
%>
Congratulations, you added successfully!<br />
<a href="chakan.jsp">View</a>

7. Create the database

/*
mysql data transfer
source host:localhost
source database:student
target host:localhost
target database:student
date:2009-3-27 13:24:01
* /
set foreign_key_checks=0;
create database student;
use student;
-----------------------------
-table structure for student
-----------------------------
create table `student` (
 `id` int (11) not null auto_increment, `name` varchar (255) default null, `age` varchar (255) default null, `birth` varchar (255) default null, primary key (`id`)
) engine=innodb auto_increment=3 default charset=gbk;
jsp
  • Previous Implementation of strcpy function in C ++
  • Next Constructing regular expression validation using exec () method in JS