1、怎么做?
/**
- @Author: 小雷学长
- @Date: 2022/3/16 - 10:06
- @Version: 1.8 */
import jakarta.servlet.Servlet; import jakarta.servlet.ServletException; import jakarta.servlet.*;
import java.io.IOException;
import java.io.IOException; import java.io.PrintWriter; import java.sql.*;
public class StudentServlet implements Servlet {
public void init(ServletConfig config)throws ServletException {}public void service(ServletRequest request, ServletResponse response)throws ServletException, IOException {/*打印到浏览器上*/response.setContentType("text/html");//返回outPrintWriter out = response.getWriter();/*编写JDBC代码,连接数据,查询所有信息*/Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try {//注册驱动Class.forName("com.mysql.cj.jdbc.Driver");//获取连接String url = "jdbc:mysql://localhost:3306/practice";String user = "root";String password = "123456";conn = DriverManager.getConnection(url, user, password);//获取预编译的数据库操作对象String sql = "select id,informant_name from t_student";ps = conn.prepareStatement(sql);//执行SQLrs = ps.executeQuery();//处理查询结果集while (rs.next()) {String id = rs.getString("id");String informant_name = rs.getString("informant_name");//打印outout.print(id + "," + informant_name + "<br>");}} catch (Exception e) {e.printStackTrace();} finally {//释放资源if (rs != null) {try {rs.close();} catch (Exception e) {e.printStackTrace();}}if (ps != null) {try {ps.close();} catch (Exception e) {e.printStackTrace();}}if (conn != null) {try {conn.close();} catch (Exception e) {e.printStackTrace();}}}}public void destroy() {}public String getServletInfo() {return "";}public ServletConfig getServletConfig() {return null;}
}
```
