Netbeans中的数据库onclick按钮

时间:2013-04-22 22:46:18

标签: java netbeans

我正在关注在Netbeans中创建数据库的教程: http://www.homeandlearn.co.uk/java/database_scrolling_buttons.html 我的JPanel上有一个Next按钮,它必须调用这个方法:

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上显示数据库的下一个条目,但我无法使其正常工作。有人知道我做错了吗?

2 个答案:

答案 0 :(得分: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");
  }

试试这个..

答案 1 :(得分:0)

Static Resultset rsN;和以下数据库连接

中将其声明为Main
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:
}        

它正在尝试这个..