我正在制作一个图书馆系统,用户可以在其中过滤数据库中的数据(使用SQLite)。到目前为止,我在更新数据库时遇到问题。当我选择数据库中的一项然后进行更新时,所有数据都会更改。如何仅更改所选项目?
这是我的代码:
package librarySystemPackage;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;
public class LibrarySystem extends javax.swing.JFrame {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
public LibrarySystem() {
initComponents();
conn = DbConnection.ConnecrDb();
updateTable();
}
private void updateTable() {
String sql = "select * from LibrarySystemDatabase";
try {
pst = conn.prepareStatement (sql);
rs = pst.executeQuery();
tblTable.setModel(DbUtils.resultSetToTableModel (rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
private void clearFields() {
txtNo.setText(null);
txtTitle.setText(null);
txtAuthor.setText(null);
txtGenre.setText(null);
txtLexile.setText(null);
txtPoints.setText(null);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtTitle = new javax.swing.JTextField();
txtNo = new javax.swing.JTextField();
txtAuthor = new javax.swing.JTextField();
txtGenre = new javax.swing.JTextField();
txtLexile = new javax.swing.JTextField();
txtPoints = new javax.swing.JTextField();
btnInsert = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tblTable = new javax.swing.JTable();
btnDeselect = new javax.swing.JButton();
btnUpdate = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
jLabel1.setText("Title:");
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
jLabel2.setText("Accession Number:");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
jLabel3.setText("Author:");
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
jLabel4.setText("Lexile:");
jLabel5.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
jLabel5.setText("Genre:");
jLabel6.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
jLabel6.setText("Points:");
btnInsert.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
btnInsert.setText("Insert");
btnInsert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnInsertActionPerformed(evt);
}
});
tblTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tblTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tblTableMouseClicked(evt);
}
});
tblTable.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
tblTableKeyReleased(evt);
}
});
jScrollPane1.setViewportView(tblTable);
btnDeselect.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
btnDeselect.setText("Deselect");
btnDeselect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeselectActionPerformed(evt);
}
});
btnUpdate.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtPoints, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtLexile, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtGenre, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtNo, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 548, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(btnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnDeselect)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUpdate)
.addGap(0, 0, Short.MAX_VALUE)))
.addGap(28, 28, 28))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(122, 122, 122)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtNo, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtGenre, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtLexile, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtPoints, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnDeselect)
.addComponent(btnInsert)
.addComponent(btnUpdate))
.addContainerGap(224, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {
String sql = "INSERT INTO LibrarySystemDatabase(txtNo, txtTitle, txtAuthor, txtGenre, txtLexile, txtPoints) VALUES(?,?,?,?,?,?)";
try {
pst = conn.prepareStatement (sql);
pst.setString(1, txtNo.getText());
pst.setString(2, txtTitle.getText());
pst.setString(3, txtAuthor.getText());
pst.setString(4, txtGenre.getText());
pst.setString(5, txtLexile.getText());
pst.setString(6, txtPoints.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Inserted Successfully");
updateTable();
clearFields();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
private void tblTableMouseClicked(java.awt.event.MouseEvent evt) {
int row = tblTable.getSelectedRow();
String selection = tblTable.getModel().getValueAt(row, 0).toString();
String sql = "select * from LibrarySystemDatabase where txtNo = " + selection;
try {
pst = conn.prepareStatement (sql);
rs = pst.executeQuery();
if(rs.next()) {
txtNo.setText(rs.getString("txtNo"));
txtTitle.setText(rs.getString("txtTitle"));
txtAuthor.setText(rs.getString("txtAuthor"));
txtGenre.setText(rs.getString("txtGenre"));
txtLexile.setText(rs.getString("txtLexile"));
txtPoints.setText(rs.getString("txtPoints"));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
private void tblTableKeyReleased(java.awt.event.KeyEvent evt) {
int row = tblTable.getSelectedRow();
String selection = tblTable.getModel().getValueAt(row, 0).toString();
String sql = "select * from LibrarySystemDatabase where txtNo = " + selection;
try {
pst = conn.prepareStatement (sql);
rs = pst.executeQuery();
if(rs.next()) {
txtNo.setText(rs.getString("txtNo"));
txtTitle.setText(rs.getString("txtTitle"));
txtAuthor.setText(rs.getString("txtAuthor"));
txtGenre.setText(rs.getString("txtGenre"));
txtLexile.setText(rs.getString("txtLexile"));
txtPoints.setText(rs.getString("txtPoints"));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
private void btnDeselectActionPerformed(java.awt.event.ActionEvent evt) {
clearFields();
updateTable();
}
这是我的更新按钮代码
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String sql = "update LibrarySystemDatabase set"
+ " txtNo = '"+txtNo.getText()+"',"
+ " txtTitle = '"+txtTitle.getText()+"',"
+ " txtAuthor = '"+txtAuthor.getText()+"',"
+ " txtGenre = '"+txtGenre.getText()+"',"
+ " txtLexile = '"+txtLexile.getText()+"',"
+ " txtPoints = '"+txtPoints.getText()+"'";
try {
pst = conn.prepareStatement (sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Record Updated");
updateTable();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} 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 LibrarySystem().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnDeselect;
private javax.swing.JButton btnInsert;
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.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblTable;
private javax.swing.JTextField txtAuthor;
private javax.swing.JTextField txtGenre;
private javax.swing.JTextField txtLexile;
private javax.swing.JTextField txtNo;
private javax.swing.JTextField txtPoints;
private javax.swing.JTextField txtTitle;
// End of variables declaration
}
谢谢
答案 0 :(得分:1)
您的.btn-group a {
border: 1px solid white;
color: white;
cursor: pointer;
padding: 1em, 2em;
width: 20%;
display: block;
font: medium bold;
font-family: arial, sans-serif;
margin-bottom: 1em;
}
.btn-group1 a {
background-color: red;
}
.btn-group2 a {
background-color: green;
}
.btn-group a:not(:last-child) {
border-bottom: none;
}
.btn-group a:hover {
background-color: #3e8e41;
}
<div class="btn-container">
<div class="btn-group btn-group1">
<a href="#">Introduction</a>
<a href="#">Career Development - IT Support</a>
<a href="#">Career Development - Testing</a>
</div>
<div class="btn-group btn-group2">
<a href="#">Lesson 1 - Methodologies and Development Lifecycle</a>
<a href="#">Lesson Breakdown</a>
<a href="#">Methodologies</a>
<a href="#">Waterfall SDLC</a>
<a href="#">Agile SDLC</a>
<a href="#">Scrum vs Kanban</a>
<a href="#">Ceremonies</a>
</div>
</div>
SQL语句没有update LibrarySystemDatabase
子句,因此每一行都会更新为相同的值。
您的代码应为:
WHERE
所做的更改:
根据private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String sql = "UPDATE LibrarySystemDatabase"
+ " SET txtTitle = ?"
+ ", txtAuthor = ?"
+ ", txtGenre = ?"
+ ", txtLexile = ?"
+ ", txtPoints = ?"
+ " WHERE txtNo = ?";
try (PreparedStatement pst = conn.prepareStatement(sql)) {
pst.setString(1, txtTitle.getText());
pst.setString(2, txtAuthor.getText());
pst.setString(3, txtGenre.getText());
pst.setString(4, txtLexile.getText());
pst.setString(5, txtPoints.getText());
pst.setString(6, txtNo.getText());
int count = pst.executeUpdate();
JOptionPane.showMessageDialog(null, count + " Records Updated");
updateTable();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
语句,在WHERE
列上使用txtNo
子句,因为这似乎是主键。
使用try-with-resources可以更简单地处理SELECT
。
致电close()
获取更新计数并显示更新计数。
使用executeUpdate()
参数标记来防止SQL注入攻击和SQL语法错误,就像您对?
语句所做的一样。
答案 1 :(得分:0)
也许在您要更新的行中:
select * from LibrarySystemDatabase
这将调用库系统数据库中的所有内容,也许会指定您要在此方法中更新的列。在您使用的所有其他方法中,您似乎都在指定要存储数据的位置