How to insert data from a JTable into a Database

So guys today i’ll show you how to insert data from a JTable into a mysql Database.I have created a project called ItStuff and created some classes for as the main class( Database operations ( and a JFrame ( for give you a better and clear idea about this operations.

Screen Shot 2558-11-16 at 12.57.05 AM

This is the JFrame i have created with just a table and a button.

Screen Shot 2558-11-16 at 12.55.20 AM

And this is my database where i’m going to put the details in the first two columns.

Screen Shot 2558-11-16 at 12.50.27 AM

So now we’ll go to the database operations.

First I have implemented the connection with the database giving the url,user name and password.

and also a method called addData with the parameter of an array list and the method basically carries out the database query we need to perform.

package itstuff;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
 * @author akilajwasala
public class Dbops {
    String url = "jdbc:mysql://localhost:3306/ITStuffDB?useUnicode=true&characterEncoding=utf-8";
    String username = "root";
    String password = "";
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    public boolean addData(ArrayList<ArrayList<String>> table){
       try {
          for (int i = 0; i < table.size(); i++) {
             con = (Connection) DriverManager.getConnection(url, username, password);
             String query = "INSERT INTO Data (Col1,Col2)VALUES (?,?)";
             pst = (PreparedStatement) con.prepareStatement(query);
             pst.setString(1, table.get(i).get(0));
             pst.setString(2, table.get(i).get(1)); 
          }return true;
       } catch (Exception e) {
           return false;
       } finally {
            try {
               if (pst != null) {
               if (con != null) {
            } catch (Exception e) {

And here is the code i have written for the Submit button.The array lists are the main data structure i used to collect all the relevant data from the table.

private void SubmitButtonMouseClicked(java.awt.event.MouseEvent evt) {
      ArrayList<ArrayList<String>> table = new ArrayList<ArrayList<String>>();
      for (int rowNumber = 0; rowNumber < jTable1.getRowCount(); rowNumber++) {
         int columnNumber = 0;
         ArrayList<String> row = new ArrayList<String>();

         while (columnNumber < 3 && jTable1.getValueAt(rowNumber, columnNumber) != null) {
            row.add(jTable1.getValueAt(rowNumber, columnNumber).toString());
         if (row.size() != 0) {
   int[] column1=new int[table.size()];
   int[] column2=new int[table.size()];

   for (int i = 0; i < table.size(); i++) {
      column1[i] = Integer.parseInt(table.get(i).get(1));
      column2[i] = Integer.parseInt(table.get(i).get(2));


   }catch(Exception e){

I hope you understand the way of doing this operation.Try out..

Feel free to ask questions regarding these codes.












One thought on “How to insert data from a JTable into a Database

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s