JDBC开发中,操作数据库需要和数据库建立连接,然后将要执行的SQL语句发送到数据库服务器,
最后关闭数据库连接,都是按照这样的操做的,如果按照此流程要执行多条SQL语句,
那么就要建立多个数据库连接,将时间浪费在数据库连接上,针对这样的问题,
最后关闭数据库连接,都是按照这样的操做的,如果按照此流程要执行多条SQL语句,
那么就要建立多个数据库连接,将时间浪费在数据库连接上,针对这样的问题,
JDBC给出了一个很好的解决方案------JDBC的批处理。
依赖包更换为: mysql-connector-java-.5.0.8.jar
步骤说明:
1、代码请参考——第五篇JDBC操作数据库之删除数据。
2、在第五篇基础上继续实现功能——第六篇JDBC操作数据库之批处理(新增)。
问题解决(跳转页面):
2.Could not retrieve transation read-only status server
一、bookList.jsp
1.添加
<li><a href="<%=request.getContextPath()%>/BookBatchAdd">批量新增图书</a></li>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.entity.*" %>
<%
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="css/bookList.css">
</head>
<body>
<form action="" method="post">
<table width="80%" align="center">
<div class="list">图书列表</div>
<ul>
<li><a href="bookAdd.jsp">新增图书</a> </li>
<li><a href="<%=request.getContextPath()%>/BookBatchAdd">批量新增图书</a></li>
<li>xxx</li>
<li>xxx</li>
</ul>
<tr>
<td>图书编号</td>
<td>图书名称</td>
<td>图书价格</td>
<td>图书数量</td>
<td>图书作者</td>
<td>图书修改</td>
<td>图书删除</td>
</tr>
<c:forEach var="bookitem" items="${booklist}">
<tr>
<td>${bookitem.id}</td>
<td>${bookitem.name}</td>
<td>${bookitem.price}</td>
<td>${bookitem.bookCount}</td>
<td>${bookitem.author}</td>
<td><a href="<%=request.getContextPath()%>/BookUpdate?id=${bookitem.id}">修改</a></td>
<td><a href="<%=request.getContextPath()%>/BookDelete?id=${bookitem.id}">删除</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
二、BookBatchAdd.java
1.调用模型---调用批量新增方法
2.response重定向
/**
* Created by Ray on 2018/3/12 0012.
**/
public class BookBatchAdd extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用模型
BookBatch bookBatch = new BookBatch();
//调用批量新增图书方法
bookBatch.saveBatch();
//response重定向
response.sendRedirect(request.getContextPath() + "/BookList");
}
}
三、BookBatch.java
1.使用for循环实现批量新增
2.执行批处理操作并返回计数组成的数组
int[] rows = ps.executeBatch();
/**
* Created by Ray on 2018/3/12 0012.
**/
public class BookBatch {
Dbconn dbconn = new Dbconn();
Connection conn = null;
PreparedStatement ps = null;
/**
* @Author: Ray
* @Date: 2018/3/12 0012
* @Description: 批量新增图书
* @Return: 行数
*/
public int saveBatch(){
//行数
int row = 0;
try{
//获取数据库连接
conn = dbconn.getConnection();
//插入数据的sql语句
String sql = "insert into booktable(id,name,price,bookCount,author) values (?,?,?,?,?)";
//实例化PreparedStatement
ps = conn.prepareStatement(sql);
//实例化Random
Random random = new Random();
//循环添加数据
for(int i = 10; i < 20; i++){
//对占位符进行赋值
ps.setInt(1, i + 1);
ps.setString(2,"图书" + i);
ps.setDouble(3, i%2);
ps.setInt(4,random.nextInt(5) + 10);
ps.setString(5,"作者" + i);
//添加批处理命令
ps.addBatch();
}
//执行批处理操作并返回计数组成的数组
int[] rows = ps.executeBatch();
//对row赋值所影响的行数
row = rows.length;
ps.close();
conn.close();
}catch (Exception e){
e.printStackTrace();
}
return row;
}
}
四、web.xml
这个很关键,不能出错,否则访问会出现404错误
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>BookList</servlet-name>
<servlet-class>com.control.BookList</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookList</servlet-name>
<url-pattern>/BookList</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BookAdd</servlet-name>
<servlet-class>com.control.BookAdd</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookAdd</servlet-name>
<url-pattern>/BookAdd</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BookUpdate</servlet-name>
<servlet-class>com.control.BookUpdate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookUpdate</servlet-name>
<url-pattern>/BookUpdate</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BookDoUpdate</servlet-name>
<servlet-class>com.control.BookDoUpdate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookDoUpdate</servlet-name>
<url-pattern>/BookDoUpdate</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BookDelete</servlet-name>
<servlet-class>com.control.BookDelete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookDelete</servlet-name>
<url-pattern>/BookDelete</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BookDoDelete</servlet-name>
<servlet-class>com.control.BookDoDelete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookDoDelete</servlet-name>
<url-pattern>/BookDoDelete</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>BookBatchAdd</servlet-name>
<servlet-class>com.control.BookBatchAdd</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookBatchAdd</servlet-name>
<url-pattern>/BookBatchAdd</url-pattern>
</servlet-mapping>
</web-app>
五、Dbconn.java
1.修改
private static final String url = "jdbc:mysql://localhost:3306/Book?characterEncoding=utf-8&useSSL=true";
package com.dbutil;
import javax.xml.transform.Result;
import java.sql.*;
/**
* Created by Ray on 2018/3/10 0010.
**/
public class Dbconn {
// 数据库,端口号
private static final String url = "jdbc:mysql://localhost:3306/Book?characterEncoding=utf-8&useSSL=true";
// 数据库用户名
private static final String username = "root";
// 数据库登录密码
private static final String password = "root";
// 数据库驱动固定
private static final String jdbcName = "com.mysql.jdbc.Driver";
/**
* @Author: Ray
* @Date: 2018/3/11 0011
* @Description: 数据库连接
* @Return: Connection对象
*/
public Connection getConnection(){
//创建Connection连接对象
Connection conn = null;
try{
//加载数据库驱动并将其注册到驱动管理列表中
Class.forName(jdbcName);
//连接数据库操作
conn = DriverManager.getConnection(url,username,password);
}catch (Exception e){
e.printStackTrace();
}
//返回conn
return conn;
}
/**
* @Author: Ray
* @Date: 2018/3/11 0011
* @Description: 关闭数据库连接
* @Return:
*/
public void closeConnection(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
六、页面效果
ok!