使用JDBC ResultSet更新行时出现问题

时间:2014-10-02 01:19:43

标签: java sql swing jdbc

我正在尝试使用从swing GUI获取的信息更新行。 这是我的例外:

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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我怀疑在调用ResultSet#updateRow时,您在ResultSet中定位当前行的问题。对于名为[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