您的位置:首页 > 其它

图书管理系统(增,改,删,查)功能的实现

2012-05-11 13:05 746 查看

显示功能实现(index.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>

<%@ page contentType="text/html" %>

<html>

<head>

<title>My JSP 'index.jsp' starting page</title>

</head>

<body>

<center><a href="add.jsp">添加图书信息</a></center><p>

<table align="center" width="50%" border="1">

<tr>

<th>书名</th>

<th>作者</th>

<th>价格</th>

<th>管理</th>

</tr>

<%

try{

Class.forName("com.mysql.jdbc.Driver");//数据库驱动加载

//取得数据库连接

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");

//实例化createStatement对象

Statement stmt=con.createStatement();

String s="select * from book";

//执行查询操作

ResultSet rs=stmt.executeQuery(s);

while(rs.next()){

int id=rs.getInt(1);

out.println("<tr><td>"+rs.getString(2)+"</d><td>"+rs.getString(3)+"</td><td>"

+rs.getString(4)+"</td><td><a href='edit.jsp?id="+id+"'>修改</a> <a href='del.jsp?id="+id+"'>删除</a></td></tr>");

//String bookname=rs.getString(2);

//String author=rs.getString(3);

//Float price=rs.getFloat(4);

}

rs.close();

stmt.close();

con.close();

%>

</table>

<%

}catch(Exception e){

System.out.println(e);

}

%>

</body>

</html>

添加部分功能实现(edit.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>

<%@ page contentType="text/html" %>

<html>

<head>

<title>My JSP 'edit.jsp' starting page</title>

</head>

<body>

<%

try{

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");

Statement stmt=con.createStatement();

String id=request.getParameter("id");

ResultSet rs=stmt.executeQuery("select * from book where id="+id);

rs.next();

%>

<form action="update.jsp" method="post">

<table align="center" width="50%" border="1">

<caption>修改图书信息</caption>

<tr>

<th width="30%">书名</th>

<td width="70%"><input name="bookname" type="text" value="<%=rs.getString(2)%>"></td>

</tr>

<tr>

<th >作者:</th>

<td ><input name="author" type="text" value="<%=rs.getString(3)%>"></td>

</tr>

<tr>

<th >价格:</th>

<td ><input name="price" type="text" value="<%=rs.getString(4)%>">元</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>

<%

rs.close();

con.close();

}catch(Exception e)

{

System.out.println(e);

}

%>

</body>

</html>

添加功能验证代码实现(add.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>

<%@ page contentType="text/html" %>

<html>

<head>

<title>My JSP 'add.jsp' starting page</title>

</head>

<body>

<%

request.setCharacterEncoding("gbk");

%>

<form action="add.jsp" method="post">

<table align="center" width="50%" border=""1>

<caption>添加图书信息</caption>

<tr>

<th width="30%">书名:</th>

<td width="70%"><input name="bookname" type="text"></td>

</tr>

<tr>

<th width="30%">作者:</th>

<td width="70%"><input name="author" type="text"></td>

</tr>

<tr>

<th width="30%">价格:</th>

<td width="70%"><input name="price" type="text"></td>

</tr>

<tr>

<th clospan="2">

<input type="submit" name="submit" value="添加" >

<input type="reset" value="重置">

</th>

</tr>

</table>

</form>

<%

String submit=request.getParameter("submit");

if(!"".equals(submit)&&submit!=null)

{

String bookname=request.getParameter("bookname");

String author=request.getParameter("author");

String price=request.getParameter("price");

try{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");

Statement stmt=con.createStatement();

String sql="insert into book(bookname,author,price)values('"+bookname+"','"+author+"',"+price+")";

int i=stmt.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=add.jsp");

}

stmt.close();

con.close();

}catch(Exception e){

System.out.println(e);

}

%>

<%

}

%>

</body>

</html>

删除功能实现(del.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>

<%@ page contentType="text/html" %>

<html>

<head>

<title>My JSP 'del.jsp' starting page</title>

</head>

<body>

<%

request.setCharacterEncoding("gbk");

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");

Statement stmt=con.createStatement();

String id=request.getParameter("id");

int i=stmt.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=index.jsp");

}

stmt.close();

con.close();

%>

</body>

</html>

修改功能代码实现(update.jsp)

<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gbk"%>

<%@ page contentType="text/html" %>

<html>

<head>

<title>My JSP 'update.jsp' starting page</title>

</head>

<body>

<%

request.setCharacterEncoding("gbk");

String bookname=request.getParameter("bookname");

String author=request.getParameter("author");

String price=request.getParameter("price");

String id=request.getParameter("id");

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/book","root","zhukexue");

Statement stmt=con.createStatement();

String sql="update book set bookname='"+bookname+"',author='"+author+"',price="+price+"where id="+id;

int i=stmt.executeUpdate(sql);

if(i==1)

{

out.println("<script language='javaScript'>alert('修改成功,单击确定后自动跳转到主页。');</script>");

response.setHeader("refresh","1;url=index.jsp");

}

stmt.close();

con.close();

%>

</body>

</html>

数据库实现(book.sql)

-- ----------------------------

创建表book

-- ----------------------------

CREATE TABLE `book` (

`id` int(10) NOT NULL AUTO_INCREMENT,

`bookname` varchar(20) DEFAULT NULL,

`author` varchar(20) DEFAULT NULL,

`price` float(7,2) DEFAULT NULL,

PRIMARY KEY (`id`)

)

-- ----------------------------

插入数据

-- ----------------------------

INSERT INTO `book` VALUES ('2', 'web基础教程', '姜忠', '90.00');

INSERT INTO `book` VALUES ('3', '数据库应用技术', '城东', '90.00');

INSERT INTO `book` VALUES ('4', 'android开发实战经典', '李兴华', '88.00');

INSERT INTO `book` VALUES ('6', 'javaweb开发实战经典', '李兴华', '60.00');

INSERT INTO `book` VALUES ('12', '数据库系统简明教程', '王珊', '27.00');

INSERT INTO `book` VALUES ('13', 'java开发实战经典', '李兴华', '65.00');

INSERT INTO `book` VALUES ('14', 'oracle开发实战经典', '李兴华', '72.00');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: