I was following this tutorial for JavaFX and Gradle:
JavaFX with Gradle, Eclipse and, Scene Builder on OpenJDK11
After completion of the tutorial you will have a simple GUI which you input a lower and upper bound and generate random numbers between them. The generated numbers and their bounds are then saved and listed in another view.
After completion of this tutorial I wanted to integrate MySQL as the back end.
The tutorial creator acknowledged at some point that a database like SQLite would be the preferred back end but that it was outside the scope of the tutorial. Luckily it seems he designed the app in such a way that he uses DAO class and I just replaced his code in there with my own JDBC implementation.
package RandomNumber.repository;
import java.util.*;
import java.sql.*;
import RandomNumber.models.RandomNumber;
public class LocalRandomNumberDAO implements RandomNumberDAO {
public LocalRandomNumberDAO() {
}
private List<RandomNumber> numbers = new ArrayList<>();
private Connection openConn() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/numbers?" +
"user=demo_java&password=1234");
}
@Override
public boolean save(RandomNumber number) {
boolean res = false;
try (
var conn = openConn();
var stmt = conn.prepareStatement("insert into numbers_tabl values(?,?,?,?,?)");
) {
stmt.setInt(1, 0);
stmt.setInt(2, number.getNumber());
stmt.setInt(3, number.getLowerBounds());
stmt.setInt(4, number.getUpperBounds());
stmt.setDate(5, java.sql.Date.valueOf(number.getCreatedAt()));
res = stmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
return res;
}
@Override
public void loadNumbers() {
try (
var conn = openConn();
var stmt = conn.createStatement();
) {
String strQuery = "select * from numbers_tabl;";
var rset = stmt.executeQuery(strQuery);
numbers.clear();
while(rset.next()) {
numbers.add(new RandomNumber(
rset.getDate("created").toLocalDate(),
rset.getInt("number"),
rset.getInt("min"),
rset.getInt("max")
)
);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public List<RandomNumber> getNumbers() {
return Collections.unmodifiableList(numbers);
}
}
I am also calling loadNumbers each time the generate button is pressed, so I essentially insert 1 row, and then query all rows in the database.
Questions: 1. Must I always provide a value for the Primary Key to the PreparedStatement in the save method? my table description follows:
mysql> describe numbers_tabl;
+---------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| number | int | NO | | NULL | |
| min | int | NO | | NULL | |
| max | int | NO | | NULL | |
| created | date | YES | | NULL | |
+---------+------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
2. What best practices am I blatantly ignoring?
3. Is there a better way? Or rather, is there a library or framework that is more worth my time?
4. This is my first time using Java-11. I have always used pretty much only Java 7 or 8 features. Is my usage of the var keyword appropriate?