Getting MySQL unique id in JFrame

SWING MySQL

JFrame to MySQL & getting unique ID Inserting data to MySQL table in Part I

In Part 1 we have seen how the data is stored in MySQL table. Here we are not sure about success of inserting record to table. By using one auto increment field we can generate one unique ID for the record, just added to the table.

getGeneratedKeys() to collect the unique id from the MySQL database.

By using JFrame we have already created one class Collect.java to collect data from the user. The same JFrame we will modify by adding one JTextField to display the inserted ID of the record.

My_update.java

To insert data into mysql table we have used my_update.java class and my_db_update() method. We will use the same file to modify the code.

Now our method my_db_update() will return a string, so we have to modify it by replacing void with String.
We will declare one private variable str_return to return the insert id to main calling program at my_db_update() method.

public class my_update{
private String str_return;	
String my_db_update(String str1,String str2,String str3,String str4){
		try{  
In part 1 of data insert we have seen how to add data to student table. Now after inserting the data we will collect the insert id by using getGeneratedKeys(). Using this id we will collect the record from the student table in MySQL database.

We will assign the value of ID and name as we get from table to our string variable str_return. This value we will return to our main calling class Collect.java.

Collect.java

In this file we have used for JTextField to collect field data ( name , class, mark and sex ) from the user. Now we will add one more JTextFeild ( t5 ) and assign the return value of My_db_update() ( str_return ) by using setText(). Here is the modified code.
my_update obj=new my_update();
t5.setText(obj.my_db_update(name, class1, mark, sex));
Now our JTextFeild t5 will display the ID and name of the just inserted record.
The complete code for ActionListener ( of Submit button b1 ) is here
b1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		String name=t1.getText();
		String class1=t2.getText();
		String mark=t3.getText();
		String sex=t4.getText();
		// creating one object 
		my_update obj=new my_update();
		t5.setText(obj.my_db_update(name, class1, mark, sex));
	}
});
Swing JFrame showing inserted id and name



swing


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer