简体   繁体   English

无论如何,是否要使用来自2个不同表的2个不同外键将数据插入表中?

[英]Is there anyway to insert data to table with 2 different foreign keys from 2 different tables?

I have trouble when inserting data from jsp to database. 将数据从jsp插入数据库时​​遇到麻烦。 I have 3 tables: 我有3张桌子:

Books(bookid), Users(id), Review(b_id,u_id) , foreign key b_id, u_id references to 2 table above. Books(bookid),Users(id),Review(b_id,u_id),外键b_id,u_id引用了上面的2个表。 Here is my code so far: 到目前为止,这是我的代码:

java java的

public void insert(ReviewModel model) {
    try {
        String b_id = null;
        String u_id = null;
        String sql = "insert into review (content,datePost,rating,b_id,u_id)\n"
                + "values (?,?,?,(select BookID from Books where BookID = '" + b_id + "'),(select id from Users where id  = '" + u_id + "') )";
        PreparedStatement statement = connection.prepareCall(sql);
        statement.setString(1, model.getContent());
        statement.setDate(2, (Date) (model.getDatePost()));
        statement.setFloat(3, model.getRating());
        statement.setInt(4, model.getBookid());
        statement.setInt(5, model.getUserid());
        statement.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(ReviewDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The problem lies here: 问题出在这里:

"...where BookID = '" + b_id + "')..."

statement.setInt(4, model.getBookid());
statement.setInt(5, model.getUserid());

The number of set parameters and the question marks must match. 设置参数的数量和问号必须匹配。 You try to bind it as a named parameter, however in your sql you just concat a null string to it. 您尝试将其绑定为命名参数,但是在sql中,您仅将空字符串连接到该参数。 So either remove the parameter setter lines above and fill up the string variables like 因此,要么删除上面的参数设置器行,然后填充字符串变量,例如

String b_id=mode.getBookId()

or use placeholder of '?' 或使用占位符“?” : ... where BookID = ? ... ... where BookID = ? ... ... where BookID = ? ...

(use the latter one, the first one is vulnerable to sql injection) (使用后一个,第一个容易受到sql注入的攻击)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Derby:引用来自不同表的多个外键 - Derby: Referencing Multiple Foreign Keys From Different Tables JPA如何从单个表的复合主键的两个不同表部分制作复合外键 - JPA how to make composite Foreign Keys from two different table part of composite Primary Key of a single table 如何将数据插入带有外键的表中? - How do I insert data into a table with foreign keys? 制作一种在不同表中插入数据的方法 - Making a method to insert data in different tables 如何同时(从不同线程)将数据插入一个Android SQLite数据库的不同表中? - How can I simultaneously (from different threads) insert data into different tables in one Android SQLite database? 外键中有一个公共列的不同表连接的表 - Table joined with different tables with one common column in the foreign key 在JPA 2.0中从没有外键的多个表中获取数据 - Getting data from multiple tables without foreign keys in JPA 2.0 使用休眠模式从具有两个外键的表中检索数据 - Retrieve data from a table with two foreign keys using hibernate 无论如何在Java HashMap中有两个相同的键,但是值不同? - Anyway to have two of the same keys, but different values in Java HashMap? hibernate使用两个外键用于不同的coloumns? - hibernate use two foreign keys for different coloumns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM