im working on a project and this is the last step basically, we used action events to change from scene to scene and stuff like that, my database is connected but only saves pre-defined info like obj.setNumber(1) , and not info that the user enters in a text field, for example obj.setName(txtfld.getText()), it just shows up in the DB as " " (not null).
#can someone help with saving user input to a database?(javafx)
1 messages · Page 1 of 1 (latest)
<@&987246584574140416> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Sure, I can help you with saving user input to a database in JavaFX. Here's a step-by-step guide:
-
First, make sure you have a database set up and connected to your JavaFX application. You can use any database of your choice (e.g., MySQL, SQLite, etc.). You'll need to import the necessary JDBC driver for your chosen database.
-
Create a class for your database connection. This class will handle the connection and provide methods for executing queries. Here's an example:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
private Connection connection;
public DatabaseConnection() {
try {
connection = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
return connection;
}
}
Make sure to replace DB_URL, USERNAME, and PASSWORD with your own database details.
- In your JavaFX controller class, create an instance of the
DatabaseConnectionclass and use it to save user input to the database. Here's an example:
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MyController {
@FXML
private TextField userInputField;
private DatabaseConnection dbConnection;
public MyController() {
dbConnection = new DatabaseConnection();
}
@FXML
private void saveUserInput() {
String userInput = userInputField.getText();
try {
Connection connection = dbConnection.getConnection();
PreparedStatement statement = connection.prepareStatement("INSERT INTO mytable (column_name) VALUES (?)");
statement.setString(1, userInput);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
In this example, userInputField is a TextField in your JavaFX scene where the user enters their input. The saveUserInput() method is an action event handler that gets called when the user clicks a button or performs any other action to save the input.
- Finally, make sure to wire up the
saveUserInput()method to an appropriate event in your JavaFX application. For example, you can add anonActionattribute to a button in your FXML file:
<Button text="Save" onAction="#saveUserInput" />
That's it! Now, whenever the user enters input and clicks the "Save" button (or performs any other action you've defined), their input will be saved to the database.
Note: This is just a basic example to get you started. Depending on your specific requirements and database structure, you may need to modify the code accordingly.
Can someone give a different answer….?
can you provide more info? like code and stuff?
i cant give the full code but basically theres a scene with text fields and a button, the button has an action event like this: BT.setOnAction(e->{player.setName(textfield1.getText(); stage.setScene(scene3); });
And at the end of the code i have
player.setNumber(1);
opened a session and a transaction to save the players info in the database.
the number gets correctly saved since its just a number and not something that i took from the user, but the name just doesn’t show up at all
It doesn’t say NULL on the table retrieval, but the cell is empty
^
I hope it’s enough
without full code no one can really help you
without code all we can do is make an educated guess like you're not reading the values properly/setting them on your entity.
All I can advise is you place some breakpoints in your code, to see if the state's what you expect