Using database (mysql) in JSP

Use mysql database in JSP

Test 1: Read and query the contents of the database

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<% 
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl="jdbc:mysql://localhost:3306/yefan?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="zhangqian5yefan";
Connection con=DriverManager.getConnection(connectionUrl,username,password);
String SQL="SELECT ID,site_name,site_url from data_01";
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(SQL);
%>
<table border="1" bgcolor="cyan">
<tr>
<th>ID</th>
<th>站点名</th>
<th>站点地址</th>
</tr>
<%
int i=0;
while(rs.next()){
i++;
%>
<tr>
<td><%=i%></td>
<td><%=rs.getString("site_name")%></td>
<td><%=rs.getString("site_url")%></td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>

Insert picture description here

Test 2: Insert data into the database

  1. First, you need to create a new data table in the database
  2. Need to have a JSP file submitted to the database page
  3. Update database content
Insert picture description here

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<body>
<form action="insert.jsp" method="get">
信息提交:<br>
<table border="1" bgcolor="cyan">
<tr><th>学号</th><tr>
<tr>
<td><input type="text" name="sno" value="2009211211"></td>
<td><input type="text" name="sname" value="张三"></td>
</tr><td><td colspan="2" align="center"><input type="submit" name="submit" value="提交"></td></tr>
</form>
<body>

insert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<% 
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl="jdbc:mysql://localhost:3306/yefan?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8";
String username="root";
String password="yefanyefan55";
Connection con=DriverManager.getConnection(connectionUrl,username,password);
String sno,sname;
sno=request.getParameter("sno");
sname=request.getParameter("sname");
String SQL="insert into student values('"+sno+"','"+sname+"')";
Statement stmt=con.createStatement();
int count=stmt.executeUpdate(SQL);
stmt.close();
SQL="select sno,sname from student";
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(SQL);
%>
<html>
<body>
数据插入成功!<br>
<table border="1" bgcolor="cyan">
<tr><th>序号</th><th align="center">学号</th><th align="center">姓名</th></tr>
<%
int i=0;
while (rs.next()){
i++;
%>
<tr>
<td><%=i%></td>
<td><%=rs.getString("sno")%></td>
<td><%=rs.getString("sname")%></td>
</tr>
<%
}
stmt.close();
con.close();
%>
</table>
</body>
</html>
Insert picture description here


If you think it's good, please like it~