using ResultSet.Previous method not working in Java using .mdb file OBDC
- by jsonnie
Hello,
I'm currently having an issue with my open result set not working how I believe it should. The only function that is currently working is the next() method, nothing else will work. If the project is placed into a debug mode you can follow through actionperformed event on the button it hits the previous() method and jumps over the remaining code in the method. If someone could point me in the right direction it would be truly appreciated.
FORM CODE:
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
}
CLASS OBJECT CODE:
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;
}