RunCodes
Programming & Tech

How to Insert, Delete, Update And Display Data in jTable? [With Source Code]

10 15,256

Hello guyz, in this tutorial i am gonna talk about insertion, deletion, updation and displaying data in jtable in is stored in your MySQL Database using java programming language with the help of Netbeans IDE. So, lets start this simple but very useful and very very important step that is used in any of either big or small project.

STEPS:

 

[sociallocker]

1. I am hoping that you have Netbeans IDE and any server application. if not download from here:

i am using wamp server so:

Wamp server: http://www.wampserver.com/en/

Netbeans IDE: https://netbeans.org/downloads/

i am hoping that you have install jdk and jre in your system, if not follow this tutorial: click here

2. After download and installing wamp server open it, and then open your localhost, for this open up your any browsing software and type localhost or 127.0.0.1 in the address bar. Then new page will appear then scroll down and find phpmyadmin and click on that. Then you will be able to see the login page. Login in by entering username and password (Default username is root and password is empty). And then click on new and create one database having any name you want and the click ok, then create table having any number of field you want but i am using four field in this case and click on go. After that, you need to insert the field name like: Roll No, Name, Address etc. and then select the length as well as data type as your requirement and then click on save. You can watch the video for this designing part, you may understand better in video. Make sure that make any one filed primary key, generally Roll No is set as PK because of unique identification than other field.

3. Open your Netbens IDE and click on File>New and then select java from the categories and java application from project and click next and then the provide the name of your project as you want and click finish. After completing this, you may see new folder in your project explorer in your Netbeans. Expand that folder, expand source package folder and under that folder another sub-folder will be appear. So right click on that folder and click on new and then select jframe form.

New window will appear and then provide the name of your form and then click finish.

4. Then you may able to see the full window having jframe form, drag the corner to expand it and from the palette drag and drop four label, text field and button and change the text of label as well as button and variable name of text field from their properties window just by right clicking.

5. Now you need to have MySQL connector for java so download it.

https://dev.mysql.com/downloads/connector/j/

Now double click on any button, after doing this you will go to the source code file. Scroll up and find the main class of your project and under that class declare these variable:

 Connection con = null;
 PreparedStatement pst = null;
 ResultSet rs = null;

6. Goto the jframe form and then double click on the insert button and write the following piece of code and run your project by right clicking the the jframe form from the project explorer and then click on run file but before running your file you need to add the MySQL Connector for java. So right click on the libraries folder from the project explorer, then click on add JAR/folder and select that downloaded jar file and click on add.

Insert Button:

 try{
 String sql = "INSERT INTO idusjavanew"
 +"(fname, lname, address)"
 +"VALUES (?,?,?)";
 con = DriverManager.getConnection("jdbc:mysql://localhost/idusjavanew","root","");
 pst = con.prepareStatement(sql);
 pst.setString(1,fname.getText());
 pst.setString(2,lname.getText());
 pst.setString(3,add.getText());
 pst.executeUpdate();
 JOptionPane.showMessageDialog(null, "inserted successfully");
 
 }
 catch(SQLException | HeadlessException ex){
 JOptionPane.showMessageDialog(null, ex);
 }

7. Go back to the jframe form and double click on the delete button and write the following piece of code:

Delete Button:

 try{
 String sql = "DELETE FROM idusjavanew WHERE Rollno =?";
 con = DriverManager.getConnection("jdbc:mysql://localhost/idusjavanew","root","");
 pst = con.prepareStatement(sql);
 pst.setString(1,Rollno.getText());
 pst.executeUpdate();
 JOptionPane.showMessageDialog(null, "delete successfully");
 
 }
 catch(SQLException | HeadlessException ex){
 JOptionPane.showMessageDialog(null, ex);
 }

8. Go back to the jframe form and double click on the update button and write the following piece of code:

Update Button:

try{
 String sql = "UPDATE idusjavanew SET fname=?,lname=?,address=? WHERE Rollno=?";
 con = DriverManager.getConnection("jdbc:mysql://localhost/idusjavanew","root","");
 pst = con.prepareStatement(sql);
 pst.setString(4, Rollno.getText());
 pst.setString(1,fname.getText());
 pst.setString(2,lname.getText());
 pst.setString(3,add.getText());
 pst.executeUpdate();
 JOptionPane.showMessageDialog(null, "updated successfully");
 
 }
 catch(SQLException | HeadlessException ex){
 JOptionPane.showMessageDialog(null, ex);
 }

9. Now go back to the jframe form and from the palette drag and drop jtable on the form. Now you need to download one for application:

HERE

or you can search in google and download form any website:

After that you need to add this jar file in your library just as we already add MySQL connector for java in the library folder of your project.

10: Now create one function [in my case showTableData() ]and write the following code:


public void showTableData(){
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/idusjavanew","root","");
String sql = "SELECT * FROM idusjavanew";
pst = con.prepareStatement(sql);
rs=pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);

}

}

and finally copy the name of this function and paste at the end of every button code and especially inside the constructor of your jframe i.e under the initComponents() function as:

initComponents();
showTableData();

 

[/sociallocker]

WATCH THESE VIDEO FOR MORE INFO:

 

 

Show Comments (10)