我正在创建一个带有下一个,上一个,第一个,最后一个,更新按钮的表单,但是ResultSet不允许我回滚浏览数据库记录,即使我将其设置为对滚动不敏感。它给我错误消息,SQlite仅支持TYPE_FORWARD RESULTSET。我在哪里错了,该怎么办?
package DatabaseOperations;
import java.sql.*;
import javax.swing.JOptionPane;
/**
*
* @author Kelvin Mike, Programmer
*/
public class DatabaseOperations extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
/**
* Creates new form DatabaseOperations
*/
public DatabaseOperations() {
initComponents();
DoConnect();
}
public void DoConnect(){
try {
//connect to the database
String host = "jdbc:sqlite:libraryass.db";
con = DriverManager.getConnection(host);
//execute some sql and load the records into the resultsheet
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String SQL = "SELECT * FROM books_stored";
rs = stmt.executeQuery(SQL);
//move the cursor to the first record and get the data
rs.next();
int Serial_no = rs.getInt("Serial_no");
String Serialno = Integer.toString(Serial_no);
String Bookname = rs.getString("Book_name");
String Categ = rs.getString("Category");
String Shelf = rs.getString("Shelf/Section");
int Copies = rs.getInt("Copies");
String Copy = Integer.toString(Copies);
//Dispaly the first record in the text fields
textserial.setText(Serialno);
textbkname.setText(Bookname);
textcopies.setText(Copy);
textcategory.setText(Categ);
textshelf.setText(Shelf);
} catch (SQLException err) {
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
textserial = new javax.swing.JTextField();
textbkname = new javax.swing.JTextField();
textcopies = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
textcategory = new javax.swing.JTextField();
textshelf = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
btnNext = new javax.swing.JButton();
btnPrevious = new javax.swing.JButton();
btnLast = new javax.swing.JButton();
btnFirst = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
btnUpdate = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
btnNew = new javax.swing.JButton();
btnSave = new javax.swing.JButton();
btnCancelNew = new javax.swing.JButton();
jPanel4 = new javax.swing.JPanel();
textSearch = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textserial.setToolTipText("Serial number");
textserial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textserialActionPerformed(evt);
}
});
textbkname.setToolTipText("Book name");
textcopies.setToolTipText("Number of copies");
jLabel1.setText("Category");
textcategory.setToolTipText("Category");
textshelf.setToolTipText("section/shelf");
jLabel2.setText("Section");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
.addComponent(textserial))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(textbkname, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textcategory)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(textcopies, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textshelf, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(24, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textserial, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textbkname, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textcopies, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(42, 42, 42)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textcategory, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textshelf, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
btnNext.setText("Next");
btnNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnNextActionPerformed(evt);
}
});
btnPrevious.setText("Previous");
btnPrevious.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnPreviousActionPerformed(evt);
}
});
btnLast.setText("Last");
btnLast.setToolTipText("");
btnLast.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLastActionPerformed(evt);
}
});
btnFirst.setText("First");
btnFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFirstActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnNext, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnPrevious, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(btnFirst, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnLast, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnNext)
.addComponent(btnPrevious)
.addComponent(btnLast)
.addComponent(btnFirst))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
btnDelete.setText("Delete");
btnNew.setText("New Record");
btnSave.setText("Save New Record");
btnCancelNew.setText("Cancel New Record");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCancelNew, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(42, 42, 42)
.addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnNew, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnUpdate)
.addComponent(btnDelete)
.addComponent(btnNew))
.addGap(18, 18, 18)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSave)
.addComponent(btnCancelNew))
.addContainerGap(18, Short.MAX_VALUE))
);
textSearch.setText("Search Records");
textSearch.setToolTipText("Search Records");
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addComponent(textSearch)
.addContainerGap())
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.addComponent(textSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(16, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void textserialActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
if (rs.next()) {
int Serial_no = rs.getInt("Serial_no");
String Serialno = Integer.toString(Serial_no);
String Bookname = rs.getString("Book_name");
String Categ = rs.getString("Category");
String Shelf = rs.getString("Shelf/Section");
int Copies = rs.getInt("Copies");
String Copy = Integer.toString(Copies);
//Dispaly the first record in the text fields
textserial.setText(Serialno);
textbkname.setText(Bookname);
textcopies.setText(Copy);
textcategory.setText(Categ);
textshelf.setText(Shelf);
}
else{
rs.previous();
JOptionPane.showMessageDialog(DatabaseOperations.this, "End of File");
}
} catch (SQLException err) {
JOptionPane.showMessageDialog(DatabaseOperations.this, err.getMessage());
}
}
private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
if (rs.previous()) {
int Serial_no = rs.getInt("Serial_no");
String Serialno = Integer.toString(Serial_no);
String Bookname = rs.getString("Book_name");
String Categ = rs.getString("Category");
String Shelf = rs.getString("Shelf/Section");
int Copies = rs.getInt("Copies");
String Copy = Integer.toString(Copies);
//Dispaly the first record in the text fields
textserial.setText(Serialno);
textbkname.setText(Bookname);
textcopies.setText(Copy);
textcategory.setText(Categ);
textshelf.setText(Shelf);
}
else{
rs.next();
JOptionPane.showMessageDialog(DatabaseOperations.this, "Start of File");
}
} catch (SQLException err) {
JOptionPane.showMessageDialog(DatabaseOperations.this,err.getMessage());
}
}
private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
rs.first();
int Serial_no = rs.getInt("Serial_no");
String Serialno = Integer.toString(Serial_no);
String Bookname = rs.getString("Book_name");
String Categ = rs.getString("Category");
String Shelf = rs.getString("Shelf/Section");
int Copies = rs.getInt("Copies");
String Copy = Integer.toString(Copies);
//Dispaly the first record in the text fields
textserial.setText(Serialno);
textbkname.setText(Bookname);
textcopies.setText(Copy);
textcategory.setText(Categ);
textshelf.setText(Shelf);
} catch (SQLException err) {
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
rs.last();
int Serial_no = rs.getInt("Serial_no");
String Serialno = Integer.toString(Serial_no);
String Bookname = rs.getString("Book_name");
String Categ = rs.getString("Category");
String Shelf = rs.getString("Shelf/Section");
int Copies = rs.getInt("Copies");
String Copy = Integer.toString(Copies);
//Dispaly the first record in the text fields
textserial.setText(Serialno);
textbkname.setText(Bookname);
textcopies.setText(Copy);
textcategory.setText(Categ);
textshelf.setText(Shelf);
} catch (SQLException err) {
JOptionPane.showMessageDialog(this, err.getMessage());
}
}
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String Serial = textserial.getText();
String Book = textbkname.getText();
String Copies = textcopies.getText();
String Category = textcategory.getText();
String nShelf = textshelf.getText();
int newSerial = Integer.parseInt(Serial);
try {
rs.updateInt("Serial", newSerial);
rs.updateString("Bookname", Book);
rs.updateString("Copy", Copies);
rs.updateString("Categ", Category);
rs.updateString("Shelf", nShelf);
} catch (SQLException err) {
System.out.println(err.getClass().getName() + ":" + err.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DatabaseOperations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DatabaseOperations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DatabaseOperations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DatabaseOperations.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DatabaseOperations().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCancelNew;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnFirst;
private javax.swing.JButton btnLast;
private javax.swing.JButton btnNew;
private javax.swing.JButton btnNext;
private javax.swing.JButton btnPrevious;
private javax.swing.JButton btnSave;
private javax.swing.JButton btnUpdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JTextField textSearch;
private javax.swing.JTextField textbkname;
private javax.swing.JTextField textcategory;
private javax.swing.JTextField textcopies;
private javax.swing.JTextField textserial;
private javax.swing.JTextField textshelf;
// End of variables declaration
}