This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable issue in swing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable issue in swing?" Watch "JTable issue in swing?" New topic

JTable issue in swing?

Devendra Ghag

Joined: Jun 19, 2013
Posts: 14
i want my customer detail in tabular formate in swing. when i enter the ok button on screen.
my database is look like
Customer_id CUstomer _name Contact New_paper_name
1 abc 123 xyz
2 ABC 456 XYZ
please help me out?

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class TestTable {

public static void main(String[] args) {
TestTable testTable = new TestTable();

public TestTable() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());
frame.add(new TestTable.Bill());

public class Bill extends JPanel implements ActionListener {

JTextField textFieldId;
JLabel l1;
JLabel l2;
JButton b2;
ResultSet rs1 = null;
DefaultTableModel dtm = new DefaultTableModel();

public Bill() {
setLayout(new BorderLayout());

JPanel fields = new JPanel();

textFieldId = new JTextField(10);
l1 = new JLabel("New Customer Entry :-");
l2 = new JLabel("Customer Id");
b2 = new JButton("Ok");


add(fields, BorderLayout.NORTH);


// Don't forget to add a table.
add(new JScrollPane(new JTable(dtm)));


public void actionPerformed(ActionEvent e) {

System.out.println("You clicked the button");
if (e.getSource() == b2) {
PreparedStatement ps = null;
try {
Connection con;
con = DriverManager.getConnection("jdbcdbc:devendra");
ps = con.prepareStatement("SELECT * FROM Customer where Customer_Id = ?");
// You must bind the parameter with a value...
ps.setString(1, textFieldId.getText());
rs1 = ps.executeQuery();
while ( {
dtm.addRow(new Object[]{
rs1.getString(1), rs1.getString(2), rs1.getInt(3), rs1.getString(4)});
JOptionPane.showMessageDialog(null, "You successfully Enter the Entry");
} catch (SQLException s) {
System.out.println("SQL code does not execute.");
JOptionPane.showMessageDialog(null, "Please Enter the Detail Correctly");
} catch (Exception exp) {
JOptionPane.showMessageDialog(this, "Failed to perform query: " + exp.getMessage());
} finally {

try {
} catch (Exception ex) {


wood burning stoves
subject: JTable issue in swing?
It's not a secret anymore!