将特定用户的数据显示在JTable上

时间:2013-06-25 02:05:49

标签: java swing jdbc jtable odbc

我希望使用JTable向java页面显示特定的客户详细信息 当我输入客户ID然后按下然后我想要客户的所有细节都在同一窗口或另一个窗口?

怎么做?我尝试使用以下代码但失败了。

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

1 个答案:

答案 0 :(得分:2)

我可以看到一些对你没有帮助的东西。

  1. Billabstract。它没有理由abstract
  2. 没有JTable。这实际上非常令人惊讶,因为您有一个实际的TableModel
  3. 您正在使用null布局。这永远不会帮助你。
  4. 你是从JFrame延伸出来的。这不是一个好主意,因为它会立即阻止您以其他形式重复使用此类。
  5. 您的查询未将任何值绑定到变量参数。
  6. 你的异常处理有点不尽如人意......
  7. 更好的方法可能看起来像......

    enter image description here

    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) {
                        }
    
                    }
                }
            }
        }
    }
    

    我想你可能想花一点时间阅读......