创作不易,如果你觉得有用不妨点个赞~
代码可以自由复制、转载。
- 建立数据库 lianxi,在该数据库下建立一个图书表 book,图书包含信息:图书号、图书名、作者、价格、备注字段。设计一应用程序,完成图书信息的管理。主要完成图书信息的添加、查询、删除、修改等操作。
(1个数据库建表语句 + 1个Java类 + 5个页面 )
1. 数据库建表语句
create database if not exists lianxi;
use lianxi;
drop table if exists book;
create table book(
id int auto_increment primary key,
bookName varchar(30) default '' not null,
author varchar(20) default '' not null,
price float(8, 2) default 0.00 not null,
message varchar(100) default '' not null
);
INSERT INTO lianxi.book ( bookName, author, price, message) VALUES ( '《作业好难》', 'yourself', 30, '做完了吗?');
2. DBBean.java(数据库连接类)
package book.bean;
import java.sql.*;
public class DBBean {
private String driverStr="com.mysql.cj.jdbc.Driver";
private String url1="jdbc:mysql://localhost:3306/lianxi";
private String url2="?user=root&password=123456";
private String url3="&useUnicode=true&characterEncoding=UTF-8";
private String connStr;
private Connection conn=null;
private Statement stmt=null;
public DBBean(){
connStr=url1+url2+url3;
try{
Class.forName(driverStr);
conn = DriverManager.getConnection(connStr);
stmt = conn.createStatement();
}catch(Exception ex){
System.out.println("无法同数据库建立连接!");
}
}
public int executeUpdate(String s){
int result=0;
try{
result=stmt.executeUpdate(s);
}catch(Exception ex){
System.out.println("执行更新错误!");
}
return result;
}
public ResultSet executeQuery(String s){
ResultSet rs=null;
try{
rs=stmt.executeQuery(s);
}
catch(Exception ex){
System.out.println("执行查询错误!");
}
return rs;
}
public void close(){
try{
stmt.close();
conn.close();
}
catch(Exception e){
}
}
}
3. index.jsp(首页)
<%@ page import="java.sql.*" pageEncoding="UTF-8" %>
<html>
<head>
<title>图书系统</title>
</head>
<body>
<center><a href=addBook.jsp>增加图书信息</a></center>
<p>
<table align=center width=510 border=1.5>
<tr>
<td>书名
<td>作者
<td>价格
<td>操作
<td>备注
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
String s="select * from book";
ResultSet resultSet=db.executeQuery(s);
while(resultSet.next()){
int id=resultSet.getInt(1);
out.println("<tr><td>"+resultSet.getString(2)+"</td><td>"+resultSet.getString(3)+"</td><td>"+resultSet.getString(4)+"</td><td><a href='dropBook.jsp?id="+id+"'>删除</a> <a href='editBook.jsp?id="+id+"'>修改</a></td><td>"+resultSet.getString(5)+"</td></tr>");
}
resultSet.close();
db.close();
%>
</table>
</body>
</html>
4. addBook.jsp(添加图书页面)
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head><title>添加图书</title></head>
<body>
<form action="addBook.jsp" method="post">
<table width="50%" border="1" align="center">
<CAPTION>添加图书</CAPTION>
<tr>
<th width="40%">书名:</th>
<td width="60%"><input name="bookName" type="text"></td>
</tr>
<tr>
<th>作者:</th>
<td><input name="author" type="text"></td>
</tr>
<tr>
<th>价格:</th>
<td><input name="price" type="text"> 元</td>
</tr>
<tr>
<th>备注:</th>
<td><input name="message" type="text"></td>
</tr>
<tr>
<th colspan="2">
<input type="submit" name="submit" value="提交">
<input type="reset" value="重置">
</th>
</tr>
</table>
</form>
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
request.setCharacterEncoding("gb2312");
String submit = request.getParameter("submit");
if (submit != null && !submit.equals("")) {
String b = request.getParameter("bookName");
String m = request.getParameter("message");
String p = request.getParameter("price");
String a = request.getParameter("author");
String sql = "insert into book(bookName,author,price,message) values('" + b + "','" + a + "'," + p + ",'" + m + "')";
int i = db.executeUpdate(sql);
if (i == 1) {
out.println("<script language='javaScript'> alert('添加成功!');</script>");
response.setHeader("refresh", "1;url=index.jsp");
} else {
out.println("<script language='javaScript'> alert('添加失败,请重试!');</script>");
response.setHeader("refresh", "1;url=addBook.jsp");
}
db.close();
}
%>
</body>
</html>
5. editBook.jsp(编辑图书页面)
<%@ page import="java.sql.*" pageEncoding="gb2312" %>
<html>
<head><title>修改图书</title></head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean" />
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
ResultSet resultSet =db.executeQuery("select * from book where id="+id);
resultSet.next();
%>
<form action="update.jsp" method="post">
<table width="60%" border="1" align="center">
<CAPTION>修改图书</CAPTION>
<tr>
<th width="40%">书名:</th>
<td width="60%">
<input name="bookName" type="text" value="<%=resultSet.getString(2)%>"></td>
</tr>
<tr>
<th>作者:</th>
<td><input name="author" type="text" value="<%=resultSet.getString(3)%>"></td>
</tr>
<tr>
<th>价格:</th>
<td><input name="price" type="text" value="<%=resultSet.getString(4)%>">元</td>
</tr>
<tr>
<th>备注:</th>
<td><input name="message" type="text" value="<%=resultSet.getString(5)%>"></td>
</tr>
<tr>
<th colspan="2">
<input type="hidden" name="id" value="<%=id%>">
<input type="submit" value="提交">
<input type="reset" value="重置">
</th>
</tr>
</table>
</form>
<%
db.close();
%>
</body>
</html>
6. update.jsp(更新操作)
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>update</title>
</head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
request.setCharacterEncoding("gb2312");
String b = request.getParameter("bookName");
String a = request.getParameter("author");
String p = request.getParameter("price");
String id = request.getParameter("id");
String m = request.getParameter("message");
String sql = "update book set bookName='" + b + "',author='" + a + "',price=" + p + ",message='" + m + "' where id=" + id;
int i = db.executeUpdate(sql);
if (i == 1) {
out.println("<script language='javaScript'> alert('修改成功!');</script>");
response.setHeader("refresh", "1;url=index.jsp");
}
db.close();
%>
</body>
</html>
7. dropBook.jsp(删除图书操作)
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<head><title>删除</title></head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
String id=request.getParameter("id");
request.setCharacterEncoding("gb2312");
int i=db.executeUpdate("delete from book where id="+id);
if(i==1)
{out.println("<script language='javaScript'> alert('删除成功!');</script>");
response.setHeader("refresh","1;url=index.jsp");
}
else{
out.println("<script language='javaScript'> alert('删除失败!');</script>");
response.setHeader("refresh","1;url=dropBook.jsp");
}
db.close();
%>
</body>
</html>
(结束)
- 设计一个简单的网上名片管理系统,实现名片的增、删、改、查等操作。该名片管理系统包括如下功能。
(1) 用户登录与注册
用户登录:在登录时,如果用户名和密码正确,进入系统页面。
用户注册:新用户应该先注册,然后再登录该系统。
(2) 名片管理
增加名片:以仿真形式(按常用的名片格式)增加名片信息。
修改名片:以仿真形式(按常用的名片格式)修改名片信息。
查询名片:以模糊查询的方式查询名片。
删除名片:名片的删除有两种方式,即把名片移到回收站,把名片彻底删除。
(3) 回收站管理
还原:把回收站中的名片还原回收。
彻底删除:把名片彻底从回收站删除。
浏览/查询:可以模糊查询、浏览回收站中的名片。
(1个数据库建表语句 + 1个Java类 + 10个页面 )
1. 数据库建表语句
create database if not exists lianxi;
use lianxi;
drop table if exists user;
create table user(
id int primary key auto_increment,
username varchar(30) not null unique ,
password varchar(30) not null default '123456'
comment '默认密码都是123456'
);
# 管理员账号admin,密码123456
insert into user(username, password) values ('admin','123456');
drop table if exists card;
create table card(
id int primary key auto_increment,
name varchar(50) not null ,
age int,
sex char(1),
work varchar(50),
logicDelete int default 0
);
insert into card( name, age, sex, work) values ('张三',22,'男','职员'),
('李四',20,'女','经理');
2. DBBean.java(数据库连接类)
package book.bean;
import java.sql.*;
public class DBBean {
private String driverStr="com.mysql.cj.jdbc.Driver";
private String url1="jdbc:mysql://localhost:3306/lianxi";
private String url2="?user=root&password=123456";
private String url3="&useUnicode=true&characterEncoding=UTF-8";
private String connStr;
private Connection conn=null;
private Statement stmt=null;
public DBBean(){
connStr=url1+url2+url3;
try{
Class.forName(driverStr);
conn = DriverManager.getConnection(connStr);
stmt = conn.createStatement();
}catch(Exception ex){
System.out.println("无法同数据库建立连接!");
}
}
public int executeUpdate(String s){
int result=0;
try{
result=stmt.executeUpdate(s);
}catch(Exception ex){
System.out.println("执行更新错误!");
}
return result;
}
public ResultSet executeQuery(String s){
ResultSet rs=null;
try{
rs=stmt.executeQuery(s);
}
catch(Exception ex){
System.out.println("执行查询错误!");
}
return rs;
}
public void close(){
try{
stmt.close();
conn.close();
}
catch(Exception e){
}
}
}
3. index.jsp(首页、登录页面)
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>名片管理系统</title>
<style>
p {
text-align: center;
margin: 5px;
}
* {
margin: 0;
padding: 0;
}
div {
text-align: center;
}
.custom {
display: flex;
justify-content: center;
align-items: center;
width: 400px;
height: 400px;
margin: 150px auto;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="custom">
<form action="index.jsp">
<p>用户名:<input type="text" name="username"></p>
<p>密码 :<input type="password" name="password" ></p>
<br>
<input type="submit" name="submit" value="登录" style="font-size: 16px;padding: 5px 5px;margin: 5px">
<br>
<a href="register.jsp">没有账号,立即注册?</a>
</form>
</form>
</div>
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
request.setCharacterEncoding("gb2312");
String submit = request.getParameter("submit");
if (submit != null && !submit.equals("")) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String sql = "select count(*) from user where username = '" + username + "' and password = '" + password + "'";
ResultSet rs = db.executeQuery(sql);
int count = 0;
// 判断是否登录
if (rs.next()) {
count = rs.getInt(1);
}
if (count == 1) {
Cookie cookie = new Cookie("login", username);
response.addCookie(cookie);
Cookie cookie2 = new Cookie("key", "");
response.addCookie(cookie2);
response.setHeader("refresh", "1;url=card.jsp");
} else {
out.println("<script language='javaScript'> alert('账号或密码错误!');</script>");
response.setHeader("refresh", "1;url=index.jsp");
}
db.close();
} %>
</body>
</html>
4. register.jsp(注册页面)
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册</title>
<style>
* {
margin: 0;
padding: 0;
}
p {
text-align: center;
margin: 5px;
}
div {
text-align: center;
}
.custom {
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height: 300px;
margin: 150px auto;
border: 1px solid red;
}
</style>
</head>
<body>
<div class="custom">
<form action="register.jsp">
<p>用户名:<input type="text" name="username"></p>
<p>密码 :<input type="password" name="password" ></p>
<br>
<input type="submit" name="submit" value="注册账号" style="font-size: 18px;padding: 3px 3px;margin: 3px">
<br>
</form>
</form>
</div>
<jsp:useBean id="db" class="book.bean.DBBean" />
<%
request.setCharacterEncoding("gb2312");
String submit = request.getParameter("submit");
if (submit != null && !submit.equals("")) {
String u = request.getParameter("username");
String p = request.getParameter("password");
String s = "insert into user(username,password) values('" + u + "','" + p + "')";
try {
int n = db.executeUpdate(s);
if (n > 0) {
out.println("<script language='javaScript'> alert('注册成功!');</script>");
response.setHeader("refresh", "1;url=index.jsp");
} else {
out.println("<script language='javaScript'> alert('账号已存在,请重新输入!');</script>");
response.setHeader("refresh", "1;url=register.jsp");
}
} catch (Exception e) {
out.println("<script language='javaScript'> alert('未知错误!');</script>");
response.setHeader("refresh", "1;url=index.jsp");
}
db.close();
}
%>
</body>
</html>
5. card.jsp(名片页面)
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>名片管理系统</title>
<style>
.center {
display: block;
text-align: center;
}
</style>
</head>
<body>
<%
// 获取所有的cookie
Cookie[] cookies = request.getCookies();
String key = "";
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("login")) {
out.println("<h1 class=\"center\">欢迎你," + cookies[i].getValue() + "!</h1>");
}
if (cookies[i].getName().equals("key")) {
key = cookies[i].getValue();
}
}
%>
<div class="center">
<a href=addCard.jsp>添加名片</a>
  <a href=cycleCard.jsp>回收站</a>
  <a href=index.jsp>退出登录</a>
