JSP 和 Servets(我需要帮助)

问题描述 投票:0回答:1

我想制作一个银行管理系统,所以我编写了所有代码,但最后一个代码(使用带有jdbc数据库活动的html表)不起作用,所以我的程序不起作用,这是我的代码

Userdb.java =

public class Userdb {
    
        public ArrayList<User> getAllUser() throws SQLException{
            
            ArrayList<User> users = new ArrayList<User>();
            String query = "Select firstName,password from login";
            DbHelper db = new DbHelper();
            
                    try {
                    Connection connection = db.getConnection();
                PreparedStatement statement = connection.prepareStatement(query);
                    ResultSet resultSet = statement.executeQuery();
                    while(resultSet.next()) {
                            
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    String surname = resultSet.getString("surname");
                    String username = resultSet.getString("username");
                    String password = resultSet.getString("password");
                    String email = resultSet.getString("email");
                User user = new User(id,name,surname,username,password,email);
                            users.add(user);
                            
                        }
                        
                        
                    }catch(SQLException e) {
                            db.getErrorMessages(e);
                    }
                    
                    return users;   
                    
        }

DbHelper.java =

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbHelper {
    
    
    
    public Connection getConnection() throws SQLException{
        String dbUrl = "jdbc:mysql://localhost:3306/classicmodels";
        String name = "root";
        String password = "1234";
        return DriverManager.getConnection(dbUrl,name,password);
    }

    
    
    public void getErrorMessages(SQLException e) {
        System.out.println("Error Code : "+e.getErrorCode());
        System.out.println("Error Messages : "+e.getMessage());
    }
}

用户.java =


public class User {

    private int id;
    private String name;
    private String surname;
    private String username;
    private String password;
    private String email;
    
    public User() {
        
    }
    public User(int id,String name,String surname,String username,String password,String email) {
        this.setId(id);
        this.setName(name);
        this.setSurname(surname);
        this.setUsername(username);
        this.setPassword(password);
        this.setEmail(email);
        
    }
        
    
    public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    

     public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getSurname() {
            return surname;
        }

        public String getUsername() {
            return username;
        }

        public String getEmail() {
            return email;
        }

        
        public String getPassword() {
            return password;
        }
    
    
    
    
    
    
    
}

登录成功管理.jsp =

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>    
<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
    
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        
        th, td {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }
        
        th {
            background-color: #f2f2f2;
        }
    </style>
   
</head>
<body>
    
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="#">Admin Panel</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
            aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="loginPage.jsp"><i class="fas fa-sign-out-alt fa-lg"></i> Logout</a>
                </li>
            </ul>
        </div>
    </nav>
    
    <!-- Orta Alan -->
    <div class="container-fluid d-flex justify-content-center align-items-center h-100">
        <div class="row">
           <h1>User List</h1>
        <form action="AdminServlet" method="post">
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Surname</th>
                    <th>Username</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <%
                Userdb userdb = new Userdb();
                                                    ArrayList<User> users = userdb.getAllUser();
                                                    for (User user : users) {
                %>
                <tr>
                    <td><%= user.getId() %></td>
                    <td><%= user.getName() %></td>
                    <td><%= user.getSurname() %></td>
                    <td><%= user.getUsername() %></td>
                    <td><%= user.getEmail() %></td>
                </tr>
                <% } %>
            </tbody>
        </table>
        </form>
        </div>
    </div>
    
    <!-- Bootstrap JS ve jQuery -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    
</body>
</html>

我保证我的数据库工作完美,问题不是由数据库引起的,你们有什么建议给我吗?

java mysql performance jsp servlets
1个回答
0
投票

JSP 代码无法编译,因为您没有通过

Userdb
指令导入类
User
@page import

第二个问题是在方法

Userdb
中的
getAllUser()
类中。
Connection
对象应在方法结束时关闭。如果您没有关闭连接,它将保持活动状态并保留内存,这会造成内存泄漏(也称为资源泄漏)。在java 8及以上版本中,该对象实现了
AutoClosable
接口,并且可以与 try-with-resource 语句一起使用。这与
Statement
ResultSet
相关,您应该相应地关闭。但是,如果您显式关闭连接,那么这些对象也会关闭。

try (Connection connection = db.getConnection();) {
  //your code here                         
}catch(SQLException e) {
  db.getErrorMessages(e);
}     
© www.soinside.com 2019 - 2024. All rights reserved.