纯JSP 增删改查CRUD MYSQL UTF8MB4中文

查index.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
	String id = request.getParameter("id");
String driver = "com.mysql.cj.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/student?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
String userid = "root";
String password = "root";
try {
	Class.forName(driver);
} catch (ClassNotFoundException e) {
	e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<html>
<body>
	<h1>Retrieve data from database in jsp</h1>
	<a href="create.jsp"><button
						type="button" class="add">增加</button></a>
	<table border="1">
		<tr>
			<td>id</td>
			<td>first name</td>
			<td>last name</td>
			<td>City name</td>
			<td>Email</td>
			<td>update</td>
		</tr>
		<%
			try {
			connection = DriverManager.getConnection(connectionUrl, userid, password);
			statement = connection.createStatement();
			String sql = "select * from users";
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
		%>
		<tr>
			<td><%=resultSet.getString("id")%></td>
			<td><%=resultSet.getString("first_name")%></td>
			<td><%=resultSet.getString("last_name")%></td>
			<td><%=resultSet.getString("city_name")%></td>
			<td><%=resultSet.getString("email")%></td>
			<td>
			<a href="update.jsp?id=<%=resultSet.getString("id")%>"><button
						type="button" class="update">修改</button></a>
			<a href="delete.jsp?id=<%=resultSet.getString("id")%>"><button
						type="button" class="delete">删除</button></a>
			</td>
		</tr>
		<%
			}
		connection.close();
		} catch (Exception e) {
		e.printStackTrace();
		}
		%>
	</table>
</body>
</html>

 

增create.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
%>

<!DOCTYPE html>
<html>
<body>
	<form method="post" action="create-process.jsp">
		First name:<br> <input type="text" name="first_name"> <br>
		Last name:<br> <input type="text" name="last_name"> <br>
		City name:<br> <input type="text" name="city_name"> <br>
		Email Id:<br> <input type="email" name="email"> <br>
		<br> <input type="submit" value="submit">
	</form>
</body>
</html>

 

增处理create-process.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
%>

<%@page import="java.sql.*,java.util.*"%>

<%
	String first_name = request.getParameter("first_name");
String last_name = request.getParameter("last_name");
String city_name = request.getParameter("city_name");
String email = request.getParameter("email");

try {
	Class.forName("com.mysql.cj.jdbc.Driver");
	Connection conn = DriverManager.getConnection(
	"jdbc:mysql://localhost:3306/student?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
	"root", "root");
	Statement st = conn.createStatement();

	int i = st.executeUpdate("insert into users(first_name,last_name,city_name,email)values('" + first_name + "','"
	+ last_name + "','" + city_name + "','" + email + "')");
	out.println("Data is successfully inserted!");
} catch (Exception e) {
	System.out.print(e);
	e.printStackTrace();
}
%>

 

改update.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
%>

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
	String id = request.getParameter("id");
String driver = "com.mysql.cj.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/student?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
String userid = "root";
String password = "root";
try {
	Class.forName(driver);
} catch (ClassNotFoundException e) {
	e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<%
	try {
	connection = DriverManager.getConnection(connectionUrl, userid, password);
	statement = connection.createStatement();
	String sql = "select * from users where id=" + id;
	resultSet = statement.executeQuery(sql);
	while (resultSet.next()) {
%>
<!DOCTYPE html>
<html>
<body>
	<h1>Update data from database in jsp</h1>
	<form method="post" action="update-process.jsp">
		<input type="hidden" name="id" value="<%=resultSet.getString("id")%>">
		<input type="text" name="id" value="<%=resultSet.getString("id")%>">
		<br> First name:<br> <input type="text" name="first_name"
			value="<%=resultSet.getString("first_name")%>"> <br>
		Last name:<br> <input type="text" name="last_name"
			value="<%=resultSet.getString("last_name")%>"> <br>
		City name:<br> <input type="text" name="city_name"
			value="<%=resultSet.getString("city_name")%>"> <br>
		Email Id:<br> <input type="email" name="email"
			value="<%=resultSet.getString("email")%>"> <br>
		<br> <input type="submit" value="submit">
	</form>
	<%
		}
	connection.close();
	} catch (Exception e) {
	e.printStackTrace();
	}
	%>
</body>
</html>

 

改处理update-process.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
%>

<%@ page import="java.sql.*"%>
<%!String driverName = "com.mysql.cj.jdbc.Driver";%>
<%!String url = "jdbc:mysql://localhost:3306/student?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";%>
<%!String user = "root";%>
<%!String psw = "root";%>
<%
	String id = request.getParameter("id");
String first_name = request.getParameter("first_name");
String last_name = request.getParameter("last_name");
String city_name = request.getParameter("city_name");
String email = request.getParameter("email");
if (id != null) {
	Connection con = null;
	PreparedStatement ps = null;
	int personID = Integer.parseInt(id);
	try {
		Class.forName(driverName);
		con = DriverManager.getConnection(url, user, psw);
		String sql = "Update users set id=?,first_name=?,last_name=?,city_name=?,email=? where id=" + id;
		ps = con.prepareStatement(sql);
		ps.setString(1, id);
		ps.setString(2, first_name);
		ps.setString(3, last_name);
		ps.setString(4, city_name);
		ps.setString(5, email);
		int i = ps.executeUpdate();
		if (i > 0) {
	out.print("Record Updated Successfully");
		} else {
	out.print("There is a problem in updating Record.");
		}
	} catch (SQLException sql) {
		request.setAttribute("error", sql);
		out.println(sql);
	}
}
%>

 

删delete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.sql.*,java.util.*"%>
<%
	String id = request.getParameter("id");
try {
	Class.forName("com.mysql.cj.jdbc.Driver");
	Connection conn = DriverManager.getConnection(
	"jdbc:mysql://localhost:3306/student?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
	"root", "root");
	Statement st = conn.createStatement();
	int i = st.executeUpdate("DELETE FROM users WHERE id=" + id);
	out.println("Data Deleted Successfully!");
} catch (Exception e) {
	System.out.print(e);
	e.printStackTrace();
}
%>

表SQL

CREATE TABLE IF NOT EXISTS `users` (
  `id` int NOT NULL AUTO_INCREMENT,
  `first_name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `last_name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `city_name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoD

 

上一篇:SpringBoot启动原理


下一篇:MyBatis.3.CRUD