-1

我想在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");
                    }        
            }
        }}
4

1 回答 1

2

我可以看到很多事情对您没有帮助。

  1. Billabstract。没有理由这样做abstract
  2. 没有JTable。这实际上是非常令人惊讶的,因为你有一个实际的TableModel
  3. 您正在使用null布局。这永远不会帮助你。
  4. 您正在从JFrame. 这不是一个好主意,因为它会立即阻止您以其他形式重新使用此类。
  5. 您的查询不会将任何值绑定到变量参数。
  6. 您的异常处理还有一些不足之处......

更好的方法可能看起来像......

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestTable {

    public static void main(String[] args) {
        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 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)));

        }

        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.");
                    s.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Please Enter the Detail Correctly");
                } catch (Exception exp) {
                    exp.printStackTrace();
                    JOptionPane.showMessageDialog(this, "Failed to perform query: " + exp.getMessage());
                } finally {

                    try {
                        ps.close();
                    } catch (Exception ex) {
                    }

                }
            }
        }
    }
}

我想你可能想花更多时间阅读...

于 2013-06-25T02:27:55.703 回答