团队冲刺个人博客4
今日份代码:
neirong.jsp
<%-- Created by IntelliJ IDEA. User: pangshaohang Date: 2023/4/10 Time: 19:45 To change this template use File | Settings | File Templates. --%> <%@ page language= "java" contentType= "text/html; charset=UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <head> <meta charset="UTF-8" > <%@ page import="java.sql.*"%> <title>内容展示页面</title> </head> <body> <table border="1px" align="center"> <% String name= request.getParameter("name"); %> <%out.print(name); %></td></tr> <% String URL = "jdbc:mysql://localhost:3306/test"; String USERNAME = "root"; String PWD = "123456"; //jsp就是在html中嵌套的java代码,因此java代码可以写在jsp中(《%%》) PreparedStatement pstmt = null; ResultSet rs=null; Connection connection = null; try { // a.导入驱动,加载具体驱动类 Class.forName("com.mysql.cj.jdbc.Driver");//注册// 加载具体的驱动类 // b.与数据库建立链接 connection = DriverManager.getConnection(URL, USERNAME, PWD); String sql ="select * from policy where name like ? " ; pstmt=connection.prepareStatement(sql); pstmt.setString(1, "%"+name+"%"); rs= pstmt.executeQuery();// 反回值,增删改了几条数据增删改用update while(rs.next()) { //政策名称 String text=rs.getString("text"); out.print(text);} } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭数据库 try{//先开的后关 if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close();// 对象.方法 if(connection!=null)connection.close(); } catch( SQLException e){ e.printStackTrace(); } } %> </table> </body>
浙公网安备 33010602011771号