如何将一个表中的主键值插入另一个表中的外键列?

时间:2016-11-29 15:36:32

标签: mysql sql jdbc

我是SQL和编程的新手,所以如果答案很明显,请耐心等待。我有一张名为"门票"其中包含主键ticket_id。我还有一个名为" contact_info"它存储创建票证的人的联系信息。在此表中,ticket_id是名为ticket_number的外键。用户通过GUI插入票证,并且ticket_number在数据库中自动递增。如何选择ticket_number并将其插入到包含创建该票证的人员的contact_info的行中?

这是现在的代码,它不符合我的预期:

try{
              //Open a connection
              System.out.println("Connecting to a selected database...");
              this.connection = DriverManager.getConnection(url, username, password);
              System.out.println("Connected database successfully...");

              //Execute a query
              System.out.println("Inserting records into the table...");
              PreparedStatement pstmt = connection.prepareStatement(" INSERT INTO s_fuse_ticket_table "
                              + " (summary, status, severity, classification, type, internal_notes, description, assignees) "
                              + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)");

                          if(summary.getText().equals("")){
                              throw new SQLException("Summary cannot be blank");
                          }
                          pstmt.setString(1, summary.getText());
                  pstmt.setString(2, status.getSelectionModel().getSelectedItem().toString());
                  pstmt.setString(3, severity.getSelectionModel().getSelectedItem().toString());
                          if(classification.getSelectionModel().getSelectedItem().toString().equals("Make a Selection")){
                              throw new SQLException("Please select a classification");
                          }
                          pstmt.setString(4, classification.getSelectionModel().getSelectedItem().toString());
                          if(type.getSelectionModel().getSelectedItem().toString().equals("Make a Selection")){
                              throw new SQLException("Please select a type");
                          }
                          pstmt.setString(5, type.getSelectionModel().getSelectedItem().toString());
                          pstmt.setString(6, internalNotes.getText());
                          pstmt.setString(7, description.getText());
                          pstmt.setString(8, assignee.getSelectionModel().getSelectedItem().toString());
                  pstmt.executeUpdate();

                          //Execute a query
                        System.out.println("Inserting records into the table...");
                        PreparedStatement pstmt2 = connection.prepareStatement(" INSERT INTO s_fuse_contact_info_table "
                              + " (ticket_number, email, last_name, first_name) "
                              + " VALUES (?, ?, ?, ?)");

                          pstmt2.setString(1, ("SELECT ticket_id FROM s_fuse_ticket_table"));
                          /*if(!email.getText().contains("@") && !email.getText().contains(".")){

                          }*/
                          pstmt2.setString(2, email.getText());
                          pstmt2.setString(3, lname.getText());
                          pstmt2.setString(4, fname.getText());
                  pstmt2.executeUpdate();
           }catch(SQLException se){
              //Handle errors for JDBC
              se.printStackTrace();
           }catch(Exception e){
              //Handle errors for Class.forName
              e.printStackTrace();
           }finally{
              //finally block used to close resources
              try{
                 if(stmt!=null)
                 {
                    connection.close();
                 }
              }catch(SQLException se){
              }// do nothing
              try{
                 if(connection!=null)
                 {
                    connection.close();
                 }
              }catch(SQLException se){
                 se.printStackTrace();
              }//end finally try
           }//end try
           System.out.println("Goodbye!");

1 个答案:

答案 0 :(得分:0)

不同的DBMS有不同的方法 - 请参阅this question获取相当全面的列表。

另一种方法是使用交易。如果MVCC DBMS处于合适的隔离级别(可重复读取或可序列化),当您启动新连接时,您将拥有自己的数据库视图,因此您可以INSERT INTO主表,然后SELECT与您的插入匹配的最新行,并确信它属于您。最好将这样的插件捆绑在一起,因为它可以防止任何不一致的数据进入系统 - 只需确保你之后提交!