- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个图书馆系统,用户可以在其中过滤数据库中的数据(使用 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
}
谢谢
最佳答案
您的update LibrarySystemDatabase
SQL语句没有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
上的条款列,因为根据您的 SELECT
这似乎是主键声明。
使用try-with-resources为了更简单地处理close()
.
调用 executeUpdate()
获取更新计数并显示它。
使用?
参数标记,以防止 SQL 注入(inject)攻击和 SQL 语法错误,就像您已经对 INSERT
所做的那样声明。
关于java - 为什么当我点击更新按钮时,数据库中的所有数据都改变了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60359430/
我有以下MWE function f(p) ans = zeros(p, 2) return ans end ans = f(2) ans b=ans.+1.0 ans 起初,ans是正确的,
OWIN AppBuilder“UseStatic”位从本地文件系统传送文件,这在某些情况下很方便,但我希望它从我在应用程序启动时预先填充的内存中 IDictionary 传送内容。任何人都可以指出一
我是 JavaScript 新手。 我的代码允许我列出 JSON 文档的元素及其类型,并将所有元素连接到一个字符串 donnees_types 中。 问题是 JavaScript 中的 typeof
我想在每次刷新时更改主页上的背景图像。我怎样才能做到这一点?我认为 jquery 是可能的,但我不太清楚。 感谢您对此主题的任何帮助或评论。 最佳答案 我不知道“如何”,但我找到了以下链接: http
所以我已经在这上面花了几个小时了,老实说我完全陷入困境。我写了一个 for 循环来计算整数中的数字数量,但我发现一旦我输入 10 位以上的数字,除数值就会发生变化,而且我不明白为什么。我在互联网上搜索
当我在使用表面 View 的游戏 Activity 和使用膨胀菜单的其他 Activity 之间切换时,我会收到错误消息。 日志猫: 07-13 15:15:34.464: ERROR/Android
听说很简单 R*=f; G*=f; B*=f; 其中 f 是标量值 0 .. 1.0 或更大改变亮度的方法不太正确颜色,但我找不到一些代码片段获得更好的东西(无需太多学习色彩理论)也许有人可以在这里给
如以下链接所述:- How to get the ThreadPoolExecutor to increase threads to max before queueing? 我将队列实现更改为在进入
我只显示最初提供 20 分钟 slotMinutes 的日历。我试图让用户即时更改为 10 分钟的 slotMinutes。 我有一个触发以下代码的按钮: $('#calendar').fullCal
我的问题是:我的应用程序中有一个新闻列表(UITableView)当我点击 1 个"new"时,我打开它,里面有一个后退按钮,可以让我回到列表。现在的问题是我必须在滑动时实现"new"更改,所以我制作
我面临着与 I'm trying to install psycopg2 onto Mac OS 10.6.3; it claims it can't find "stdarg.h" but I ca
需要通过为 array2 中不存在的索引设置 visible false 来从 array1 创建一个新的 array3。 在下面的示例中,我有索引 0,2。所以对于 1,3,结果数组必须具有 vis
我有一个对象,类似这样 var Egg = function(){ this.test = $(.slider .label); $('.slider').slider({
我想改变 ScrollView 的宽度。首先,我这样做了: var scrollWidthConstraint: NSLayoutConstraint! 然后设置它: scrollWidthConst
我有两个动画,一个是“过渡”,它在悬停时缩小图像,另一个是 animation2,其中图像的不透明度以周期性间隔重复变化。 我有 animation2 在图像上进行,当我将鼠标悬停在它上面时,anim
我是一个 jQuery 新手,一直在尝试添加一个脚本来更改 div onClick 的 id。 这是一个jsfiddle example . $(function accept() { $("
我正在尝试更改用户输入的字母的大小写,并将该字母的小写和大写版本存储在变量中。我已经编写了下面的代码,但它在运行时遇到了问题。有人指出是什么导致了问题吗? #include #include #i
假设我有这段代码: char num[2] = {15, 240}; char* p_num; 现在,如果我理解的一切正确,数组中的位应该像这样对齐: 00001111 11110000 我的问题是
var html = '' + count + '' + i.Description + '' + i.Priority + '' + i.Status + 'johnsmith- ' + creat
我在虚拟机上安装了 minix3,并希望我可以操纵当前的队列选择算法,以便我可以将其从优先级顺序更改为包括随机分类的低优先级作业的优先级顺序。我发现我需要更改的代码部分在 proc.c 中,具体部分是
我是一名优秀的程序员,十分优秀!