java - 如何根据主键的值更改外键?

标签 java mysql

我现在有两张 table , table talkview:

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(10)     | NO   | PRI | NULL    | auto_increment |
| titlename | varchar(64) | NO   |     | NULL    |                |
| postname  | varchar(64) | NO   |     | NULL    |                |
| counts    | varchar(11) | YES  |     | 0       |                |
+-----------+-------------+------+-----+---------+----------------+

和表主要讨论:

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| title    | varchar(64) | NO   |     | NULL    |                |
| text     | varchar(64) | NO   |     | NULL    |                |
| username | varchar(64) | NO   |     | NULL    |                |
| talk_id  | int(10)     | YES  | MUL | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

现在我想将数据一起插入这些表中,“talk_id”将根据表 talkview 中的“id”自动更改。

在Java中,我尝试了这段代码,但'talk_id'没有变化:

public void posttalk(String title, String con, String name){
        connection = DBConection.getConnection();
        Statement stmt = null;
        String SQL_1 = "insert into talkview(titlename,postname) values(? , ?)";
        String SQL_2 = "insert into maintalk(title,text,username) values(? , ? , ?)";
        try {
            connection.setAutoCommit(false);
            PreparedStatement preparedStatement = connection.prepareStatement(SQL_1);
            preparedStatement.setString(1, title);
            preparedStatement.setString(2, name);
            Integer a = preparedStatement.executeUpdate();
            preparedStatement = connection.prepareStatement(SQL_2);
            preparedStatement.setString(1, title);
            preparedStatement.setString(2, name);
            preparedStatement.setString(3, con);
            Integer b = preparedStatement.executeUpdate();
            connection.commit();
            connection.setAutoCommit(true);
            } catch (SQLException sqle) {
            try {
                connection.rollback();
                stmt.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            sqle.printStackTrace();
            } finally {
            DBConection.closeConnection(connection);
        }
    }

最佳答案

在第一个名为 talkview 的表中插入一些数据后。您可以使用最大主键检索插入的最后一条记录,因为您的PK是自动递增的,然后将其放在maintalktalk_id列中 table 。您可以执行以下代码来检索它。

    String SQL_1 = "insert into talkview(titlename,postname) values(? , ?)";
    String SQL_2 = "insert into maintalk(title,text,username,talk_id) values(? , ? , ?, ?)";
    String SQL_3 = "SELECT id FROM talkview order by id DESC LIMIT 1;";
    try {
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement(SQL_1);
        preparedStatement.setString(1, title);
        preparedStatement.setString(2, name);
        Integer a = preparedStatement.executeUpdate();

        // This is the query to retrieve the last ID you insert in talkveiw table
        PreparedStatement preparedStatement = connection.prepareStatement(SQL_3);
        ResultSet result = preparedStatement.executeQuery();
        Integer id = 0; // This will be the id you will add on your talk_id
        if(result.next()) {
            id = result.getInt(1);
        }

        preparedStatement = connection.prepareStatement(SQL_2);
        preparedStatement.setString(1, title);
        preparedStatement.setString(2, name);
        preparedStatement.setString(3, con);
        preparedStatement.setInt(4, id); // you will insert it here the 'id'
        Integer b = preparedStatement.executeUpdate();
        connection.commit();
        connection.setAutoCommit(true);

关于java - 如何根据主键的值更改外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712098/

相关文章:

Java 格式化程序不写入文件

php - 使用 x-editable 将 URL 文本插入数据库

java - 卢塞恩 : how to get a line of occurence of query

java - JMapViewer、MouseListener调用2次

java - Joshua Bloch 在 Effective Java 中解释的枚举类型

mysql - 将消息分组为对话

javascript - 将 node.js 变量导入数据库

java - 有没有比 jdb 更好的 Java CLI 调试器工具?

php - 使用 PDO 将时间插入 MySQL 数据库

php - php执行顺序