Display MySQL table values using Java
For this, you can use the ResultSet concept. For connection, we will be using the MySQL JDBC Driver.
Example
mysql> create table demo87 -> ( -> name varchar(20), -> age int -> ) -> ; Query OK, 0 rows affected (0.62
Insert some records into the table with the help of insert command −
Example
mysql> insert into demo87 values('John',21); Query OK, 1 row affected (0.15 mysql> insert into demo87 values('David',23); Query OK, 1 row affected (0.12 mysql> insert into demo87 values('Bob',22); Query OK, 1 row affected (0.16
Display records from the table using select statement −
Example
This will produce the following output −
Output
+-------+------+| name | age |
+-------+------+| John | 21 |
| David | 23 || Bob | 22 |
+-------+------+3 rows in set (0.00 sec)
Following is the Java code to display table values in MySQL −
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import com.mysql.jdbc.Statement; public class TableValuesDemo < public static void main(String[] args) < Connection con = null; Statement statement = null; try < HashMap hm = new HashMap<>(); Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledatabase", "root", "123456"); statement = (Statement) con.createStatement(); String sql; sql = "select *from demo87"; ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()) < hm.put(resultSet.getString("name"), resultSet.getInt("age")); >System.out.println(hm); > catch (Exception e) < e.printStackTrace(); >> >
This will produce the following output −
Output
Following is the snapshot of sample output −
How to Use JTable to Display Data
In this tutorial, you will learn how to create table for displaying tabular data using JTable class in Java swing application.
JTable class represents Swing table component. JTable provides rich functionality for managing appearance and behavior of the table component. JTable directly extends JComponent and implements several model listener interfaces such as TableModelListener, TableColumnModelListener, ListSelectionListener… etc. JTable also implements scrollable interface therefore table is usually placed in a JScrollPane.
Each JTable has three models TableModel, TableColumnModel, and ListSelectionModel.
- TableModel is used to specify how the table’s data is stored and retrieval of this data. JTable’s data is often in two-dimensional structure such as a two-dimension array or a Vector of Vectors. TableModel also is used to specify how data can be editable in the Table.
- TableColumnModel is used to manage all TableColumn in term of column selection, column order and margin size.
- ListSelectionModel allows table to have different mode of selection such as single, single interval and multiple interval selections.
We can create a table by using its constructors as follows:
JTable Constructors | Description |
---|---|
JTable() | Create an empty table |
JTable(int rows, int columns) | Create a table with rows and columns empty cell. |
JTable(Object[][] data, Object[] heading) | Create a table with data specify in two-dimensional array data and column heading heading |
JTable(TableModel dm) | Create a table with a given TableModel |
JTable(TableModel dm, TableColumnModel cm) | Create a table with a given TableModel and TableColumnModel. |
JTable(TableModel dm,TableColumnModel cm, ListSelectionModel sm) | Create a table with a given TableModel, TableColumnModel, and ListSelectionModel. |
JTable(Vector data, Vector heading) | Create a table with data in vector of Vectors data and column headings headin. |
Example of using JTable to display stock quotes
In this example, we will display stock quotes in a table using JTable class.
- First we specify the column heading in the columns array.
- Then we use two-dimensional array data to store stock quotes data.
- Next we create an instance of JTable by passing table data and column heading to the constructor.
- Finally we place the table JScrollPane and add it to the main frame.
Here is the screenshot of the JTable demo application:
Code language: JavaScript (javascript)package jtabledemo1; import javax.swing.*; import java.awt.*; public class Main < public static void main(String[] args) < final JFrame frame = new JFrame("JTable Demo"); String[] columns = "Code", "Name", "High", "Low", "Close", "Volume", "Change","Change %">; Object[][] data = < "MBF", "CITYGROUP", 10.16, 10.16, 10.16, 200, 0.08,0.79>, "MBL", "BANK OF AMERICA", 12.66, 12.66, 12.66, 6600, 0.13,1.04>, "MJP", "Morgan Stanley Dean Witter & Co.", 24.97, 24.97, 24.97, 1000, -0.04,-0.16> >; JTable table = new JTable(data, columns); JScrollPane scrollPane = new JScrollPane(table); table.setFillsViewportHeight(true); JLabel lblHeading = new JLabel("Stock Quotes"); lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24)); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START); frame.getContentPane().add(scrollPane,BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(550, 200); frame.setVisible(true); > >