如何在java jframe表中插入递增的主键和外键?

问题描述 投票:0回答:1

我是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代码怎么样,有什么东西不见了?

java mysql jframe
1个回答
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();

//你的地方剩下的代码在这里......我想你有了主意

© www.soinside.com 2019 - 2024. All rights reserved.