</div>
<form class="center">
姓名:<input type="text" name="key" >
<input type="button" value="查找" onclick="seek()">
</form>
<table align=center width=500 border=1>
<tr>
<td>姓名
<td>年龄
<td>性别
<td>职位
<td>操作
<jsp:useBean id="db" class="book.bean.DBBean" />
<%
String s="select * from card where logicDelete=0 and name like '%"+key+"%'";
ResultSet rs=db.executeQuery(s);
while(rs.next()){
int id=rs.getInt(1);
out.println("<tr><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td>"+rs.getString(5)+"<td><a href='delCard.jsp?id="+id+"'>删除</a> <a href='editCard.jsp?id="+id+"'>修改</a></td></tr>");
}
rs.close();
db.close();
%>
</table>
<script>
function seek(){
var key = document.forms[0].key.value;
document.cookie = " key = "+key;
document.forms[0].submit()
}
</script>
</body>
</html>
6. addCard.jsp(添加名片页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加名片</title>
<style>
div {
text-align: center;
}
.custom {
display: flex;
align-items: center;
width: 500px;
height: 300px;
margin: 150px auto;
border: 1px solid green;
padding: 3px;
}
.img {
margin-left: 150px;
border: 1px solid green;
width: 100px;
height: 150px;
ine-height: 150px;
text-align: center;
font-size: 16px;
}
</style>
</head>
<body>
<a href="card.jsp" style="display: block;text-align: center;font-size: 24px">返回首页</a>
<div class="custom">
<form action="addCard.jsp">
<p>姓名:<input type="text" name="name">
<p>年龄:<input type="number" name="age"></p>
<p>性别:<input type="radio" name="sex" value="男" checked>男 
<input type="radio" name="sex" value="女">女</p>
<p>职位:<input type="text" name="work"></p>
<br>
<input type="submit" name="submit" value="添加" style="font-size: 16px;padding: 3px 3px;margin: 3px">
<br>
</form>
</form><span class="img">这是头像</span>
</div>
</body>
<jsp:useBean id="db" class="book.bean.DBBean" scope="page"/>
<%
request.setCharacterEncoding("gb2312");
String submit = request.getParameter("submit");
if (submit != null && !submit.equals("")) {
String n = request.getParameter("name");
String a = request.getParameter("age");
String s = request.getParameter("sex");
String w = request.getParameter("work");
String sql = "insert into card(name,age,sex,work) values('" + n + "'," + a + ",'" + s + "','" + w + "')";
int i = db.executeUpdate(sql);
if (i == 1) {
out.println("<script language='javaScript'> alert('添加成功!');</script>");
response.setHeader("refresh", "1;url=addCard.jsp");
} else {
out.println("<script language='javaScript'> alert('添加失败!');</script>");
response.setHeader("refresh", "1;url=addCard.jsp");
}
db.close();
}
%>
</html>
7. editCard.jsp(名片编辑页面)
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
div {
text-align: center;
}
.custom {
display: flex;
align-items: center;
width: 500px;
height: 300px;
margin: 150px auto;
border: 1px solid green;
padding: 3px;
}
.img {
margin-left: 150px;
border: 1px solid green;
width: 100px;
height: 150px;
ine-height: 150px;
text-align: center;
}
</style>
</head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean" />
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
ResultSet resultSet =db.executeQuery("select * from card where id="+id);
resultSet.next();
%>
<a href="card.jsp" style="display: block;text-align: center;font-size: 24px">返回首页</a>
<div class="custom">
<form action="updateCard.jsp">
<p>姓名:<input type="text" name="name" value="<%=resultSet.getString(2)%>">
<p>年龄:<input type="number" name="age" value="<%=resultSet.getString(3)%>"></p>
<p>性别:<input type="radio" name="sex" value="男" checked>男 
<input type="radio" name="sex" value="女">女</p>
<p>职位:<input type="text" name="work" value="<%=resultSet.getString(5)%>"></p>
<br>
<input type="hidden" name="id" value="<%=id%>">
<input type="submit" name="submit" value="确定修改" style="font-size: 16px;padding: 5px 5px;margin: 5px">
<br>
</form><span class="img">这是头像</span>
</div>
</body>
</html>
8. updateCard.jsp(名片更新操作)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>update</title>
</head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean" />
<%
request.setCharacterEncoding("gb2312");
String n = request.getParameter("name");
String a = request.getParameter("age");
String s = request.getParameter("sex");
String w = request.getParameter("work");
String id = request.getParameter("id");
String sql = "update card set name='" + n + "',age=" + a + ",sex='" + s + "',work ='"+ w +"' where id=" + id;
int i = db.executeUpdate(sql);
if (i == 1) {
out.println("<script language='javaScript'> alert('修改成功!');</script>");
response.setHeader("refresh", "1;url=card.jsp");
}
db.close();
%>
</body>
</html>
9. delCard.jsp(删除名片 , 逻辑删除)
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<head><title>delete</title></head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
int i=db.executeUpdate("update card set logicDelete = 1 where id="+id);
if(i==1)
{out.println("<script language='javaScript'> alert('删除成功!');</script>");
response.setHeader("refresh","1;url=card.jsp");
}
else{
out.println("<script language='javaScript'> alert('删除失败!');</script>");
response.setHeader("refresh","1;url=card.jsp");
}
db.close();
%>
</body>
</html>
10. cycleCard.jsp(名片回收站)
<%@ page import="java.sql.*" %>
<%@ page import="com.sun.jdi.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>回收站</title>
<style>
.center {
display: block;
text-align: center;
}
</style>
</head>
<body>
<div class="center">
<a href=card.jsp>返回首页</a>
<form class="center">
关键词:<input type="text" name="key">
<input type="button" value="搜索" onclick="seek()">
</form>
<table align=center width=500 border=1>
<tr>
<td>姓名
<td>年龄
<td>性别
<td>职位
<td>操作
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
String key = "";
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("key2")) {
key = cookies[i].getValue();
}
}
String s="select * from card where logicDelete=1 and name like '%"+key+"%'";
ResultSet rs=db.executeQuery(s);
while(rs.next()){
int id=rs.getInt(1);
out.println("<tr><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td>"+rs.getString(5)+"<td><a href='recoverCard.jsp?id="+id+"'>恢复</a> "+"<a href='dropCard.jsp?id="+id+"'>彻底删除</a></td></tr>");
}
rs.close();
db.close();
%>
</table>
</div>
<script>
function seek() {
var key = document.forms[0].key.value;
document.cookie = " key2 = " + key;
location.reload()
}
</script>
</body>
</html>
11. recoverCard.jsp(恢复操作)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean" />
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
int i=db.executeUpdate("update card set logicDelete = 0 where id="+id);
if(i==1)
{out.println("<script language='javaScript'> alert('恢复成功!');</script>");
response.setHeader("refresh","1;url=cycleCard.jsp");
}
else{
out.println("<script language='javaScript'> alert('恢复成功!');</script>");
response.setHeader("refresh","1;url=cycleCard.jsp");
}
db.close();
%>
</body>
</html>
12. dropCard.jsp(彻底删除名片,物理删除)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>drop</title>
</head>
<body>
<jsp:useBean id="db" class="book.bean.DBBean"/>
<%
request.setCharacterEncoding("gb2312");
String id=request.getParameter("id");
int i=db.executeUpdate("delete from card where id="+id);
if(i==1)
{out.println("<script language='javaScript'> alert('删除成功!');</script>");
response.setHeader("refresh","1;url=cycleCard.jsp");
}
else{
out.println("<script language='javaScript'> alert('删除失败!');</script>");
response.setHeader("refresh","1;url=cycleCard.jsp");
}
db.close();
%>
</body>
</html>