文件分布图:

在MySQL中设置表格:

Books:

package com.caiduping.entity;

public class Books {
private int id;
// 图书名称
private String name;
// 价格
private double price;
// 数量
private int bookCount;
// 作者
private String author;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getBookCount() {
return bookCount;
}
public void setBookCount(int bookCount) {
this.bookCount = bookCount;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

bookdao:

package com.caiduping.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ArrayList;
import com.caiduping.dao.dataconn;
import com.caiduping.entity.Books; public class bookdao {
public int addBooks(Books b) throws ClassNotFoundException,SQLException{
dataconn c=new dataconn();
Connection conn=c.openconn();
String sql = "insert into tb_books(name,price,bookCount,author) values(?,?,?,?)";
// 获取PreparedStatement
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句中的第1个参数赋值
ps.setString(, b.getName());
System.out.println("name:"+b.getName());
// 对SQL语句中的第2个参数赋值
ps.setDouble(, b.getPrice());
// 对SQL语句中的第3个参数赋值
ps.setInt(,b.getBookCount());
// 对SQL语句中的第4个参数赋值
ps.setString(, b.getAuthor());
// 执行更新操作,返回所影响的行数
int row = ps.executeUpdate();
// 判断是否更新成功
conn.close();
return row;
}
public List<Books> Listbook() throws ClassNotFoundException, SQLException{
List<Books> b=new ArrayList<Books>();
dataconn c=new dataconn();
Connection conn=c.openconn();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from tb_books"); //利用st执行语句,结果防到结果集.
while(rs.next())
{Books b1=new Books();
b1.setId(rs.getInt("id"));
b1.setName(rs.getString("name"));
b1.setPrice(rs.getDouble("price"));
b1.setBookCount(rs.getInt("bookCount"));
b1.setAuthor(rs.getString("author"));
b.add(b1);
}
rs.close();
st.close();
conn.close();
return b;
}
public List<Books> Listbook(String a) throws ClassNotFoundException, SQLException //查询书籍名称含有a串的书籍
{List<Books> b=new ArrayList<Books>();
dataconn c=new dataconn();
Connection conn=c.openconn();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from tb_books where name like '%'+" + a + "+'%'"); //利用st执行语句,结果防到结果集.
while(rs.next())
{Books b1=new Books();
b1.setId(rs.getInt("id"));
b1.setName(rs.getString("name"));
b1.setPrice(rs.getDouble("price"));
b1.setBookCount(rs.getInt("bookCount"));
b1.setAuthor(rs.getString("author"));
b.add(b1);
}
rs.close();
st.close();
conn.close();
return b; }
public Books Listbook(int a) throws ClassNotFoundException, SQLException //参数a为书籍编号
{Books b1=new Books();
dataconn c=new dataconn();
Connection conn=c.openconn();
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from tb_books where id=" + a); //利用st执行语句,结果防到结果集.
if(rs.next())
{
b1.setId(rs.getInt("id"));
b1.setName(rs.getString("name"));
b1.setPrice(rs.getDouble("price"));
b1.setBookCount(rs.getInt("bookCount"));
b1.setAuthor(rs.getString("author")); }
rs.close();
st.close();
conn.close();
return b1;
}
public int modiBooks(Books b) throws SQLException, ClassNotFoundException
{int a=;
dataconn c=new dataconn();
Connection conn=c.openconn();
String sql="update tb_books set name=?,price=?,bookCount=?,author=? where id=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(,b.getName());
ps.setDouble(, b.getPrice());
ps.setInt(,b.getBookCount());
ps.setString(, b.getAuthor());
ps.setInt(,b.getId());
a=ps.executeUpdate();
ps.close();
conn.close(); return a;
}}

dataconn:

package com.caiduping.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class dataconn {
public Connection openconn()throws ClassNotFoundException,SQLException {
// TODO Auto-generated method stub
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_database10","user2","");
return conn;
}
}

Booklist:

package com.caiduping.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.caiduping.dao.bookdao;
import com.caiduping.entity.Books; public class Booklist extends HttpServlet { /**
* Constructor of the object.
*/
public Booklist() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
bookdao b=new bookdao();
List<Books> b1=new ArrayList<Books>();
try{
try {
b1=b.Listbook();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }

modibooks1:

package com.caiduping.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.caiduping.dao.bookdao;
import com.caiduping.entity.Books; public class modibooks1 extends HttpServlet { /**
* Constructor of the object.
*/
public modibooks1() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int a=Integer.parseInt(request.getParameter("ID"));
bookdao b=new bookdao();
Books b1=new Books();
try{
try {
b1=b.Listbook(a);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}
request.setAttribute("book", b1);
request.getRequestDispatcher("modi1.jsp").forward(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }

modibooks2:

package com.caiduping.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.caiduping.dao.bookdao;
import com.caiduping.entity.Books; public class modibooks2 extends HttpServlet { /**
* Constructor of the object.
*/
public modibooks2() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Books b=new Books();
b.setId(Integer.parseInt(request.getParameter("id").toString()));
b.setName(request.getParameter("name"));
b.setPrice(Double.parseDouble(request.getParameter("price").toString()));
b.setAuthor(request.getParameter("author"));
bookdao b1=new bookdao();
int n=;
try{
try {
n=b1.modiBooks(b);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(SQLException e){
e.printStackTrace();
}
request.getRequestDispatcher("b").forward(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }

book_list.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.caiduping.entity.Books" %>
<%
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>My JSP 'book_list.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<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>
<table width="" border="" align="center">
<tr>
<td>编号</td>
<td>名称</td>
<td>单价</td>
<td>数量</td>
<td>作者</td><td>操作</td>
</tr>
<%
List<Books> list=(List<Books>)request.getAttribute("list");
if(list==null||list.size()<){
out.print("没有数据!");
}else{
%>
<%for(Books b:list){ %>
<tr>
<td><%=b.getId() %></td>
<td><%=b.getName() %></td>
<td><%=b.getPrice() %></td>
<td><%=b.getBookCount() %></td>
<td><%=b.getAuthor() %></td>
<td><a href="modi?ID=%=b.getId() %>">修改</a></td>
</tr>
<%
}
} %>
</table>
</body>
</html>

modi1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.caiduping.entity.Books" %>
<%
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>My JSP 'modi1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<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>
<%
Books b=(Books)request.getAttribute("books"); %>
<form id="form1" name="form1" method="post" action="modi2">
<input type="hidden" name="id" value="<%=b.getId()%>" />
<table width="" border="" align="center">
<tr>
<td colspan=""><div align="center" class="STYLE1">书籍修改</div></td>
</tr> <tr>
<td>书名</td>
<td><label>
<input name="name" type="text" id="name" value="<%=b.getName()%>"/>
</label></td>
</tr>
<tr>
<td>单价</td>
<td><label>
<input name="price" type="text" id="price" value="<%=b.getPrice()%>"/>
</label></td>
</tr>
<tr>
<td>数量</td>
<td><label>
<input name="bookCount" type="text" id="bookCount" value="<%=b.getBookCount()%>"/>
</label></td>
</tr>
<tr>
<td>作者</td>
<td><label>
<input name="author" type="text" id="author" value="<%=b.getAuthor()%>"/>
</label></td>
</tr>
<tr>
<td colspan=""><label>
<div align="center">
<input type="submit" name="Submit" value="修改" />
</div>
</label></td>
</tr>
</table>
</form>
</body>
</html>

JDBC之修改数据的更多相关文章

  1. JDBC操作数据库之修改数据

    使用JDBC修改数据库中的数据,起操作方法是和添加数据差不多的,只不过在修改数据的时候还要用到UPDATE语句来实现的,例如:把图书信息id为1的图书数量改为100,其sql语句是:update bo ...

  2. c#教程之通过数据绑定修改数据

    通过数据绑定修改数据 "实体框架"提供了与数据库的双向通信通道.前面已经讲述了如何使用"实体框架"获 取数据,现在来看看如何修改获取的信息,并将改动发送回数据库 ...

  3. Redis修改数据多线程并发—Redis并发锁

    本文版权归博客园和作者本人吴双共同所有 .转载爬虫请注明地址,博客园蜗牛 http://www.cnblogs.com/tdws/p/5712835.html 蜗牛Redis系列文章目录http:// ...

  4. MySQL数据库5 - 插入数据,修改数据,删除数据

    一.插入数据 1. 所有列都插入值 INSERT [INTO] TABLE_NAME VALUES(V1,V2....Vn); 特点:列值同数,列值同序 eg: insert into users v ...

  5. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据

    1. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据:修改GridView.OptionsBehavior.EditorShowMode属性为Click 2. 实现De ...

  6. IntelliJ IDEA 在网页修改数据,但是在浏览器刷新的时候,不能读取到修改之后的数据

    使用IntelliJ IDEA 在网页修改数据,但是在浏览器刷新的时候,不能读取到修改之后的数据? 解决办法:tomcat配置中,On frame deactivation属性选择Update cla ...

  7. DataSnap修改数据ApplyUpdates出现错误:连接繁忙导致另一个命令

    最近准备尝试用DBExpress做个SQL Serer应用,在学习的时候发现一个问题使用DBExpress连接Sql server 2008 express使用以下控件SQLConnection-&g ...

  8. phalcon: update修改数据却变成了insert插入数据

    phalcon: 在对表进行操作是,update修改数据却变成了insert插入数据. 发现,update的时,无论怎么加where都会变成了insert插入数据. 检查了一下表,原来是表没有 主键引 ...

  9. MYSQL中约束及修改数据表

    MYSQL中约束及修改数据表 28:约束约束保证数据的完整性和一致性约束分为表级约束和列级约束约束类型包括:    NOT NULL(非空约束)    PRIMARY KEY(主键约束)    UNI ...

随机推荐

  1. idea 搭建java项目

    IntelliJ IDEA 12.0搭建Maven Web SSH2架构项目示例         以IDEA为环境,搭建SSH架构示例程序,用Maven管理依赖.这篇文章是一个示例,你需要首先搭建好M ...

  2. WinForms 新窗体后台打开完美的解决

    最近在做浏览器开发时,想要实现 IE 6那种多窗体,又允许后台打开而不抢占视野的方式. WinForms 应用程序中想要后台打开一个新的窗体,而不(抢焦).(遮挡)目前窗体. 需要注意的是,SW_SH ...

  3. WIM更新命令(打补丁)

    在D盘新建3个文件夹:win7(install.wim).updates(补丁).win7ultra 1.先打开ISO文件,然后加载映像到D:\win7ultra文件夹dism /mount-wim ...

  4. C++ 内存相关

    1.C++的内存管理可分为以下几个部分: 栈:记录程序的执行过程. 堆:采用new,delete申请释放内存. 自由存储区:对应于C中使用malloc,free申请释放内存. 全局存储区:也叫静态存储 ...

  5. 异常捕捉 ( try catch finally ) 你真的掌握了吗?

    前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理? finally 体遇到 return 怎么办?finally 体里有 System.exit ...

  6. SQL Server中DML语句要申请的锁

    对于select语句: 1.当採用表扫描时,会直接锁定page,而不是锁定详细的某条记录,所以会有这些锁: A.数据库S锁 B.表的IS锁 C.页的S锁 2.当採用索引来查找数据时,会锁定详细的记录, ...

  7. cc代码学习笔记1

    #define #define INT32 int #define INT8 char #define CHAR char #define SSHORT signed short #define IN ...

  8. objective-c对NSArray的学习

    转自:http://gekie.iteye.com/blog/1086256 NSARRAY简单的使用 定义数组,遍历数组: 1 2 3 4 5 6 7 8 NSArray *array; array ...

  9. strassen algorithm

    the explaination that is clear in my view is from wiki.

  10. Aggregating local features for Image Retrieval

    Josef和Andrew在2003年的ICCV上发表的论文[10]中,将文档检索的方法借鉴到了视频中的对象检测中.他们首先将图像的特征描述类比成单词,并建立了基于SIFT特征的vusual word ...