I can connect the JDBC driver connect to the database. Break points show it has a connection id and the fields are properly filled, but after execution of the select statement, no rows are returned even though data is in the database and the SQL call works properly in workbench. It only returns field names without any data.
Why aren’t any rows being returned?
Code:
public class DBConnect {
private static Connection conn;
public static String url = "jdbc:mysql://localhost/efwalter";
public static String user = "root";
public static String pass = "XXXXXXXXX";
private PreparedStatement prep;
public void open_Con() throws ClassNotFoundException {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pass);
} catch (SQLException ex) {
infoBox(ex.toString(), "ERROR");
}
}
public ResultSet get_data(String SQL) {
try {
prep = conn.prepareStatement("SELECT * FROM efwalter.impact_tests");
ResultSet rs = prep.executeQuery();
return rs;
} catch (SQLException ex) {
infoBox(ex.toString(), "ERROR");
return null;
}
}
public void close_Con() {
try {
conn.close();
} catch (SQLException ex) {
infoBox(ex.toString(), "ERROR");
}
}
public void infoBox(String infoMessage, String location) {
JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + location, JOptionPane.INFORMATION_MESSAGE);
}
}
Code where ResultSet is accessed:
public void searchFired(ActionEvent event) throws ClassNotFoundException {
try{
DBConnect db = new DBConnect();
db.open_Con();
ResultSet rs = db.get_data();
db.close_Con();
while (rs.next())
{
study_struct study = new study_struct();
ObservableList<String> row = FXCollections.observableArrayList();
study.setStudy_number(rs.getInt(1));
row.add(rs.getString(1));
study.setCustomer_id(rs.getInt(2));
study.setShop_order(rs.getInt(3));
study.setProduct(rs.getString(4));
study.setGmax_results(rs.getString(5));
study.setGmax_average(rs.getDouble(6));
study.setHic_results(rs.getString(7));
study.setHic_average(rs.getDouble(8));
study.setSensor_data_x(rs.getString(9));
study.setSensor_data_y(rs.getString(10));
study.setDescription(rs.getString(11));
study.setGauge(rs.getString(12));
study.setAppraiser(rs.getString(13));
study.setStudy_name(rs.getString(14));
row.add(rs.getString(14));
study.setTimestamp(rs.getString(15));
row.add(rs.getString(15));
study.setWeight(rs.getString(16));
found_studies.add(study);
search_table.add(row);
}
resultsGrid.setItems(search_table);
}
catch (SQLException ex)
{
}
}
Without seeing the code calling your
get_data()method, which you should post, I will suspect you need to extract your data from yourResultSetbefore you close theconnection.Here is an example of how to work this appropriately:
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzaha%2Fprepex.htm
EDIT: Based on your newly-posted code:
You are closing the connection right after you get the rows, this closes your ResultSet and flushes your data.
Iterate through the data, THEN call
db.close_Con().What you really want is a take on this: