-1

JDBCドライバーをデータベースに接続できます。ブレークポイントは、接続IDがあり、フィールドが適切に入力されていることを示していますが、selectステートメントの実行後、データがデータベースにあり、SQL呼び出しがワークベンチで適切に機能していても、行は返されません。データなしでフィールド名のみを返します。

行が返されないのはなぜですか?

コード:

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);
    }
}

ResultSetにアクセスするコード:

 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)
   {

   }
}
4

2 に答える 2

2

JoshDMの答えの拡張として...

接続が完了するまで接続を開いたままにしておく必要がありますが、接続が適切に閉じられていることを確認するためにあらゆる試みを行う必要があります...

public void searchFired(ActionEvent event) throws ClassNotFoundException {
    // This needs to be declared out side the try/catch so we can
    // reference it later...
    DBConnect db = new DBConnect();
    try {
        // Open the connection
        db.open_Con();
        // Get the data
        ResultSet rs = db.get_data();

        // Process the data
        while (rs.next()) {
            //...Trimmed for space ;)
        }

        resultsGrid.setItems(search_table);
    } catch (SQLException ex) {
        // It's never a good idea to "consume" exceptions,
        // if you're not going to re-throw it, you should it at least
        // log it
        ex.printStackTrace();
    } finally {
        // Make every attempt to close the connection now we're finished with it...
        try {
            db.close_Con();
        } catch (Exception e) {
        }
    }

}
于 2013-01-28T23:10:50.657 に答える
1

get_data()投稿する必要があるメソッドを呼び出すコードが表示されない場合は、ResultSetを閉じる前にデータを抽出する必要があると思われますconnection

これを適切に機能させる方法の例を次に示します。

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzaha%2Fprepex.htm

編集:新しく投稿されたコードに基づく:

    DBConnect db = new DBConnect();
    db.open_Con();
    ResultSet rs = db.get_data();
    db.close_Con();

行を取得した直後に接続を閉じます。これにより、ResultSetが閉じられ、データがフラッシュされます。

データを繰り返し処理してから、を呼び出しますdb.close_Con()

あなたが本当に欲しいのはこれに対する見方です:

CustomDataContainer data = new CustomDataContainer();
Connection conn = null;
PreparedStatement prep = null;
ResultSet rs = null;

try {

    conn = getConnection(); // method returns a connection to your DB
    prep = conn.prepareStatement(STRING_REPRESENTING_YOUR_STATEMENT);
    rs = prep.executeQuery();

    while (rs.next()) {

       data.addData(rs.getString(1));
    }
} catch (Exception ex) {
    ex.printStackTrace();
    // re-throw ex
} finally {

    try { rs.close(); } catch (Exception ignore) {}
    try { prep.close(); } catch (Exception ignore) {}
    try { conn.close(); } catch (Exception ignore) {}
}

return data;
于 2013-01-28T22:38:37.397 に答える