I would like to test if my database is connected to my code, after running the code, there’s no output. This will be my first post here i hope someone will help me.
here are my codes:
App.java
`import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
try {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Login Page");
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
}
}
public static void main(String[] args) {
launch(args);
}
}
Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="456.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
<children>
<PasswordField fx:id="txtPassword" layoutX="159.0" layoutY="294.0" prefHeight="25.0" prefWidth="283.0" style="-fx-border-color: ec4848; -fx-border-radius: 50;" />
<Button fx:id="txtLogin" layoutX="159.0" layoutY="339.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="283.0" style="-fx-background-color: ec4848; -fx-border-radius: 50;" text="Login" textFill="WHITE" />
<TextField fx:id="txtUsername" layoutX="159.0" layoutY="242.0" prefHeight="25.0" prefWidth="283.0" style="-fx-border-color: ec4848; -fx-border-radius: 50;" />
<ImageView fitHeight="150.0" fitWidth="206.0" layoutX="225.0" layoutY="55.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../Untitled%20design%20(2).png" />
</image>
</ImageView>
<Text layoutX="159.0" layoutY="233.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Username:" />
<Text layoutX="159.0" layoutY="290.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Password:" />
<Text layoutX="257.0" layoutY="201.0" strokeType="OUTSIDE" strokeWidth="0.0" style="-fx-font-weight: 5;" text="Welcome, User!" />
<Label fx:id="isConnected" layoutX="283.0" layoutY="47.0" text="Status" />
</children>
</AnchorPane>
LoginController.java
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class LoginController implements Initializable{
public LoginModel loginModel = new LoginModel();
@FXML
private TextField tfTitle;
private TextField txtUsername;
private Label isConnected;
@FXML
void btnOKClicked(ActionEvent event) {
Stage mainWindow = (Stage) tfTitle.getScene().getWindow();
String title = tfTitle.getText();
String username = txtUsername.getText();
mainWindow.setTitle(title);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
if (loginModel.isDbConnected()){
isConnected.setText("Connected");
} else {
isConnected.setText("Not Connected");
}
}
}
LoginModel.java
import java.sql.*;
public class LoginModel {
Connection connection;
public LoginModel (){
connection = SqliteConnection.Connector();
if (connection == null) {
System.out.println("connection not successful");
System.exit(1);
}
}
public boolean isDbConnected(){
try{
return connection.isClosed();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
SqliteConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class SqliteConnection {
public static Connection Connector() {
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:db:MotorPHEmployee.db");
} catch (ClassNotFoundException e) {
System.err.println("SQLite JDBC driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("Failed to connect to the database.");
e.printStackTrace();
}
return conn;
}
}
I can’t figure out what’s wrong on it? I guess because of the .db file instead of sqlite.
New contributor
seteliakim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1