欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp+mysql数据库操作常用方法实例总结

程序员文章站 2024-01-14 08:44:52
本文实例讲述了jsp+mysql数据库操作常用方法。分享给大家供大家参考。具体如下: 1. 查看:     <%@...

本文实例讲述了jsp+mysql数据库操作常用方法。分享给大家供大家参考。具体如下:

1. 查看:    

<%@ 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 style='font-size: 10pt'>"); 
out.print("<tr><td colspan=5 align=center>考生数据</td></tr>"); 
out.print("<tr><td colspan=5 align=center><a href='add.jsp' target='_self'>添加考生信息</a></td></tr>"); 
out.print("<tr>"); 
out.print("<td width=50 >"+"姓名"); 
out.print("<td width=100 >"+"年龄"); 
out.print("<td width=100>"+"出生日期"); 
out.print("<td width=100 colspan=2>"+"操作"); 
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+"'>删除</a></td>"); 
out.print("<td><a href='update.jsp?id="+idstr+"'>修改</a></td>"); 
out.print("</tr>") ; 
}
out.print("</table>"); 
con.close(); 
} 
catch(sqlexception e1) 
{ 
out.print("sql异常!!!!"); 
} 
%> 
</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" class="style6">添加考生信息</p>
<center>
<form action="insert.jsp" name=form>
<table>
  <tr><td height="36"><span class="style5">姓名:</span></td>
  <td><input name="name" type=text size="15" ></td></tr>
  <tr>
   <td height="36"><span class="style5">年龄:</span></td>
  <td><input name="age" type=text size="15"></td></tr>
  <tr>
   <td height="36"><span class="style5">出生年月:</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="添加"></td>
   <td width="28" wnameth="50"> </td>
   <td width="42" wnameth="50"><input type="reset" name="h" value="重置"></td>
   <td width="33" wnameth="42"> </td>
  </tr>
</table>
</form></center>
</body></html>

3. delete 删除:

<%@ page contenttype="text/html;charset=gb2312" %> 
<%@ page import="java.sql.*" %> 
<html> 
<head> 
<title>删除操作</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 where id="+id; 
stmt.executeupdate(sql); 
s=con.createstatement(); 
rs=s.executequery("select * from student"); 
out.print("<table border style='font-size: 10pt'>"); 
out.print("<tr><td colspan=5 align=center>考生数据</td></tr>");
out.print("<tr><td colspan=5 align=center><a href='add.jsp' target='_self'>添加考生信息</a></td></tr>"); 
out.print("<tr>"); 
out.print("<td width=50 >"+"姓名"); 
out.print("<td width=100 >"+"年龄"); 
out.print("<td width=100>"+"出生日期"); 
out.print("<td width=100 colspan=2>"+"操作"); 
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+"'>删除</a></td>"); 
out.print("<td><a href='update.jsp?id="+idstr+"'>修改</a></td>"); 
out.print("</tr>") ; 
} 
out.print("</table>"); 
con.close(); 
} 
catch(sqlexception e1) 
{ 
out.print("sql异常!!!!"); 
} 
%>
</center>
</body> 
</html>

4. update 示例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>无标题文档</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异常!!!!"); 
} 
%>
<center>
<form action="update2.jsp">
<center>
 <p>  </p>
 <p>姓名:
  <input name="name" type="text" size="15" value="<%=name%>">
 </p>
 <p>
 年龄:
  <input name="age" type="text" size="15" value="<%=age%>">
 </p>
 <p>出生日期:
  <input name="birth" type="text" size="15" value="<%=birth%>">
  <input name="id1" type="hidden" value="<%=id1%>" />
</p>
 <p><input name="g" type="submit" value="修改">     <input name="h" type="reset" value="重置"></p>
</form>
</center>
</body>
</html>

5. update 示例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+"' where id="+id1;
system.out.print(sql2);
int s=sql.executeupdate(sql2);
}
catch(exception e){
system.out.println(e);
}
%>
恭喜你,修改成功!<br />
<a href="chakan.jsp">查看</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);
}
%>
恭喜你,添加成功!<br />
<a href="chakan.jsp">查看</a>

7. 创建数据库

/*
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程序设计有所帮助。