范例:
在查詢所有圖書信息的頁面中,添加刪除圖書信息的超鏈接,通過Servlet實現對數據的刪除操作
(1)在book_list.jsp中,增加刪除圖書信息的超鏈接,將連接的地址指向DeleteServlet。
<%@ page language="java" import="java.util.*" pageEncoding="UTF⑻"%>
<%
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 'check.jsp' starting page</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>
<a href="FindServlet">查看所有圖書信息</a>
</body>
</html>
(2)編寫DeleteServlet,在doGet()方法中,編寫刪除圖書信息的方法
package com.zgy.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeleteServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
int id = Integer.valueOf(request.getParameter("id"));//獲得圖書id
try{
Class.forName("com.mysql.jdbc.Driver");//注冊驅動
String url = "jdbc:mysql://localhost:3306/zhouguanya";//數據庫連接字符串
String user = "root";//數據庫用戶名
String password = "root";//數據庫密碼
String sql = "delete from books where id = ?";//sql語句
Connection conn = DriverManager.getConnection(url, user, password);//創建Connection連接
PreparedStatement ps = conn.prepareStatement(sql);//獲得PreparedStatement對象
ps.setInt(1, id);//設置sql中的?
ps.executeUpdate();//履行更新操作
ps.close();//關閉PreparedStatement
conn.close();//關閉Connection
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
response.sendRedirect("FindServlet");//重定向到FindServlet
}
}
在JDBC開發中,操作數據庫需要與數據庫建立連接,然后將要履行的SQL語句傳送到數據庫服務器,最后關閉數據庫連接。如果依照這個流程履行多條SQL語句,那末就需要建立多個數據庫連接,這樣會浪費很多時間。針對這1問題,JDBC的批處理提供了很好的解決方法。
JDBC中批處理的原理就是將批量的SQL語句1次性的發送到數據庫中履行,從而解決了屢次與數據庫連接所產生的速度問題。
范例:
創建學生信息表,通過JDBC的批處理操作,1次性將多個學生的信息寫入數據庫。
(1)創建student表
(2)創建名稱為Batch的類,實現對學生信息的批量添加。
package com.zgy.batchsql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class Batch {
/*
* 獲得數據庫連接
*
* @return Connection對象
*/
public Connection getConnection() {
Connection conn = null;// 數據庫連接
try {
Class.forName("com.mysql.jdbc.Driver");// 注冊驅動
String url = "jdbc:mysql://localhost:3306/zhouguanya";// 連接數據庫的字符串
String user = "root";// 數據庫用戶名
String password = "root";// 數據庫密碼
conn = DriverManager.getConnection(url, user, password);// 創建Connection對象
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/*
* 批量添加數據
*
* @return 所影響的行數
*/
public int saveBatch(){
int row = 0;
Connection conn = getConnection();
try{
String sql = "insert into student(student_id,student_name,student_age,student_class) values (?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
Random random = new Random();
for(int i = 0 ; i < 20 ; i++){
ps.setInt(1, i+1);//對student_id賦值
ps.setString(2, "學生"+(i+1));//對studet_name賦值
ps.setInt(3, random.nextInt(5)+10);//對student_age賦值
ps.setString(4, "班級"+(i+1));//對student_class賦值
ps.addBatch();//添加批處理
}
int[] rows = ps.executeBatch();//履行批處理操作返回計數組成的數組
row = rows.length;//對行進行賦值
ps.close();
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
return row;
}
}
(3)創建batchsql.jsp頁面,在該頁面中使用<jsp:useBean>實例化Batch,并履行批量添加數據的操作。
<%@ page language="java" import="java.util.*" pageEncoding="UTF⑻"%>
<%
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 'batchsql.jsp' starting page</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>
<jsp:useBean id="batch" class="com.zgy.batchsql.Batch"></jsp:useBean>
<%
int row = batch.saveBatch();
out.print("批量插入了"+row+"條數據");
%>
</body>
</html>
下一篇 TOMCAT 安裝環境配置