gpt4 book ai didi

Java-MySQL : Command from another class

转载 作者:行者123 更新时间:2023-11-29 19:03:41 26 4
gpt4 key购买 nike

我有两个关于在 Java 中使用 MySQL 的问题。我需要在程序的不同类中使用不同的值,所以我的第一个问题是我是否需要在每个类中建立连接,或者我可以在我的主类中只做一次吗?其次,我不明白如何从另一个类调用 SQL 命令。我尝试过做一个prepareStatement,但它似乎不起作用。

我尝试过执行以下操作。首先,我创建了一个 DBConnection 类(我删除了真实的用户和密码值):

public class DBConnection {
public void connect() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
String connectionUrl = "jdbc:mysql://vhost01.srv.aau.dk/db_name";
String user = "user";
String password = "password";

Connection conn = DriverManager.getConnection(connectionUrl, user, password);
System.out.println("Connection established");

然后我从我的 main 中调用:

DBConnection dbConn = new DBConnection();
dbConn.connect();

然后在我的 LoginController 中,我想通过将输入的密码和用户名与数据库中的值进行比较来验证输入的密码和用户名。我尝试通过执行以下操作来遵循 Stack Overflow 上另一个问题的答案:

    Connection conn = null;
PreparedStatement pstmt = null;

try{
String SQL = "SELECT `username`,`password` FROM `Clinician` WHERE `username` =? AND `password` = ?;";
pstmt = conn.prepareStatement(SQL);
pstmt.setString(1, txtUsername.getText());
pstmt.setString(2, String.valueOf(txtPassword.getText()));
ResultSet result = pstmt.executeQuery();

if (result.next()){
lblMessage.setText("Access confirmed!");
}else{
lblMessage.setText("Access denied!");
}
}

catch(SQLException e){

}

finally{
pstmt.close();
}

但是这不起作用。我确实通过 main 建立了连接,但它不验证登录。我是否错过了什么,或者做了什么完全错误的事情?

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at Multimodality.view.LoginController.btnLoginAction(LoginController.java:77)
... 58 more

提前致谢!

最佳答案

您有两个名为 conn 的变量。第一个仅在 connect() 方法中可用,第二个为 null。因此,pstmt = conn.prepareStatement(SQL); 行中应该存在 NullPointerException。

您必须将preparedStatement-stuff移到您的DBConnection类中,或者您的connect()方法必须返回连接,以便您可以使用Connection conn = dbConn.connect(); 在你的 Controller 类中。

更新:

public Connection connect()...
....
Connection conn = DriverManager.getConnection(connectionUrl, user, password);
System.out.println("Connection established");
return conn;
}

在 Controller 类中:

DBConnection dbConn = new DBConnection();
Connection conn = dbConn.connect();

PreparedStatement pstmt = null;

try{
String SQL = "SELECT `username`,`password` FROM `Clinician` WHERE `username` =? AND `password` = ?;";
pstmt = conn.prepareStatement(SQL);

关于Java-MySQL : Command from another class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com