我只是使用sqlite数据库在javafx中创建登录应用程序但是当我点击它不显示登录berhasil时,我该怎么办?我一直在寻找,但我没有得到针对我的问题的特定答案。
这是我的登录控制器
package sample;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class Controller implements Initializable{
@FXML
private JFXTextField username;
@FXML
private JFXPasswordField password;
@FXML
private Label pemberitahuan;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void loginButton(ActionEvent event) throws Exception {
String user = username.getText();
String pass = password.getText();
try {
DbConnection database = new DbConnection();
Connection connection = database.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM admin");
while (resultSet.next()) {
if (user.equals(resultSet.getString('2'))
&& pass.equals(resultSet.getString('3'))) {
System.out.println("login berhasil");
pemberitahuan.setText("login berhasil");
}
}
}catch (SQLException ex) {
ex.printStackTrace();
}
}
}
db connection class
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Created by agungaprian on 30/04/17.
*/
public class DbConnection {
Connection connection;
String username = "root";
String password = "";
String url = "jdbc:sqlite:posapp.db";
public Connection getConnection () throws SQLException {
try{
connection = DriverManager.getConnection(url,username,password);
System.out.println("connect to database");
}catch (SQLException ex) {
System.out.println("not connected to database");
ex.printStackTrace();
}
return connection;
}
}
fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<HBox prefHeight="400.0" prefWidth="600.0">
<children>
<VBox prefHeight="400.0" prefWidth="600.0">
<children>
<AnchorPane prefHeight="400.0" prefWidth="600.0">
<children>
<Text layoutX="153.0" layoutY="78.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Login Test Sqlite" AnchorPane.leftAnchor="153.0" AnchorPane.rightAnchor="147.380859375">
<font>
<Font size="36.0" />
</font>
</Text>
<Separator layoutX="121.0" layoutY="113.0" prefHeight="0.0" prefWidth="379.0" AnchorPane.leftAnchor="121.0" AnchorPane.rightAnchor="100.0" />
<GridPane layoutX="172.0" layoutY="149.0" prefHeight="152.0" prefWidth="261.0" AnchorPane.leftAnchor="172.0" AnchorPane.rightAnchor="167.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="124.0" minWidth="10.0" prefWidth="85.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="183.0" minWidth="10.0" prefWidth="176.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<JFXTextField fx:id="username" GridPane.columnIndex="1" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="username" />
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="password" GridPane.rowIndex="1" />
<JFXPasswordField fx:id="password" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<JFXButton onAction="#loginButton" prefHeight="25.0" prefWidth="69.0" style="-fx-background-color: #aaa999;" text="Button" textFill="#212121" GridPane.columnIndex="1" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
</children>
</GridPane>
<Label fx:id="pemberitahuan" layoutX="323.0" layoutY="326.0" prefHeight="15.0" prefWidth="117.0" textFill="#cd0909" AnchorPane.leftAnchor="323.0" AnchorPane.rightAnchor="160.0" />
</children>
</AnchorPane>
</children>
</VBox>
</children>
</HBox>
</children>
</AnchorPane>