简体   繁体   中英

Java JDBC PreparedStatement Foreign Key Constraint Failed

I'm designing a billing program with SQLite and JDBC, and I'm trying to use this helper method:

public static void preparedInsert(String query, String[] inserters) {
    Connection c = connect();
    try {
        PreparedStatement statement = c.prepareStatement(query);

        for (int i = 0; i < inserters.length; i++) {
            statement.setObject(i + 1, "\'" + inserters[i] + "\'");
        }
        statement.executeUpdate();
        c.commit();
        JOptionPane.showMessageDialog(null, "Database updated!");

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Error updating database: " + e.getMessage());
    }
    disconnect(c);
}

public static Connection connect() {
    Connection c = null;
    try {
        Class.forName("org.sqlite.JDBC");
        SQLiteConfig config = new SQLiteConfig();  
        config.enforceForeignKeys(true);  
        c = DriverManager.getConnection("jdbc:sqlite:MRWBilling.db", config.toProperties());
        c.setAutoCommit(false);
    } catch ( Exception e ) {
        JOptionPane.showMessageDialog(null, "Error connecting to database: " + e.getMessage());
    }
    return c;
}

public static void disconnect(Connection c) {
    try {
        c.close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Error disconnecting from database: " + e.getMessage());
    }
}

The parameter I'm trying to pass in is this:

SQLiteJDBC.preparedInsert("insert into timesheets(date, attorney, notes) values(?, ?, ?);", 
            new String[]{date, attorneyName, notes});   

Timesheets has four rows: id, date, attorney and notes where id is set to autoincrement and where attorney is a foreign key to the attorney table. The attorneyName I'm passing in actually exists in the attorney's table.

This was working fine during a prior build when I was using regular statements, but now that I've swapped to prepared statements, I'm getting this:

Error updating database: [SQLITE_CONSTRAINT] Abort due to constraint violation (FOREIGN KEY constraint failed)

I'm at a loss as to what I'm doing wrong. Any suggestions?

The additional single quotes that are wrapping the parameters are probably causing the FK violation. Use this instead in your loop:

statement.setString(i+1, inserters[i]);

You may also need to remove the semicolon from the insert statement.

Lee,

Can you try removing semicoln in the end like the one below "insert into timesheets(date, attorney, notes) values(?, ?, ?)"

还要验证attorneyName是否与律师表中的名称完全相同,有时我们忘记区分大小写

Your preparedInsert method seems to be creating the problem. try altering it like below

PreparedStatement statement = c.prepareStatement(query);

    for (int i = 0; i < inserters.length; i++) {
        statement.setString(i + 1, inserters[i]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM