0

我目前的开放结果集存在问题,无法按照我认为的方式工作。当前唯一可用的函数是 next() 方法,其他都不起作用。如果项目处于调试模式,您可以通过按钮上的 actionperformed 事件,它会触发 previous() 方法并跳过该方法中的剩余代码。如果有人能指出我正确的方向,那将不胜感激。

表格代码:

import java.sql.*;
import javax.swing.*;

public class DataNavigator extends javax.swing.JFrame {

public DataInterface db = null;
public Statement s = null;
public Connection con = null;
public PreparedStatement stmt = null;
public ResultSet rs = null;

/** Creates new form DataNavigator */
public DataNavigator() {
    initComponents();

    try {
        db = new DataInterface("jdbc:odbc:CMPS422");
        con = db.getConnection();
        stmt = con.prepareStatement("SELECT * FROM Products");
        rs = stmt.executeQuery();
        rs.last();
    } catch (Exception e) {
    }
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    btnFirst = new javax.swing.JButton();
    btnNext = new javax.swing.JButton();
    btnLast = new javax.swing.JButton();
    btnUpdate = new javax.swing.JButton();
    btnInsert = new javax.swing.JButton();
    btnDelete = new javax.swing.JButton();
    txtPartNum = new javax.swing.JTextField();
    txtDesc = new javax.swing.JTextField();
    txtQty = new javax.swing.JTextField();
    txtPrice = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    btnPrev = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Assignment 3 Data Navigator");
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowOpened(java.awt.event.WindowEvent evt) {
            formWindowOpened(evt);
        }
    });

    btnFirst.setText("First");
    btnFirst.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnFirstActionPerformed(evt);
        }
    });

    btnNext.setText("Next");
    btnNext.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnNextActionPerformed(evt);
        }
    });

    btnLast.setText("Last");
    btnLast.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnLastActionPerformed(evt);
        }
    });

    btnUpdate.setText("Update");

    btnInsert.setText("Insert");

    btnDelete.setText("Delete");

    jLabel1.setText("Part Number:");

    jLabel2.setText("Description:");

    jLabel3.setText("Quantity:");

    jLabel4.setText("Price:");

    btnPrev.setText("Prev");
    btnPrev.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            btnPrevMouseClicked(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(btnFirst)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(2, 2, 2)
                    .addComponent(btnPrev)
                    .addGap(4, 4, 4)
                    .addComponent(btnNext)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnLast))
                .addComponent(jLabel1)
                .addComponent(jLabel2)
                .addComponent(jLabel3)
                .addComponent(jLabel4))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(txtPartNum)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(btnUpdate)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnInsert)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(btnDelete))
                .addComponent(txtDesc)
                .addComponent(txtQty)
                .addComponent(txtPrice))
            .addContainerGap(71, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnFirst)
                .addComponent(btnNext)
                .addComponent(btnLast)
                .addComponent(btnUpdate)
                .addComponent(btnInsert)
                .addComponent(btnDelete)
                .addComponent(btnPrev))
            .addGap(66, 66, 66)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLabel1)
                .addComponent(txtPartNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(txtDesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(txtQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4))
            .addContainerGap(102, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  

    try {
        this.txtPartNum.setText(rs.getString("Partnum"));
        this.txtDesc.setText(rs.getString("Description"));
        this.txtPrice.setText(rs.getString("Price"));
        this.txtQty.setText(rs.getString("Quantity"));

    } catch (SQLException e) {
    }
}                                 

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:

    try {
        System.out.println(rs.getCursorName());
        rs.next();
        rs.moveToCurrentRow();
        System.out.println(rs.getCursorName());
        this.txtPartNum.setText(rs.getString("Partnum"));
        this.txtDesc.setText(rs.getString("Description"));
        this.txtPrice.setText(rs.getString("Price"));
        this.txtQty.setText(rs.getString("Quantity"));
        System.out.println(rs.getRow());
    } catch (Exception e) {
    }

}                                       

private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    try {
        rs.last();
        this.txtPartNum.setText(rs.getString("Partnum"));
        this.txtDesc.setText(rs.getString("Description"));
        this.txtPrice.setText(rs.getString("Price"));
        this.txtQty.setText(rs.getString("Quantity"));

    } catch (Exception e) {
    }
}                                       

private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    try {
        rs.first();
        this.txtPartNum.setText(rs.getString("Partnum"));
        this.txtDesc.setText(rs.getString("Description"));
        this.txtPrice.setText(rs.getString("Price"));
        this.txtQty.setText(rs.getString("Quantity"));

    } catch (Exception e) {
    }
}                                        

private void btnPrevMouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
            try {
        int i;
        i = rs.getRow();
        if (i > 0) {
            rs.previous();
            System.out.println(rs.getRow());
            this.txtPartNum.setText(rs.getString("Partnum"));
            this.txtDesc.setText(rs.getString("Description"));
            this.txtPrice.setText(rs.getString("Price"));
            this.txtQty.setText(rs.getString("Quantity"));
        } else {
            System.out.println("FALSE");
        }
    } catch (Exception e) {
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new DataNavigator().setVisible(true);

        }
    });
}
// Variables declaration - do not modify
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnFirst;
private javax.swing.JButton btnInsert;
private javax.swing.JButton btnLast;
private javax.swing.JButton btnNext;
private javax.swing.JButton btnPrev;
private javax.swing.JButton btnUpdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField txtDesc;
private javax.swing.JTextField txtPartNum;
private javax.swing.JTextField txtPrice;
private javax.swing.JTextField txtQty;
// End of variables declaration

}

类对象代码:

import java.sql.*;
import javax.swing.JOptionPane;

public class DataInterface {

private static DataInterface dbint = null;
private static Connection conn = null;   // connection object.
private static ResultSet rset = null;

public DataInterface(String ODBCDSN) {
    try
    {
        //  See if the driver is present.
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        //  Open connection to database.
        conn = DriverManager.getConnection(ODBCDSN);
        JOptionPane.showMessageDialog(null,
                                      "Database successfully opened");
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, e.toString());
    }
}

public Connection getConnection() { return conn; }
public static DataInterface getInstance() { return dbint; }
public static ResultSet getResultSet() { return rset; }
public PreparedStatement setStatement(String a) throws SQLException{
PreparedStatement stmt = conn.prepareStatement(a);
    return stmt;
}
4

1 回答 1

1

在声明中,您必须将ResultSetType 设置为TYPE_SCROLL_INSENSITIVE。看教程

于 2010-04-07T15:32:28.730 回答