我們最後一天來做個JSP連接資料庫資料操作八~
創建一個findperson.html的檔案
裡面用上form(html的一種格式)即可
// findPerson.html
<form action="/findPerson">
<!--創建一個標籤-->
<label>Person Id:</label>
<!--設定一個輸入格式為數值的欄位-->
<input type="number" name="personId">
<input type="submit" value="search">
</form>
基本介面這樣就完成了,但是我們點擊search的時候,因為沒有連接資料庫所以就顯示404 Error了
先去建立一個package(person),在裡面建立一個PersonServlet
接著再我們的web.xml加上servler和servlet-mapping
// web.xml
<servlet>
<servlet-name>personServlet</servlet-name>
<servlet-class>person.personServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>personServlet</servlet-name>
<url-pattern>/findPerson</url-pattern>
</servlet-mapping>
因為等等我們會需要用到資料庫裏面各個欄位的值(id, name, age),我們先幫他建立一個Person.class
// Person.class
package person;
public class Person {
private int personId;
private String personName;
private int age;
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
接著再我們的personServlet中,加上private PreparedStatement ps;
然後再寫一個JDBC初始化的方法,去doGet裡面先初始化JDBC,再去設定"?"的引數為"personId",因為executeQuery()會回傳查詢到的數值,所以在設定它為ResultSet
接著建立Person物件為null,再以rs是否有輸入去設定person裡面的屬性,後面再把request和response forward到person.jsp裡面
// personServlet.java
package person;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.sql.*;
@WebServlet(name = "personServlet", value = "/personServlet")
public class personServlet extends HttpServlet {
private PreparedStatement ps;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
initJDBC();
try {
// 設定"?"的引數
ps.setString(1, request.getParameter("personId"));
ResultSet rs = ps.executeQuery(); // 回傳找到的數值,所以我們設為ResultSet
Person person = null;
if (rs.next()){
person = new Person();
person.setPersonId(rs.getInt("personId"));
person.setPersonName(rs.getString("personName"));
person.setAge(rs.getInt("age"));
}
request.setAttribute("person", person);
request.getRequestDispatcher("/person.jsp").forward(request, response);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
// 初始化JDBC的方法
private void initJDBC() {
try {
Class.forName("con.mysql.jdbc.Driver");
System.out.println("loading driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8081/maxdb", "Max", "12345678");
System.out.println("connecting database");
ps = conn.prepareStatement("select * from person where personId = ?");
} catch (ClassNotFoundException | SQLException e) {
e.getStackTrace();
throw new RuntimeException(e);
}
}
}
我們再建立一個我們前面會需要的person.jsp,$(person.getsomething())
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>The Person information: </p>
<p>Name: $(person.getName())</p>
<p>Id: $(person.getPersonId())</p>
<p>Age: $(person.getAge())</p>
</body>
</html>
接著再去執行我們的程式就可以跑出結果拉~