使用下面的代码,我可以移动到数据库的最后一条记录和数据库的第一条记录。我创建了next()方法来移动到下一条记录。它从第一个记录移动到第二个记录,但它显示了这个错误(在最后一行之后设置了SQLException结果。我无法继续我的项目,直到我可以移动下一个和之前的记录。任何帮助请求
public void CreateResultSet(){
try{
stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from OAUTOPARTS_PRODUCT";
stmt.executeQuery(sql);
rs = stmt.getResultSet();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
public void first(){
CreateResultSet();
try{
rs.first();
String add1 = rs.getNString("PRODUCT_ID");
txtPartNumber.setText(add1);
String add2 = rs.getNString("PRODUCT_NAME");
txtPartName.setText(add2);
String add3 = rs.getNString("COST");
txtPrice.setText(add3);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
public void next(){
CreateResultSet();
try{
while(rs.next()){
rs.next();
String add1 = rs.getNString("PRODUCT_ID");
txtPartNumber.setText(add1);
String add2 = rs.getNString("PRODUCT_NAME");
txtPartName.setText(add2);
String add3 = rs.getNString("COST");
txtPrice.setText(add3);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
public void last(){
CreateResultSet();
try{
rs.last();
String add1 = rs.getNString("PRODUCT_ID");
txtPartNumber.setText(add1);
String add2 = rs.getNString("PRODUCT_NAME");
txtPartName.setText(add2);
String add3 = rs.getNString("COST");
txtPrice.setText(add3);
}catch(Exception e){
JOptionPane.showMessageDialog(null, 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() {
txtPartNumber = new javax.swing.JTextField();
txtPartName = new javax.swing.JTextField();
txtPrice = new javax.swing.JTextField();
btnFirst = new javax.swing.JButton();
btnLast = new javax.swing.JButton();
btnNext = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnFirst.setText("First");
btnFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFirstActionPerformed(evt);
}
});
btnLast.setText("Last");
btnLast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLastActionPerformed(evt);
}
});
btnNext.setText("Next");
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(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()
.addGap(144, 144, 144)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnFirst, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtPartNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtPartName, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnLast, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(89, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(txtPartNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtPartName, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnFirst, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnLast, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {
last();
}
private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
first();
}
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
next();
}
答案 0 :(得分:0)
试试这个:
private void updateForm() {
String add1 = rs.getNString("PRODUCT_ID");
txtPartNumber.setText(add1);
String add2 = rs.getNString("PRODUCT_NAME");
txtPartName.setText(add2);
String add3 = rs.getNString("COST");
txtPrice.setText(add3);
}
public void first() {
try {
if (rs.first()) {
updateForm();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void next() {
try {
if (rs.next()) {
updateForm();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public void last() {
try {
if (rs.last()) {
updateForm();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
在您的类的构造函数中移动CreateResultSet()
,这样只会创建一次。
我已经在私有方法中考虑了表单更新,该方法会在您的first
,next
和last
方法中调用。
我有这个:CreateResultSet()应该去哪里?
public MainForm() {
initComponents();
connect = DbConnection.dbConnection();
CreateResultSet();
}
public void CreateResultSet(){
try{
stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "select * from OAUTOPARTS_PRODUCT";
stmt.executeQuery(sql);
rs = stmt.getResultSet();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}