• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable issue in swing?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i enter the customer id then pressed ok the detail of that particular customer will not apper in the jtable?
why these is so please help me out from these sitivuation please
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() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestTable.Bill());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

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");

fields.add(l2);
fields.add(textFieldId);
fields.add(b2);

add(fields, BorderLayout.NORTH);

b2.addActionListener(this);

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

}

@Override
public void actionPerformed(ActionEvent e) {

System.out.println("You clicked the button");
if (e.getSource() == b2) {
PreparedStatement ps = null;
try {
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc: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 (rs1.next()) {
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 {
ps.close();
} catch (Exception ex) {
}

}
}
}
}
}

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic