Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Aman GuptaRank #193 · 12.4K reputationPosted Aug 7, 2024, 2:41 AM EDT
Hi Kivutha,
To retrieve data from a database and display it in a table in a Java application, you can follow these steps:
JTableto display the data in a Swing application.Here is an example that demonstrates these steps:
Step 1: Set up your environment
Make sure you have the JDBC driver for your database (e.g.,
mysql-connector-javafor MySQL).Step 2: Create a database connection
Create a class to handle the database connection and data retrieval.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DatabaseConnection { private static final String URL = "jdbc:mysql://localhost:3306/your_database"; private static final String USER = "your_username"; private static final String PASSWORD = "your_password"; public static Connection getConnection() { try { return DriverManager.getConnection(URL, USER, PASSWORD); } catch (Exception e) { e.printStackTrace(); return null; } } public static ResultSet getData() { Connection connection = getConnection(); if (connection != null) { try { Statement statement = connection.createStatement(); return statement.executeQuery("SELECT * FROM your_table"); } catch (Exception e) { e.printStackTrace(); } } return null; } }Step 3: Retrieve data from the database
Fetch the data using the
DatabaseConnectionclass.Step 4: Display the data in a table
Create a Swing application with a
JTableto display the data.import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.sql.ResultSet; import java.sql.ResultSetMetaData; public class DataTable extends JFrame { public DataTable() { setTitle("Database Table"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // Create the table model DefaultTableModel tableModel = new DefaultTableModel(); JTable table = new JTable(tableModel); // Fetch data from the database ResultSet resultSet = DatabaseConnection.getData(); if (resultSet != null) { try { ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); // Add column names to the table model for (int i = 1; i { DataTable dataTable = new DataTable(); dataTable.setVisible(true); }); } }Explanation
JTableto display the data.Replace
"jdbc:mysql://localhost:3306/your_database","your_username","your_password", and"your_table"with your actual database details.This example demonstrates how to connect to a MySQL database, retrieve data, and display it in a JTable. Adjustments might be necessary based on your specific database and requirements.
Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted Aug 7, 2024, 3:36 AM EDT
import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.sql.*; public class DatabaseTableExample { public static void main(String[] args) { // Database credentials String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "myuser"; String password = "mypassword"; // Table model DefaultTableModel model = new DefaultTableModel(); // Column names model.addColumn("ID"); model.addColumn("Name"); model.addColumn("Email"); // Retrieve data from database try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable")) { // Add rows to table model while (rs.next()) { model.addRow(new Object[]{rs.getInt("id"), rs.getString("name"), rs.getString("email")}); } } catch (SQLException e) { System.out.println("Error: " + e.getMessage()); } // Create JTable and display data JTable table = new JTable(model); JScrollPane pane = new JScrollPane(table); JFrame frame = new JFrame("Database Data"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(pane); frame.pack(); frame.setVisible(true); } }Error handling is minimal in this example. Add proper error handling