mysql+tomcat+jsp增删改查(二)
来源:互联网 发布:中国2015gdp数据 编辑:程序博客网 时间:2024/06/11 15:04
四、创建数据库
mysql>create database jsp;
mysql>use jsp
mysql>create table student(
->id int(30) not null primary key,
->name varchar(50),
->age int(30),
->gender varchar(30),
->major varchar(50)
->);
至此创建了名为jsp的数据库,一个名叫student的表.
五、页面制作
在安装完成的Tomcat6.0中的webapps中新建一个文件夹normal
新建一个pic和WEB-INF文件夹,在WEB-INF下新建一个web.xml和新建一个lib文件夹
pic 文件夹中放的是图片,网页要用背景图片.自己可以到网上找找。
不要太大的。
但是名字要改成background.jpg
web.xml中的内容为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
addStuInfo.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>输入学生信息界面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"">
function validate()// 功能验证
{
var id = document.forms[0].id.value;
var name = document.forms[0].name.value;// 0为id号。
var age = document.forms[0].age.value;
var major = document.forms[0].major.value;
if(id <= 0){
alert("学号不能为空,请输入学号!");
return false;
}
else if(name.length <= 0){
alert("姓名不能为空,请输入姓名!");
return false;
}
else if(age <= 0){
alert("请输入合法年龄!");
return false;
}
else if(major.length <= 0){
alert("专业不能为空,请输入所学专业!");
return false;
}
else{
return true;
}
//document.getElementById("form").submit();
}
</script>
</head>
<body background="pic/background.jpg">
<br>
<center>
<h2>添加学生信息</h2><hr>
<form action="insert.jsp" method="post" id="form" onSubmit="return validate()" >
<h4> 学号:<input type="text" name="id" class="{required:true}"></input><br></h4>
<h4> 姓名:<input type="text" name="name"></input><br></h4>
<h4> 年龄:<input type="text" name="age"></input><br></h4>
<h4> 性别:<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br></h4>
<h4> 专业:<input type="text" name="major"></input><br></h4>
<input type="submit" value="提交"/>
</form>
<a href="showInfo.jsp">查询所有学生信息</a>
</center>
</body>
</html>
delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>删除页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body background="pic/background.jpg">
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jsp";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url,user,password);
stat = conn.createStatement();
stat.executeUpdate("delete from student where id = " + id + "");
rs = stat.executeQuery("select * from student");
if(rs.next())
{
out.print("<center><br><br><h3>删除成功!</h3></center>");
}
else{
out.print("<center><h3>删除失败!</h3></center>");
}
%>
<br>
<br>
<center> <a href=addStuInfo.jsp>返回添加信息页面</a> <a href=showInfo.jsp>返回信息查询页面</a></center>
<%
if(rs != null)
{
rs.close();
rs = null;
}
if(stat != null)
{
stat.close();
stat = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
%>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Welcome,home</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body background="pic/background.jpg"><br/><br/>
<center>
<a href="addStuInfo.jsp">点此添加学生信息</a><br/><br/>
<a href="showInfo.jsp">点此查询学生信息</a><br/><br/>
<a href="showInfo.jsp">点此修改学生信息</a><br/><br/>
<a href="showInfo.jsp">点此删除学生信息</a><br/><br/>
<br>
</center>
</body>
</html>
insert.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>插入学生信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body background="pic/background.jpg">
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
System.out.println(name);
String age = request.getParameter("age");
String gender = request.getParameter("gender");
String major = request.getParameter("major");
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jsp";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
String sql = "insert into student(id,name,age,gender,major) values(" + id + ",'" + name + "'," + age + ",'" + gender + "','" + major + "')";
stat.executeUpdate(sql);
rs = stat.executeQuery("select * from student");
%>
<center>
<%
if(rs.next())
{
out.print("<br><h3>成功输入!</h3>");
}
else{
out.print("<br><h3>输入失败!</h3>");
}
%>
<br>
<a href=addStuInfo.jsp>返回添加信息页面</a> <a href=showInfo.jsp>进入信息查询页面</a>
</center>
<%
if(rs != null)
{
rs.close();
rs = null;
}
if(stat != null)
{
stat.close();
stat = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
%>
</body>
</html>
楼主qq:496056171
- mysql+tomcat+jsp增删改查(二)
- mysql+tomcat+jsp增删改查(一)
- mysql+tomcat+jsp增删改查(三)
- mysql+tomcat+jsp增删改查(四)
- mysql视图---增删改查(二)
- Mysql (二) - 增删改查
- jsp+servlet+jdbc增删改查功能实现(二)
- mysql+jsp+servlet 增删改查实例
- php数据库mysql(二)增删改查
- mysql 之 增删改查(二)2017-08-04
- mysql增删改查
- MySQL增删改查
- MYSQL增删改查
- MySQL增删改查
- mysql增删改查
- MySQL---增删改查
- MySQL增删改查
- mysql 增删改查
- 【SSLGZ 1762】工厂的烦恼
- 6.7 Swift闭包捕获值
- Linux定时任务
- js调试
- mpstat 命令详解
- mysql+tomcat+jsp增删改查(二)
- assert和de-assert的理解
- Redis列表类型的操作
- SGM BM CG匹配方法
- java网络编程(三)--非阻塞IO(通道、缓冲区、选择器)
- jdbc 中 excute executeUpdate的用法作用
- 代码执行sql脚本文件
- [几何 分治] BZOJ 2928 [Poi1999]飞弹 Rockets
- 进阶讨论Zigbee之外设定时器