0

Netbeans でデータベースを作成する方法については、次のチュートリアルに従って います

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try {
        String host = "jdbc:derby://localhost:1527/Employees";
        String uName = "bjorn";
        String uPass= "";
        conN = DriverManager.getConnection( host, uName, uPass );
        stmtN = conN.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        String SQL = "SELECT * FROM App.Workers";
        rsN = stmtN.executeQuery( SQL );

        if ( rsN.next( ) ) {
            int id_col = rsN.getInt("ID");
            String id = Integer.toString(id_col);
            String first_name = rsN.getString("First_Name");
            String last_name = rsN.getString("Last_Name");
            String job = rsN.getString("Job_Title");

            textID.setText(id);
            textFirstName.setText(first_name);
            textLastName.setText(last_name);
            textJobTitle.setText(job);
        }
        else {
            rsN.previous( );
            JOptionPane.showMessageDialog(Workers.this, "End of File");
        }
    }
    catch (SQLException err) {
        JOptionPane.showMessageDialog(Workers.this, err.getMessage());
    }        // TODO add your handling code here:
}        

このメソッドは、JPanel でデータベースの次のエントリを表示する必要がありますが、うまくいきません。誰かが私が間違っていることを知っていますか?

4

2 に答える 2

0

This を Main とStatic Resultset rsN;それに続くデータベース Connectionに宣言する

try 
  {
    String host = "jdbc:derby://localhost:1527/Employees";
    String uName = "bjorn";
    String uPass= "";
    conN = DriverManager.getConnection( host, uName, uPass );
    stmtN = conN.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,  ResultSet.CONCUR_UPDATABLE);
    String SQL = "SELECT * FROM App.Workers";
   Static Resultset rsN = stmtN.executeQuery( SQL );
 }
catch (SQLException err) {
    JOptionPane.showMessageDialog(Workers.this, err.getMessage());
}        // TODO add your handling code here:

実行されたボタンアクションでこれを使用できます..

  private void btnNextActionPerformed(java.awt.event.ActionEvent evt)               {                                        

    try
    {
    if ( rsN.next( ) ) {
        int id_col = rsN.getInt("ID");
        String id = Integer.toString(id_col);
        String first_name = rsN.getString("First_Name");
        String last_name = rsN.getString("Last_Name");
        String job = rsN.getString("Job_Title");

        textID.setText(id);
        textFirstName.setText(first_name);
        textLastName.setText(last_name);
        textJobTitle.setText(job);
    }
    else {
        rsN.previous( );
        JOptionPane.showMessageDialog(Workers.this, "End of File");
    }
}
catch (SQLException err) {
    JOptionPane.showMessageDialog(Workers.this, err.getMessage());
}        // TODO add your handling code here:
}        

これを試してみてください..

于 2013-04-24T05:49:40.280 に答える
0
      PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM  App.Workers",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

        ResultSet rs = pstmt.executeQuery();

    if(rs.next())
       {
      int id_col = rsN.getInt("ID");
        String id = Integer.toString(id_col);
        String first_name = rsN.getString("First_Name");
        String last_name = rsN.getString("Last_Name");
        String job = rsN.getString("Job_Title");

        textID.setText(id);
        textFirstName.setText(first_name);
        textLastName.setText(last_name);
        textJobTitle.setText(job);  

 }
   else
 {
   rs.beforeFirst();
   JOptionPane.showMessageDialog(Workers.this, "End of File");
  }

これを試して..

于 2013-04-23T05:30:05.290 に答える