如何在Java JFrame表中插入递增的主键和外键?

时间:2018-08-28 12:01:06

标签: java mysql jframe

我是Java的新手,我正在尝试将数据插入到netbeans jframe的三个表中。我已经在MySql上创建了数据

第一个表包含主键

create table TraineeDetals(
TraineeID smallint unsigned not null auto_increment primary key,
FirstName varchar(100) not null,
MiddleName varchar(100) not null,
LastName varchar(100) not null
)

而另一个表具有外键

create table CollegeDetails(
collegeName varchar(100) not null,
Specialization varchar(100) not null,
TraineeID smallint unsigned not null,
constraint 'fk_Trainee'
foreign key (TraineeID) references TraineeDetails (TraineeID)
on delete cascade
on update restrict
)

现在我想通过使用jframe插入数据,因此在Java代码中我这样做了

   String url="jdbc:mysql://localhost/mysql";
   String user="root";
   String pass="root";

   private void 
   SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {  

   try{

        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection(url, user, 
        pass);
        String sql="Insert into TraineeDetails 
        (FirstName,MiddleName,LastName)values(?,?,?)";
        PreparedStatement pst =con.prepareStatement(sql);

        pst.setString(1, firstNameTextFiled.getText());
        pst.setString(2, MiddleNameTextField.getText());
        pst.setString(3, LastNameTextField.getText());

        String sql2 = "Insert into CollegeDetails (CollegeName, 
        Specialization,TraineeID)values(?,?,?)";
        PreparedStatement pst2 =con.prepareStatement(sql2);

        pst2.setString(1, CollegeNameTextField.getText());
        pst2.setString(2, SpecializationTextField.getText());
        pst.setString(3, TraineeIDTextField.getText());


        pst.executeUpdate();
        pst2.executeUpdate();


        JOptionPane.showInputDialog(this,"Insert 
        Successfully");

        clearText();

    }
    catch(Exception e)
    {
        JOptionPane.showInputDialog(this,e.getMessage());
    }
    }

此代码仅显示表TraineeDetails的数据

必须从此处添加所有数据,并且ID必须是自动递增的。

All data must be added from here and the id has to be auto increment

之后,管理员可以显示他在此​​处添加的数据。 Jframe仅显示TraineeDetails表的数据,我该如何解决

jframe shows data only for TraineeDetails table how can I fix it

请问我要插入自动增量主键和外键并在添加它们之后一起显示数据,我该怎么办?那我的Java代码又如何呢?

1 个答案:

答案 0 :(得分:0)

您可以观看此视频,以更好地了解该怎么做 https://www.youtube.com/watch?v=NC6bY2oJr7s

这是您可以做到的...

第一个make类让我们将其称为sql.java并将其导入到您的项目类库中

在其中写入此代码

import java.sql.PreparedStatment;

public class sql {
public void id_incrementtable() {

int id = 1 ;
PreparedStatment ps = null;
ResultSet rs = null ; 
Conectar db = new Conectar() ;
try {
ps = db.getConnection().prepareStatement("select max(id) from your 
tablename");
rs = ps.executeQuery();
while(rs.next()) (
id = rs.getint(1) + 1 ; 
)
}catch(Exception ex);

System.out.printin("error"+ex.getMessage());


}
finally ( 
try (
ps.close();
rs.close();
db.desconectar() ;
) catch(Exception ex) ( 

)

}

//通过运行以上命令,您会发现每次将记录保存在表上时,它都会在表名中添加自动序列号

//保存数据按钮代码,您首先定义sql s = new sql()以便从添加到您的项目类引用中的该类中读取

sql s = new sql(); int id = s.id_incrementable();

//和您将剩余的代码放在这里...我想您有个主意