gpt4 book ai didi

java - 使用 JDBC 结果集更新行时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:39 25 4
gpt4 key购买 nike

我正在尝试使用从 swing GUI 获得的信息来更新一行。这是我的异常(exception):

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]Error in row.BookFrame bUpdateActionPerformed

BookDB 类处理数据库交互和连接。

public class BookDB {

private final String data = "jdbc:odbc:Books";
private Connection con;
private Statement stmt;
ResultSet result;

BookDB() throws ClassNotFoundException, SQLException {
connect();
}

public static void main(String[] args) {
try {
BookDB b = new BookDB();
} catch (ClassNotFoundException ex) {
Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void connect() throws ClassNotFoundException, SQLException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

this.con = DriverManager.getConnection(data);
this.stmt = this.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
this.result = this.stmt.executeQuery("SELECT Bookcode,Booktitle,Bookprice FROM Books");
} catch (ClassNotFoundException e) {
System.out.println("Failed to load jdbc/odbc drivers");
System.out.println("Class Erro: " + e);
} catch (SQLException e) {
System.out.println("Unable to connect");
System.out.println(("SQL ERROR: " + e));
}
}

public void updateRecord(Book book) throws SQLException {
result.updateString("BookCode", book.getCode());
result.updateString("BookTitle", book.getTitle());
result.updateDouble("BookPrice", book.getPrice());
result.updateRow();
}
}

BookFrame 处理事件处理和用户输入

public class BookFrame extends javax.swing.JFrame {

private BookDB bd;

public BookFrame() {
try {
initComponents();
this.bd = new BookDB();
bd.connect(); //connect to database
bd.result.next();
tCode.setText(this.bd.result.getString("bookcode") + ""); //sets textfields from data
tTitle.setText(this.bd.result.getString("booktitle") + "");
tPrice.setText(this.bd.result.getString("bookprice") + "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(BookFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(BookFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {
try {
Book b = new Book(tCode.getText(), tTitle.getText(), Double.parseDouble( tPrice.getText())); //create book object of values to update with
bd.updateRecord(b);
} catch (SQLException ex) {
Logger.getLogger(BookFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

最佳答案

我怀疑当您调用 ResultSet#updateRow 时,结果集中当前行的定位存在问题。对于包含单个记录的名为 [Books] 的 Access 表...

BookCode  BookTitle                  BookPrice
-------- ------------------------- ---------
BSFD Brain Surgery for Dummies 4.95

...以下概念验证代码表明您可以更新数据库中的该行,前提是该行是 ResultSet 中的 CurrentRow。

“Book.java”- Book 对象

package com.example.resultsetupdate;

import java.math.BigDecimal;

public class Book {
private String code;
private String title;
private BigDecimal price;

public Book(String code, String title, BigDecimal price) {
super();
this.code = code;
this.title = title;
this.price = price;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}
}

“BookDB.java”-数据库操作

package com.example.resultsetupdate;

import java.sql.*;

public class BookDB {

private final String data =
"jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};" +
"DBQ=C:\\Users\\Public\\mdbTest.mdb;";
private Connection con;
private Statement stmt;
ResultSet result;

BookDB() throws SQLException {
connect();
}

public void connect() throws SQLException {
this.con = DriverManager.getConnection(data);
this.stmt = this.con
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
this.result = this.stmt
.executeQuery("SELECT BookCode, BookTitle, BookPrice FROM Books");
}

public void close() throws SQLException {
this.con.close();
}

public void updateRecord(Book book) throws SQLException {
result.updateString("BookCode", book.getCode());
result.updateString("BookTitle", book.getTitle());
result.updateBigDecimal("BookPrice", book.getPrice());
result.updateRow();
}
}

“Main.java”

package com.example.resultsetupdate;

import java.sql.SQLException;

public class Main {

public static void main(String[] args) {
Book b = new Book(
"WBIA",
"Why Bacon is Awesome",
java.math.BigDecimal.valueOf(1950, 2));
BookDB bd;
try {
bd = new BookDB();
bd.result.next(); // move to first row
bd.updateRecord(b);
bd.close();
} catch (SQLException e) {
e.printStackTrace(System.out);
}
}

}

运行代码后,Access 表包含

BookCode  BookTitle             BookPrice
-------- -------------------- ---------
WBIA Why Bacon is Awesome 19.50

关于java - 使用 JDBC 结果集更新行时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com