IntelliJ Database Load

Опубликовано: 12 Март 2025
на канале: Prof. Vanselow
1,227
5

Using Java code to load data from a database table into variables using a ResultSet.

private void loadBikeTable() {
try {
String sql = "SELECT * FROM Bike";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String make = rs.getString("make");
String handlebars = rs.getString("handlebars");
String frame = rs.getString("frame");
String tyres = rs.getString("tyres");
String seatType = rs.getString("seattype");
int numGears = rs.getInt("numgears");
System.out.println(make + "\n"
"This bike has " + handlebars + " handlebars on a "
frame + " frame with " + numGears + " gears."
"\nIt has a " + seatType + " seat with " + tyres + " tyres.");
}
rs.close();
} catch (SQLException se) {
se.printStackTrace();
Alert a = new Alert(Alert.AlertType.ERROR);
a.show();
} catch (Exception e) {
e.printStackTrace();
}
}