Inserting data to MySQL table using JFrame

SWING MySQL



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

Prerequisite

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.

Create Collect.java

File > new > other > JFrame
Give the name as Collect.java

Create my_update.java

File > new > Class  > my_update.java 

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.

My_update.java

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 Collect.java, 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

my_update.java

Create one more class file as my_update.java . 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 my_update.java 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) {
try{  
	Class.forName("com.mysql.jdbc.Driver");  
	// database is my_tutorial, userid =root, password //
       // Update your usrid, password and database name // 	
	Connection con=DriverManager.getConnection(  
	"jdbc:mysql://localhost:3306/my_tutorial","root","password");
	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. 
	
	con.close();  
	
}catch(Exception e){ System.out.println(e);} 
	//////////////////////////////
}
}

Creating the objects and passing data

Open our Collect.java 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 Collect.java window, will get passed to my_update.java class through method my_db_update() and record will be added.

Getting Unique ID of inserted record in Part II

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