Inserting data to MySQL table using JFrame


We will create one JFrams and name it as Add one new class file to our project and name it as In the we will keep four JTextField and one JButton. In the second class file ( ) we will keep the code to update the data to MySQL table.


For better understanding first read the following tutorials.
Using class and method to insert data to MySQL

Transfering Data from one window to other

Create Project

Crate a project and name it as data_transfer.


File > new > other > JFrame
Give the name as


File > new > Class  > 

Adding MySQL driver

As we are using Java to connect to MySQL to manage the data, we have to use MySQL-connector.
Detail on how to install and connect to MySQL.

This is similar to the code given at inserting data to MySQL using class and method.
We have used the method my_db_update() of class my_update(). This method receives four variables ( attributes ) and insert the same to our student table in MySQL database.

Open our, we will go to design mode.
  1. Add absolute Layout
  2. Move to Components and Add one JTextField ( variable name t1 ) ,
  3. Similarly add 3 more JTextField ( t1, t2, t3, t4 ) , we will use them to collect Name, Class, Mark and Sex of the students and add one record using them in our student table.
  4. Add one JButton ( variable name b1 and change the text on it to Submit )
Swing JFrame to enter data

Create one more class file as . Inside this file we will add one method my_db_update(). This method will contain our MySQL connector, login access to MySQL and the query to add record.

More on MySQL connector
Read the class and method on how to insert data to MySQL.

We will copy the code given here for our file.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class my_update{
void  my_db_update(String str1, String str2,String str3,String str4) {
	// database is my_tutorial, userid =root, password //
       // Update your usrid, password and database name // 	
	Connection con=DriverManager.getConnection(  
	Statement st=con.createStatement();  
	int mark = Integer.parseInt(str3); // Mark is an integer
	// Adding record 
	String query1="INSERT INTO `my_tutorial`.`student`"
	 + " (`name`, `class`, `mark`, `sex`)"
	 + "VALUES('" +str1+"','"+str2+"',"+mark+",'"+str4+"')";
	st.executeUpdate(query1); // record added. 
}catch(Exception e){ System.out.println(e);} 

Creating the objects and passing data

Open our file.
Come to design mode and double click the JButton to add ActionListener() to it.
Let us first collect the text written at JTextField t1 to t4 and store them in a string variable. Then create one object for class my_update and using the same to pass the string variables to my_update() method.
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();
	obj.my_db_update(name, class1, mark, sex);
Execute the files. Now what ever data you enter in JTextfield of window, will get passed to class through method my_db_update() and record will be added.

Getting Unique ID of inserted record in Part II



* indicates required
Subscribe to plus2net

    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-2024 All rights reserved worldwide Privacy Policy Disclaimer