我想在JTable
输入客户 ID 然后按下时在 java 页面上显示特定的客户详细信息,然后我希望客户的所有详细信息显示在同一个窗口或另一个窗口上?
怎么做?我尝试使用以下代码但失败了。
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public abstract class Bill extends JFrame implements ActionListener
{
JTextField textFieldId;
JLabel l1;
JLabel l2;
JButton b2;
Container c = getContentPane();
ResultSet rs1 = null;
DefaultTableModel dtm = new DefaultTableModel();
Bill()
{
super("Shree Datta Digambar");
setBounds(140,250,777,555);
c.setLayout(null);
textFieldId = new JTextField();
l1 = new JLabel("New Customer Entry :-");
l2 = new JLabel("Customer Id");
l1.setBounds(10,10,340,20);
l2.setBounds(10,20,140,70);
textFieldId.setBounds(10,70,70,20);
b2 = new JButton("Ok");
b2.setBounds(10,160,50,20);
c.add(b2);
c.add(l1);
c.add(l2);
c.add(textFieldId);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
b2.addActionListener(this);
}
public static void main(String[] args)
{
Bill bc=new Bill() {};
}
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the button");
if(e.getSource()==b2)
{
try
{
Connection con;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Bill.class.getName()).log(Level.SEVERE, null, ex);
}
con =DriverManager.getConnection("jdbc:odbc:devendra");
java.sql.Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement(null);
ps=con.prepareStatement("SELECT FROM Customer
where Customer_Id = ?");
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");
}
}
}}