1.EditTable.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>EditTable</title>
<link rel="stylesheet" type="text/css" href="css/editTable.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/editTable.js"></script>
</head>
<body>
<table>
<thead>
<tr>
<th colspan="3">可编辑的表格</th>
</tr>
</thead>
<tbody>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
<%
String sno,name;
int age = 0;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select sno,name,age from student");
while(rs.next()){
sno = rs.getString("sno");
name = rs.getString("name");
age = rs.getInt("age");
%>
<tr>
<td id="sno"><%=sno%></td>
<td id="name"><%=name %></td>
<td id="age"><%=age %></td>
</tr>
<%}%>
</tbody>
</table>
<div id="result"></div>
</body>
</html>
2.editTable.css
table{
border:1px solid black;
border-collapse:collapse;
width:400px;
}
table td{
border:1px solid black;
width:33%;
}
table th{
border:1px solid black;
width:33%;
}
tbody th{
background-color:#7998DF;
}
#result{
color:red;
}
3.editTable.js
//在页面加载时,让所有的td拥有一个点击事件
4.DBcon.java
package com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
class DBCon{
private static Connection con = null;
public DBCon(){
}
public synchronized static Connection getConnection()
{
try{
Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
if(con == null)
con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
System.out.println("Connect Successfully");
}catch(SQLException e1){
e1.printStackTrace();
}
return con;
}
}
5.CRUD.java
package com.db;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CRUD {
public static int updateStudent(String sno, String option,String value){
int flag = 0;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "update student set " + option + "=? where sno=?";
try{
pstmt = DBCon.getConnection().prepareStatement(sql);
pstmt.setString(1,value);
pstmt.setString(2,sno);
flag =pstmt.executeUpdate();
}catch(SQLException e){
e.printStackTrace();
}
return flag;
}
}
6.UpdateStudent.java
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String sno = request.getParameter("sno");
String option = request.getParameter("option");
String value = request.getParameter(option);
int flag=CRUD.updateStudent(sno, option, value);
if(flag > 0)
out.println("更新成功!");
else
out.println("更新失败!");
out.flush();
out.close();
}
7.效果