在我的Java应用程序中,我需要使用连接池,所以我决定实现HibernateCP。问题是我得到这个异常,我需要添加以下内容:org/slf4j/LoggerFactory。完整的例外是:
INFO: HHH000490: Using JTA platform [org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform]
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.zaxxer.hikari.HikariConfig.<clinit>(HikariConfig.java:48)
at Database_Managment.DatabaseConfig.<clinit>(DatabaseConfig.java:16)
at Database_Managment.DBFunctionality.checkUser(DBFunctionality.java:45)
at DTOS.LogInView$4.mouseClicked(LogInView.java:221)
at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:278)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6629)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4584)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 35 more
我尝试了很多方法和不同的规范等等,但我还没有了解Maven或依赖项,所以我有基础知识,我可能错过了一些东西。
我正在从 IntelliJ IDEA 运行该项目。我不知道该项目是否也从命令行收到错误。该错误发生在 LogIn 界面中,并且在仅使用 main 方法运行导致错误的地方时也会发生。
相关课程:
数据库配置:
package Database_Managment;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class DatabaseConfig {
private static HikariDataSource dataSource;
static {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/storefront");
try {
Properties accessProperties = new Properties();
accessProperties.load(new FileInputStream("C:\\Users\\andre\\IdeaProjects\\demo\\Project-Poster\\src\\user.properties"));
//prop. to access the host
config.setUsername(accessProperties.getProperty("username"));
config.setPassword(accessProperties.getProperty("password"));
} catch (IOException e) {
throw new RuntimeException(e);
}
config.setMaximumPoolSize(1); // Adjust the pool size based on your needs
config.setIdleTimeout(60000); // Connection idle time
config.setConnectionTimeout(30000); // Connection timeout
dataSource = new HikariDataSource(config);
}
public static DataSource getConnectionPool() {
return dataSource;
}
}
数据库功能:
package Database_Managment;
import Client.Person;
import Server.ServerView;
import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public final class DBFunctionality {
static String checkUserQuery = "SELECT username FROM storefront.users WHERE username = ? AND password = ?";
public static void manageClientData(String username, String password, String iconId, String status, String profileLink, boolean isNew, Person person) {
if (isNew) {
var ds = new MysqlDataSource();
ds.setServerName("localhost");
ds.setPort(3306);
ds.setUser("And_GG");
ds.setPassword("Sample Password"); //Can't show the real password
try (Connection conn = ds.getConnection(); Statement st = conn.createStatement()) {
String[] joinedValues = {username, password, profileLink, iconId, status};
String insertQuery = "INSERT INTO storefront.users (username, password, link, img, status) VALUES (?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(insertQuery);
for (int i = 1; i <= joinedValues.length; i++) {
ps.setString(i, joinedValues[i - 1]);
}
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
new ServerView(person.getTimeJoined(), person);
}
public static boolean checkUser(String username, String password) {
try (Connection conn = DatabaseConfig.getConnectionPool().getConnection();
PreparedStatement ps = conn.prepareStatement(checkUserQuery)) {
ps.setString(1, username);
ps.setString(2, password);
var rs = ps.getResultSet();
if (rs.isBeforeFirst()) return false;
} catch (SQLException e) {
//Not handle
}
return true;
}
}
LogInView(仅相关部分):
//Instances that will constantly be re-used by the method under them
EntityManager entityManager = DatabaseEM.getEm();
EntityTransaction transaction = entityManager.getTransaction();
//if the user doesn't exist, we do nothing, else, we get it from the db. and init. the ServerView
confirmButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!DBFunctionality.checkUser(nameBar.getText(), linkLabel2.getText())) return;
String jpql = "SELECT user FROM User user WHERE user.username = ?1 AND user.password = ?2";
try {
transaction.begin();
var query = entityManager.createQuery(jpql, User.class);
query.setParameter(1, nameBar.getText());
query.setParameter(2, linkLabel2.getText());
var user = query.getSingleResult();
if (user != null) {
new ServerView(LocalTime.now(), new Person(user.getUsername(), user.getPassword(), user.getImg(), user.getStatus(), user.getLink(), false, false));
}
transaction.commit();
} catch (Exception ex) {
//not handle
}
}
});
这是我的 Maven pom.xml 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.21.Final</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
</project>