java gettext()返回用户名的空字符串

z9smfwbn  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(198)

我对此不知所措。我正试图通过javafx连接到他为我们建立的教授数据库。当我编译代码并运行它时,一切正常,直到我按下登录按钮。出于某种原因,它没有在username字段中捕获文本,我也将在password字段中进行假设。
我得到这个错误:

jfx-project-run:
     [echo] Executing H:\CST 3613\Projects\FXDatabase\dist\run2081948515\FXDatabase.jar using platform C:\Program Files\Java\jdk1.8.0_271/bin/java
     [java] Driver loaded
     [java] com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. ClientConnectionId:397db8ac-9ec6-4410-9008-17066cc2b031
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262)
     [java]     at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:283)
     [java]     at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:129)
     [java]     at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:37)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:5233)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3988)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:85)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3932)
     [java]     at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7375)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3206)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2713)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2362)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276)
     [java]     at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861)
     [java]     at java.sql.DriverManager.getConnection(DriverManager.java:664)
     [java]     at java.sql.DriverManager.getConnection(DriverManager.java:247)
     [java]     at fxdatabase.DBUtil.dbConnect(DBUtil.java:14)
     [java]     at fxdatabase.FXDatabase.lambda$start$0(FXDatabase.java:48)
     [java]     at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
     [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
     [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
     [java]     at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
     [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
     [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
     [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
     [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
     [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
     [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
     [java]     at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
     [java]     at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
     [java]     at javafx.event.Event.fireEvent(Event.java:198)
     [java]     at javafx.scene.Node.fireEvent(Node.java:8411)
     [java]     at javafx.scene.control.Button.fire(Button.java:185)
     [java]     at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
     [java]     at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
     [java]     at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
     [java]     at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
     [java]     at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
     [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
     [java]     at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
     [java]     at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
     [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
     [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
     [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
     [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
     [java]     at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
     [java]     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
     [java]     at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
     [java]     at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
     [java]     at javafx.event.Event.fireEvent(Event.java:198)
     [java]     at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
     [java]     at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
     [java]     at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
     [java]     at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
     [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
     [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
     [java]     at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
     [java]     at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
     [java]     at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
     [java]     at com.sun.glass.ui.View.notifyMouse(View.java:937)
     [java]     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
     [java]     at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:187)
     [java]     at java.lang.Thread.run(Thread.java:748)

由于出现“用户“”登录失败”的错误,我假设 getText() 用户名和密码字段的函数返回一个空字符串。有人能帮我弄明白为什么这样不行吗?项目代码粘贴在下面。

import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;

public class FXDatabase extends Application {
    private static final String JDBCDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String connStr = "jdbc:sqlserver://s16988308.onlinehome-server.com:1433;databaseName=CUNY_DB";
    private static Connection connection = null;
    final Label usernameLabel = new Label("Login ID:");
    final TextField username = new TextField();
    final Label passwordLabel = new Label("Password:");
    final PasswordField password = new PasswordField();
    final Label courses = new Label("Courses:");
    final TextArea courseDisplay = new TextArea();
    final Button loginBtn = new Button("Login");

    @Override
    public void start(Stage primaryStage) {
        DBUtil utility = new DBUtil();
        double height = 200.0, width = 210.0;
        StackPane stackPane = new StackPane();

        // GridPane elements
        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.TOP_CENTER);
        gridPane.setPadding(new Insets(20));
        gridPane.setHgap(5);
        gridPane.setVgap(5);
        gridPane.add(usernameLabel, 0, 0, 1, 1); // position at cell 0, 0 and span 1 row, 1 column
        gridPane.add(username, 1, 0, 1, 1);
        gridPane.add(passwordLabel, 0, 1);
        gridPane.add(password, 1, 1);
        GridPane.setHalignment(loginBtn, HPos.RIGHT);
        gridPane.add(loginBtn, 1, 3, 1, 1); // position at cell 1, 3 and span 1 row, 1 column
        gridPane.add(courses, 0, 5);
        gridPane.add(courseDisplay, 0, 6, 2, 1);

        loginBtn.setOnAction(e -> {
            try {
                utility.dbConnect();
                //login();
                //executeQuery();
            }
            catch (SQLException | ClassNotFoundException a) {
                a.printStackTrace();
            }
        });

        // Add everything to stack pane
        stackPane.getChildren().addAll(gridPane);

        // Set up scene and stage
        Scene scene = new Scene(stackPane, 300, 350);
        primaryStage.setTitle("CUNY Database Login");
        primaryStage.initStyle(StageStyle.DECORATED);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
package fxdatabase;
import java.sql.*;

public class DBUtil extends FXDatabase {
    private static final String JDBCDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String connStr = "jdbc:sqlserver://s16988308.onlinehome-server.com:1433;databaseName=CUNY_DB";
    private static Connection connection = null;

    public void dbConnect() throws SQLException, ClassNotFoundException {
        // Load driver
        Class.forName(JDBCDriver);
        System.out.println("Driver loaded");

        connection = DriverManager.getConnection(connStr, username.getText(), password.getText());
        System.out.println("Database connected");
    }

    public static void dbDisconnect() throws SQLException {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        }
        catch (Exception e) {
            throw e;
        }
    }

    // Execute database query
    /*public static void dbExecuteQuery(String sqlStatement) throws SQLException, ClassNotFoundException {

        try {
            dbConnect();
            stmt = connection.createStatement();
            stmt.executeUpdate(sqlStatement);
        }
        catch (SQLException e) {
            System.out.println("Problem occured at dbExecuteQuery operation " + e);
            throw e;
        }
        finally {
            if (stmt != null) {
                stmt.close();
            }
            dbDisconnect();
        }
    }

    // Retrieve database records
    public static ResultSet dbExecute(String sqlQuery) throws SQLException, ClassNotFoundException {
        Statement stmt = null;
        ResultSet resultSet = null;
        CachedRowSetImpl cachedRSI = null;

        try {
            dbConnect();
            stmt = connection.createStatement();
            resultSet = stmt.executeQuery(sqlQuery);
            cachedRSI = new CachedRowSetImpl();
            cachedRSI.populate(resultSet);
        }
        catch (SQLException e) {
            System.out.println("Error occured in the dbExecute operation " + e);
            throw e;
        }
        finally {
            if (resultSet != null)
                resultSet.close();

            if (stmt != null)
                stmt.close();

            dbDisconnect();
        }

        return cachedRSI;
    }*/
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题