I want to make a bank manegment system so I writing all the code but the last code (using HTML table with JDBC database activity) doesn't work, so my program doesn't work there is my code
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());
}
}
User.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;
}
}
loginSucceedAdmin.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>
I guarantee My database working perfect, the problem not caused by the database. Do you have any advice for me guys ?
The JSP code won't compile, because you didn't import classes Userdb
and User
by the @page import
directive. The syntax of this directive is similar to Java import statement. You can read more here Using Package Members.
<%@ page import="mypackage.Userdb" %>
<%@ page import="mypackage.User" %>
The second problem is in the Userdb
class in the method getAllUser()
. The Connection
object should be closed at the end of the method. If you didn't close connection it will stay active and keep memory, it imposes a memory leaks (also known as resource leaks). In the java 8 and above this object implements AutoClosable
interface and can be used with try-with-resources statement. The same is related to Statement
and ResultSet
that you should close accordingly. But if you close the connection explicitly then these objects are closed also.
try (Connection connection = db.getConnection();) {
//your code here
}catch(SQLException e) {
db.getErrorMessages(e);
}