JSP+JavaBean

在这里插入图片描述
一个简单的JavaBean实例,包含属性、构造函数,getter函数、setter函数

package com.runoob;

public class StudentsBean implements java.io.Serializable
{
    
    
   private String firstName = null;
   private String lastName = null;
   private int age = 0;

   public StudentsBean() {
    
    
   }
   public String getFirstName(){
    
    
      return firstName;
   }
   public String getLastName(){
    
    
      return lastName;
   }
   public int getAge(){
    
    
      return age;
   }

   public void setFirstName(String firstName){
    
    
      this.firstName = firstName;
   }
   public void setLastName(String lastName){
    
    
      this.lastName = lastName;
   }
   public void setAge(int age) {
    
    
      this.age = age;
   }
}

在这里插入图片描述

name属性指的是Bean的id属性。property属性指的是想要调用的getter或setter方法。

一个简单的JSP+JavaBean的实例如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>get 和 set 属性实例</title>
</head>
<body>

<jsp:useBean id="students" 
                    class="com.runoob.StudentsBean"> 
   <jsp:setProperty name="students" property="firstName"
                    value="小强"/>
   <jsp:setProperty name="students" property="lastName" 
                    value="王"/>
   <jsp:setProperty name="students" property="age"
                    value="10"/>
</jsp:useBean>

<p>学生名字: 
   <jsp:getProperty name="students" property="firstName"/>
</p>
<p>学生姓氏: 
   <jsp:getProperty name="students" property="lastName"/>
</p>
<p>学生年龄: 
   <jsp:getProperty name="students" property="age"/>
</p>

</body>
</html>

下面再说明一种JSP+JavaBean的方式

(1)创建conn类来封装对数据库的操作方法
(2)创建index.jsp来使用conn类中封装的方法

package com.gx.bean;

import java.sql.*;

public class conn {
    
    
    public Connection getConn(){
    
    
        Connection conn = null;
        try {
    
    
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3308/manage","数据库用户名","数据库密码");
        } catch (ClassNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (SQLException e){
    
    
            e.printStackTrace();
        }
        return conn;
    }
    public PreparedStatement prepare(Connection conn,String sql){
    
    
        PreparedStatement pstmt = null;
        try{
    
    
            if (conn != null) {
    
    
                pstmt = conn.prepareStatement(sql);
            }
        }catch (SQLException e){
    
    
            e.printStackTrace();
        }
        return  pstmt;
    }
    public Statement getStatement(Connection conn){
    
    
        Statement stmt = null;
        try{
    
    
            if (conn != null) {
    
    
                stmt = conn.createStatement();
            }
        }catch (SQLException e){
    
    
            e.printStackTrace();
        }
        return stmt;
    }
    public ResultSet getResultSet(Statement stmt,String sql){
    
    
        ResultSet rs = null;
        if(stmt!=null){
    
    
            try {
    
    
                rs = stmt.executeQuery(sql);
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
        return rs;
    }
    public void excuteUpdate(Statement stmt,String sql){
    
    
        if(stmt!=null){
    
    
            try {
    
    
                stmt.executeUpdate(sql);
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
        }
    }
    public void close(Connection conn){
    
    
        if(conn!=null){
    
    
            try {
    
    
                conn.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
            conn=null;
        }
    }
    public void close(ResultSet rs){
    
    
        if(rs!=null){
    
    
            try {
    
    
                rs.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
            rs=null;
        }
    }
    public void close(Statement stmt){
    
    
        if(stmt!=null){
    
    
            try {
    
    
                stmt.close();
            } catch (SQLException throwables) {
    
    
                throwables.printStackTrace();
            }
            stmt=null;
        }
    }
}

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: 12526
  Date: 2021/1/13
  Time: 13:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
  <head>
    <title>JSP+JavaBean实例</title>
  </head>
  <body>
  <jsp:useBean id="DB" class="com.gx.bean.conn" />
  <%
    Connection conn = DB.getConn();
    Statement stmt = DB.getStatement(conn);
    ResultSet rs = DB.getResultSet(stmt,"select * from users");
    try{
    
    
      while(rs.next()){
    
    
        out.println("<table>");
        out.println("<tr>");
        out.println("<tr>"+rs.getInt("id")+"</td>");
        out.println("<tr>"+rs.getString("name")+"</td>");
        out.println("<tr>"+rs.getString("password")+"</td>");
        out.println("</tr>");
        out.println("</table>");
      }
    }catch (SQLException e){
    
    
      e.printStackTrace();
    }finally {
    
    
      DB.close(conn);
      DB.close(stmt);
      DB.close(rs);
    }
  %>
  </body>
</html>

写到这里的时候遇到了一个tomcat报500的坑,就是使用out.println()的时候,对

标签和标签忘记写反斜杠结束符了,导致了报500的错误
在这里插入图片描述
添加完反斜杠后成功显示页面
在这里插入图片描述
可以看到成功将数据库的内容显示了出来
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45394002/article/details/112606025