How to get data from database to JTable in java using NetBeans

How to get data from database to JTable in java using NetBeans

import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author HP
 */
public class MySqlToJtable {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException {
        // TODO code application logic here
    // Connect to the database
        Connection con = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/test", "root", "");

        // Create a SQL query
        String query = "SELECT * FROM users";

        // Execute the query and retrieve the results
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        // Create a table model
        DefaultTableModel model = new DefaultTableModel();

        // Add the columns to the model
        model.addColumn("Column 1");
        model.addColumn("Column 2");
        model.addColumn("Column 3");

        // Add the rows to the model
        while (rs.next()) {
            String col1 = rs.getString("column1");
            String col2 = rs.getString("column2");
            String col3 = rs.getString("column3");
            model.addRow(new Object[] { col1, col2, col3 });
        }

        // Create a JTable and set the model
        JTable table = new JTable();
        table.setSize(new Dimension(100, 800));

        table.setModel(model);

        // Add the table to a scroll pane
        JScrollPane scrollPane = new JScrollPane(table);

        // Add the scroll pane to the content pane
        JFrame frame = new JFrame();
        frame.setSize(700, 600);

        frame.add(scrollPane);
        frame.setVisible(true);
    }}

Here is a sample MySQL query that creates a table with three string columns and inserts 10 rows into it:

CREATE TABLE users (
   column1 VARCHAR(255),
   column2 VARCHAR(255),
   column3 VARCHAR(255)
);

INSERT INTO users (column1, column2, column3)
VALUES
   ('value1', 'value2', 'value3'),
   ('value4', 'value5', 'value6'),
   ('value7', 'value8', 'value9'),
   ('value10', 'value11', 'value12'),
   ('value13', 'value14', 'value15'),
   ('value16', 'value17', 'value18'),
   ('value19', 'value20', 'value21'),
   ('value22', 'value23', 'value24'),
   ('value25', 'value26', 'value27'),
   ('value28', 'value29', 'value30')
;

This query creates a table called “users” with three string columns called “column1”, “column2”, and “column3”. It then inserts 10 rows into the table, with each row containing three string values for the respective columns.






phpMyAdmin SQL Dump

-- phpMyAdmin SQL Dump
-- version 5.1.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 24, 2022 at 01:48 AM
-- Server version: 10.4.20-MariaDB
-- PHP Version: 8.0.8

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `test`
--
CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `test`;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `column1` varchar(255) DEFAULT NULL,
  `column2` varchar(255) DEFAULT NULL,
  `column3` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value1', 'value2', 'value3');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value4', 'value5', 'value6');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value7', 'value8', 'value9');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value10', 'value11', 'value12');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value13', 'value14', 'value15');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value16', 'value17', 'value18');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value19', 'value20', 'value21');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value22', 'value23', 'value24');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value25', 'value26', 'value27');
INSERT INTO `users` (`column1`, `column2`, `column3`) VALUES('value28', 'value29', 'value30');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Netbeans Project Source Code – https://github.com/mauricemuteti/How-to-get-data-from-database-to-JTable-in-java-using-NetBeans

Leave a Reply

Your email address will not be published. Required fields are marked *