基于套用了网站模板,然后连接了数据库
具体实现如下:
1、定义一个实体类Uesr
package org.example;
public class User {
private String name;
private String id;
private String teacher;
private String whe;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setTeacher(String teacher){
this.teacher=teacher;
}
public String getTeacher(){
return teacher;
}
public void setWhe(String whe){
this.whe=whe;
}
public String getWhe(){
return whe;
}
}
2、在jsp文件中写入java代码,实现连接数据库的操作(注意,数据库里面的列名要与你定义的User类一致)
<%
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ad?useUnicode=true&characterEncoding=utf-8", "root", "2021");
//使用Statement对象
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from we");
while (rs.next()) {
out.println("<tr><th scope=row >"+rs.getString(1)+"</th><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>" + rs.getString(4) +"</td></tr>");}
rs.close();
stmt.close();
con.close();
%>
查看结果,功能实现!