表模型无法从数据库中检索数据

时间:2014-06-26 03:40:49

标签: java sql swing

我编写了TableModel表。我还成功添加了表列名但无法从数据库中检索数据。如果任何人对此帖有疑问,请发表评论。

错误

Unable to retrieve the data from the database.

代码

import java.awt.BorderLayout;
import java.awt.EventQueue;
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 class Customer_Bills 
{
public static void main(String[] args) 
{
    Customer_Bills testTable = new Customer_Bills();
          }
      public Customer_Bills() 
      {
    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 Customer_Bills.Bill());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
         }    
           public class Bill extends JPanel implements ActionListener 
        {
    JTextField textFieldId;
    JLabel l1;
    JLabel l2;
    JButton b1,b2,b3;
    JTextField sun,sunr,sat,satr,oth,othr;
    float sum1,totall;
    ResultSet rs1 = null;
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    private int rows;
    public Bill() 
    {
        setLayout(new BorderLayout());            
        JPanel fields = new JPanel();
        textFieldId = new JTextField(10);
        l1 = new JLabel("New Customer Entry :-");
        l2 = new JLabel("Customer Id");
        b1 = new JButton("OK");
        b2 = new JButton("Calculate");
        b3 = new JButton("Print");
        fields.add(l2);
        fields.add(textFieldId);
        fields.add(b1);
        fields.add(b2);
        fields.add(b3);
        add(fields, BorderLayout.NORTH);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        // Don't forget to add a table.
        add(new JScrollPane(new JTable(model)));
    }
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        System.out.println("You clicked the button");
        Connection con;
        if (e.getSource() == b1) 
        {
            PreparedStatement ps = null;
            Statement stmt = null;
            try 
            {

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:Dalvi");
                ps = con.prepareStatement("SELECT  * FROM customer_details where 
                 Customer_id = ?");
                ps.setString(1, textFieldId.getText());
                rs1 = ps.executeQuery();
                model.addColumn("Paper Name");
                model.addColumn("Monday");
                model.addColumn("Tuesday");
                model.addColumn("Wednesday");
                model.addColumn("Thrsday");
                model.addColumn("Friday");
                model.addColumn("Saturday");
                model.addColumn("Sunday");
                model.addColumn("Magzine Name");
                model.addColumn("Quantity");
                model.addColumn("Total");
                while (rs1.next()) 
                {                           
                        model.addRow(new Object[] 
           {rs1.getString(1),rs1.getString(2),rs1.getString(3),rs1.getString(4)
      ,rs1.getString(5),rs1.getString(6),rs1.getString(7),rs1.getString(8), 
        rs1.getString(9), rs1.getString(10),rs1.getString(11)});
                }
                Vector data = model.getDataVector();

                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)
                {
                }
            }
            if (e.getSource() == b2)     
        {
             int rowCount = table.getRowCount();


             for(int i =1; i<rowCount;i++)
             {
                 Object valuea = table.getValueAt( rowCount, 5 );
                 Object valueb = table.getValueAt( rowCount, 6 ); 
                 Object valuec = table.getValueAt( rowCount, 7 );
                 Object valued = table.getValueAt( rowCount, 8 );
                 Object valuee = table.getValueAt( rowCount, 9 );
                 Object valuef = table.getValueAt( rowCount, 10 );
                 sum1=(float)((float) 
               (Double.parseDouble(String.valueOf(valuea))*Double.parseDouble(  
                String.valueOf( valueb )))+
                 (Double.parseDouble(String.valueOf(valuec))*Double.parseDouble( 
                 String.valueOf( valued )))+
                (Double.parseDouble(String.valueOf(valuee))*Double.parseDouble(  
                String.valueOf( valuef ))));                   
                // table.setValueAt(sum1, rowCount,11); 
                 String query = "INSERT INTO Customer 
             values(table.getModel().setValueAt(sum1, rowCount,11)";
                    try {
                        stmt.executeUpdate(query);
                    } catch (SQLException ex) {                             
          Logger.getLogger(Customer_Bills.class.getName()).log(Level.SEVERE, null, ex);
                   }}     }           }   }}}

0 个答案:

没有答案