无法获得选定的行

时间:2013-12-18 11:45:20

标签: java swing

在表单加载时我的表是空的。当我们从jcomboBox中选择名称时,详细信息将加载到表中。现在我想更新记录。但无法获取选定的行进行更新。得到错误arrayIndexOutOfBound:-1 Plz帮助我解决这个问题。提前谢谢......

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.awt.*;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
class Credit extends JFrame implements ActionListener{
    private String value4="0";
    private String val="0";
    private String val1="0";
    private String jama="0",baki="0";
    private String nettdate="0",nettb="0",nettbal="0";
    private int row=0,count=0,aa=0,bb=0,t1=0;
    private String tj1="0",tb1="0",gt1="0";
    String h[]={"TID","Date","Jama","Baki","Nett"};
    private TableModel buildTableModel(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        Vector<String> columnNames = new Vector<String>();

        int columnCount = metaData.getColumnCount();


        for (int column = 0; column < columnCount; column++) {

            columnNames.add(h[column]);

        }
            Vector<String> vector1 = new Vector<String>();
            Vector<String> vector2 = new Vector<String>();

        Vector<Vector<String>> data = new Vector<Vector<String>>();
        while (rs.next()) {


                Vector<String> vector = new Vector<String>();

                vector.add(rs.getString(1));
                vector.add(rs.getString(2));
                vector.add(rs.getString(3));
                vector.add(rs.getString(4));
                vector.add(rs.getString(5));
                count++;
            data.add(vector);
        }

        for(int i=0;i<count;i++){
            aa=aa+Integer.parseInt(table.getValueAt(i, 2).toString());
            bb=bb+Integer.parseInt(table.getValueAt(i, 3).toString());
        }
        tj1=Integer.toString(aa);
        System.out.println("TJ:"+tj1);
        vector1.add("");
        vector1.add("Total");
        vector1.add(tj1);

        tb1=Integer.toString(bb);
        vector1.add(tb1);
        vector1.add("");
        System.out.println("h2:"+vector1);

        data.add(vector1);
        System.out.println("data:"+data);
        t1=Integer.parseInt(tb1)-Integer.parseInt(tj1);
        gt1=Integer.toString(t1);
        System.out.println("GT:"+gt1);

        vector2.add("");
        vector2.add("Nett Balance");
        vector2.add("");
        vector2.add(gt1);
        vector2.add("");
        data.add(vector2);
        System.out.println("data1:"+data);
        count=0;
        aa=0;
        bb=0;
        t1=0;
        tj1="0";
        tb1="0";
        gt1="0";

        return new DefaultTableModel(data, columnNames);
    }
    private static final int GAP = 5;
    private static final Font BTN_FONT = new Font(Font.DIALOG, Font.PLAIN, 15);
    private JPanel mainPanel = new JPanel();
    JButton add,cancel,show,search,print,update,delete,net;
    JTextField jTextField,jTextField1,jTextField2,jTextField3,jTextField4,jTextField5;
    JComboBox jComboBox;
    String date,paid,total,s2,s3,s6,unpaid,mobile,city,nett,s4,s5,u1,u2,u3,u4,u5,u6,accountno,tupdate;
    String Select[]={"Select"};
    Object name,s1;
    String an="0",nam="0",mono="0",cit="0";
    int token=0,tid=1,a,stid=0;
    JFrame f;
    AbstractAction action;
    private String id;
    Connection con=null;
            Statement st=null;
            ResultSet rs=null;
                        JPanel tablePanel;
                        DefaultTableModel model;
                        DefaultTableModel model1;
                        JTable table,table3;
  private Vector<Vector<String>> data; //used for data from database
  private Vector<Vector<String>> data1; //used for data from database
    private Vector<String> header; //used to store data header
    private Vector<String> header2; //used to store data header
    private Vector<String> header3;
    private Vector<String> header4;
    private JLabel jlab4,jlab5,jlab6,jlab7;

    Credit(JFrame frm){
        Toolkit tk=Toolkit.getDefaultToolkit();
    Image img=tk.getImage("1.jpg");
    setIconImage(img);
        JPanel creditPanel = createPanel1("Customer Credit & Debit Amount");
        tablePanel = createPanel2("Customer Credit & Debit Table");
        creditPanel.setBackground(Color.WHITE);
        tablePanel.setBackground(Color.WHITE);
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        mainPanel.add(creditPanel, BorderLayout.PAGE_START);
        mainPanel.add(tablePanel, BorderLayout.CENTER);
        creditPanel.setVisible(true);
        mainPanel.setBackground(Color.BLACK);
        frm.add(mainPanel);
}
    private JPanel createPanel2(String title){
        tablePanel=new JPanel();
        tablePanel.setLayout(new BoxLayout(tablePanel,BoxLayout.Y_AXIS));

        header = new Vector<String>();
        header.add("TID"); 
        header.add("Date"); 
        header.add("Jama"); 
        header.add("Baki"); 
        header.add("Nett"); 
        header4 = new Vector<String>();
        header4.add("A/C No."); 
        header4.add("Name"); 
        header4.add("Mobile"); 
        header4.add("City"); 
        model=new DefaultTableModel(data,header);
        model1=new DefaultTableModel(data1,header4);
        table = new JTable(model);
        table3 = new JTable(model1);
        JScrollPane scroll=new JScrollPane(table);
        JScrollPane scroll1=new JScrollPane(table3);
        scroll1.setPreferredSize(new Dimension(50,63));
        tablePanel.add(scroll1);
        tablePanel.add(scroll);

        JPanel p3=new JPanel();
        update=new JButton("Update");
        update.setFont(BTN_FONT);
        update.addActionListener(this);
        p3.add(update);
        tablePanel.add(p3);
        tablePanel.setBorder(BorderFactory.createTitledBorder(title));
        return tablePanel;
    }
    private JPanel createPanel1(String title) {
        JPanel addUnitPanel = new JPanel();
        addUnitPanel.setLayout(new GridLayout(4,1, GAP, GAP));
        JLabel jlab4=new JLabel("Account No.:");
        jTextField4=new JTextField();
        jlab4.setPreferredSize(new Dimension(100,10));
        jTextField4.setPreferredSize(new Dimension(150,30));
        JLabel jlab5=new JLabel("Nett:");
        jTextField5=new JTextField();
        jlab5.setPreferredSize(new Dimension(38,10));
        jTextField5.setPreferredSize(new Dimension(150,30));

        JLabel jlab=new JLabel("Name:");
        jComboBox=new JComboBox(Select);//Select
        jlab.setPreferredSize(new Dimension(100,10));
        jComboBox.setPreferredSize(new Dimension(150,30));
        JLabel jlab1=new JLabel("Date:");
        jTextField1=new JTextField();
        jlab1.setPreferredSize(new Dimension(100,10));
        jTextField1.setPreferredSize(new Dimension(150,30));
        JLabel jlab2=new JLabel("Jama:");
        jTextField2=new JTextField();
        jlab2.setPreferredSize(new Dimension(100,10));
        jTextField2.setPreferredSize(new Dimension(150,30));
        JLabel jlab3=new JLabel("Baki:");
        jTextField3=new JTextField();
        jlab3.setPreferredSize(new Dimension(100,10));
        jTextField3.setPreferredSize(new Dimension(150,30));

        String m[]={"01","02","03","04","05","06","07","08","09","10","11","12"};
        String m1[]={"00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
            Calendar cal=Calendar.getInstance();
    jTextField1.setText(m1[cal.get(Calendar.DATE)]+"/"+m[cal.get(Calendar.MONTH)]+"/"+cal.get(Calendar.YEAR));

        jComboBox.addActionListener(new ActionListener(){
            private int b=0,a=0;
            private String tj="0";
            private String tb="0";
            private int t=0;
            private String gt="0";


        public void actionPerformed(ActionEvent ae){
            try
        {
            Connection con=null;
            Statement st=null;
            ResultSet rs=null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/Program Files/Shop/shop.accdb";
                                password = "3064101991";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";"+
                            "Pwd="+password+";";
                    Object name=jComboBox.getSelectedItem();
                    con=DriverManager.getConnection(url);//,"system","manager"
            st=con.createStatement();

            rs= st.executeQuery("select AccountNo,Unpaid from Debit where Name='"+name+"'");
                     while(rs.next())
            {
                jTextField4.setText(rs.getString(1));
                                jTextField5.setText(rs.getString(2));
                                model.setRowCount(0);
                                model1.setRowCount(0);
                                b=0;
                                a=0;
                                tj="0";
                                tb="0";
                                t=0;
                                gt="0";
                                an="0";nam="0";mono="0";cit="0";

                                data=getJamaCustomer(name);
                                data1=getIdName(name);
        Object[] d3={data1.get(0).get(0),data1.get(0).get(1),data1.get(0).get(3),data1.get(0).get(4)};
        model1.addRow(d3);

        JTable table1=new JTable(data,header);
        for(int i=0;i<table1.getRowCount();i++){
        Object[] d={data.get(i).get(0),data.get(i).get(1),data.get(i).get(2),data.get(i).get(3),data.get(i).get(4)};
        model.addRow(d);

        }
        for(int i=0;i<table1.getRowCount();i++){
            a=a+Integer.parseInt(data.get(i).get(2));

            b=b+Integer.parseInt(data.get(i).get(3));
        }
        tj=Integer.toString(a);
        tb=Integer.toString(b);
        Object[] d1={"","Total",tj,tb,""};
        model.addRow(d1);



        t=Integer.parseInt(tb)-Integer.parseInt(tj);
        gt=Integer.toString(t);
        Object[] d2={"","Nett Balance","",gt,""};
        model.addRow(d2);

        table = new JTable(model);
        table3 = new JTable(model1);


        table.setRowHeight(30);
        table3.setRowHeight(30);
        JScrollPane scroll=new JScrollPane(table);
        JScrollPane scroll1=new JScrollPane(table3);
        tablePanel.add(scroll1);
        tablePanel.add(scroll);
                        }
            rs.close();
                        st.close();
                        con.close();
        }
        catch(Exception e)
        {
            System.out.println("GG"+e);
        }

        }

            private Vector<Vector<String>> getJamaCustomer(Object name) throws ClassNotFoundException, SQLException {
              Vector<Vector<String>> jamacustomerVector = new Vector<Vector<String>>();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/Program Files/Shop/shop.accdb";
                                password = "3064101991";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";"+
                            "Pwd="+password+";";
                    Connection conn = DriverManager.getConnection(url);//,"system","manager"

PreparedStatement pre = conn.prepareStatement("select * from Credit where Name='"+name+"' order by TID");
ResultSet rs = pre.executeQuery();//

while(rs.next())
{
Vector<String> jamacustomer = new Vector<String>();
jamacustomer.add(rs.getString(1)); //Empid
jamacustomer.add(rs.getString(4)); //Empid
jamacustomer.add(rs.getString(5)); //Empid
jamacustomer.add(rs.getString(6)); //Empid
jamacustomer.add(rs.getString(7)); //Empid
jamacustomerVector.add(jamacustomer);

}
/*Close the connection after use (MUST)*/
if(conn!=null)
conn.close();

return jamacustomerVector;
            }

            private Vector<Vector<String>> getIdName(Object name) throws ClassNotFoundException, SQLException {
Vector<Vector<String>> idnameVector = new Vector<Vector<String>>();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//String myDB ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=pay.MDB";
String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/Program Files/Shop/shop.accdb";
                                //userID = "Admin";
                                password = "3064101991";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";"+
                            "Pwd="+password+";";
                    Connection conn = DriverManager.getConnection(url);//,"system","manager"


//Connection conn = dbConnection();
PreparedStatement pre = conn.prepareStatement("select * from ManageCustomer where Name='"+name+"'");
ResultSet rs = pre.executeQuery();//

while(rs.next())
{
Vector<String> idname = new Vector<String>();
idname.add(rs.getString(1)); //Empid
idname.add(rs.getString(2)); //Empid
idname.add(rs.getString(3)); //Empid
idname.add(rs.getString(4)); //Empid
idname.add(rs.getString(5)); //Empid
idnameVector.add(idname);

}
/*Close the connection after use (MUST)*/
if(conn!=null)
conn.close();

return idnameVector;
//                throw new UnsupportedOperationException("Not yet implemented");
            }
        });
        JPanel p1=new JPanel();
        p1.add(jlab); 
        p1.add(jComboBox);
        addUnitPanel.add(p1);
        loadcombo2();

        addUnitPanel.setBorder(BorderFactory.createTitledBorder(title));
        return addUnitPanel;
    } 


    void loadcombo2()
    {
        try
        {
            Connection con=null;
            Statement st=null;
            ResultSet rs=null;
                       // ResultSet rs1=null;

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/Program Files/Shop/shop.accdb";
                                //userID = "Admin";
                                password = "3064101991";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";"+
                            "Pwd="+password+";";
                    //sql = "SELECT * FROM tblUserProfile";
                    con=DriverManager.getConnection(url);//,"system","manager"
                    //con=DriverManager.getConnection("jdbc:odbc:shop","system","manager"); 
            st=con.createStatement();

            rs= st.executeQuery("select distinct(Name) from ManageCustomer");
            //rs1=st.executeQuery("select Unit from AddUnit");
                        while(rs.next())
            {
                jComboBox.addItem(rs.getString(1));
                                //jComboBox1.addItem(rs1.getString(1));
            }
            rs.close();
                       // rs1.close();
                        st.close();
                        con.close();
        }
        catch(Exception e)
        {
            System.out.println("GG"+e);
        }

    }

    public void actionPerformed(ActionEvent ae){
        System.out.println(ae.getActionCommand());
if(ae.getSource()==update){
       int row = table.getSelectedRow();
if (row != -1) {
   row = table.convertRowIndexToModel(row);
   String value = (String) model.getValueAt(row, 0);
   System.out.println("value:"+value);
           }
            int n=JOptionPane.showConfirmDialog(mainPanel, "Would you like to update the record?", "Confirm", JOptionPane.YES_NO_OPTION);

    if (n == JOptionPane.YES_OPTION) {

        try{   


            Connection con=null;
            Statement st=null;
            ResultSet rs=null;
            ResultSet rs1=null;
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/Program Files/Shop/shop.accdb";
                                //userID = "Admin";
                                password = "3064101991";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";"+
                            "Pwd="+password+";";
                    //sql = "SELECT * FROM tblUserProfile";
                    con=DriverManager.getConnection(url);//,"system","manager"
                    //con=DriverManager.getConnection("jdbc:odbc:shop");
                    st=con.createStatement();
                    System.out.println("table.getValueAt(row,0)"+table.getValueAt(row, 0));
                    rs=st.executeQuery("select * from Credit where TID="+table.getValueAt(row, 0));
                    if(rs.next()){
                        s1=rs.getString(1);
                        s2=rs.getString(2);//accounno
                        s3=rs.getString(3);//name
                        s4=rs.getString(4);
                        s5=rs.getString(5);//paid
                        s6=rs.getString(6);//unpaid
                    }
                PreparedStatement smt=con.prepareStatement("select * from Debit where Name=?");
                smt.setObject(1, s3);
                rs=smt.executeQuery();
                if(rs.next()){
                    u1=rs.getString(1);
                    u2=rs.getString(2);
                    u3=rs.getString(3);
                    u4=rs.getString(4);
                    u5=rs.getString(5);
                    u6=rs.getString(6);//unpaid

                }

                st.executeUpdate("delete from Credit where TID="+table.getValueAt(row, 0));
                System.out.println("Deleted");
                st.executeUpdate("delete from Debit where Name='"+s3+"'");
                tid=Integer.parseInt(table.getValueAt(row,0).toString());
                //s2
                //s3
                date=table.getValueAt(row, 1).toString();
                jama=table.getValueAt(row, 2).toString();//s5
                baki=table.getValueAt(row, 3).toString();//s6
                nett=table.getValueAt(row, 4).toString();
                 rs1=st.executeQuery("select * from NettDate where Name='"+s3+"'");
                 while(rs1.next()){
                     nettdate=rs1.getString(2);
                     nettbal=rs1.getString(3);
                 }
                String tpaid=Integer.toString(Integer.parseInt(s5)-Integer.parseInt(jama));
                String tunpaid=Integer.toString(Integer.parseInt(s6)-Integer.parseInt(baki));

                if(u6.contains("-")){
                    System.out.println("Contains -");
                if(tpaid.contains("-")&tunpaid.contains("-")){
                  tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                 nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                  System.out.println("1-"+tupdate);
              }else if(!tpaid.contains("-")&tunpaid.contains("-")){
                tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                 nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                System.out.println("2-"+tupdate);}
              else if(tpaid.contains("-")&!tunpaid.contains("-")){
                    tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
               nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                System.out.println("3-"+tupdate);
                }else{
                    tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
               nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                System.out.println("4-"+tupdate);
                }
                }else{
                    System.out.println("Not Contains -");
                if(tpaid.contains("-")&tunpaid.contains("-")){
                  tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                 nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                  System.out.println("1"+tupdate);
              }else if(!tpaid.contains("-")&tunpaid.contains("-")){
                tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                 nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                System.out.println("2"+tupdate);}
              else if(tpaid.contains("-")&!tunpaid.contains("-")){
                    tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
               nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                System.out.println("3"+tupdate);
                }else{
                    tupdate=Integer.toString(Integer.parseInt(u6)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
               nettb=Integer.toString(Integer.parseInt(nettbal)+Integer.parseInt(tpaid)-Integer.parseInt(tunpaid));
                System.out.println("4"+tupdate);
                }    
                }
                int i=st.executeUpdate("insert into Credit values("+tid+",'"+s2+"','"+s3+"','"+date+"','"+jama+"','"+baki+"','"+nett+"')");
                st.executeUpdate("insert into Debit values('"+s2+"','"+s3+"','"+u3+"','"+u4+"','"+date+"','"+tupdate+"')");
                if(Integer.parseInt(table.getValueAt(row, 4).toString())==1){
                   st.executeUpdate("delete from NettDate where Name='"+s3+"'");

                    st.executeUpdate("insert into NettDate values('"+s3+"','"+date+"','"+nettb+"')");
                }
                if(i==0){
                    String msg="Record not updated Successfully";
                    String ss="Sorry..........";
                    int res=JOptionPane.PLAIN_MESSAGE;
                    JOptionPane.showMessageDialog((Component) null,msg,ss,res);
                }else{
                    String msg="Record updated Successfully";
                    String ss="Congratlations..........";
                    int res=JOptionPane.PLAIN_MESSAGE;
                    JOptionPane.showMessageDialog((Component) null,msg,ss,res);
                    //dispose();
                }

            rs=st.executeQuery("select TID,Date,Paid,Unpaid,Nett from Credit where Name='"+s3+"' order by Date");
            st.close();
            rs.close();
            rs1.close();
                con.close();

         } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ManageCustomer.class.getName()).log(Level.SEVERE, null, ex);
                }catch(Exception err){ 
                    System.out.println("GG"+err);
                }
    } else if (n == JOptionPane.NO_OPTION) {
        try {
            Connection con=null;
            Statement st=null;
            ResultSet rs=null;
                       // ResultSet rs1=null;

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url=null,userID=null,password=null;
                    String dbFileName=null;
                    String sql=null;

                    dbFileName = "C:/Program Files/Shop/shop.accdb";
                                //userID = "Admin";
                                password = "3064101991";
                    url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};"+
                            "DBQ="+dbFileName+";"+
                            "Pwd="+password+";";
                    //sql = "SELECT * FROM tblUserProfile";
                    con=DriverManager.getConnection(url);//,"system","manager"
                    st = con.createStatement();
            rs=st.executeQuery("select TID,Date,Paid,Unpaid,Nett from Credit where Name='"+s3+"' order by Date");
            table.setModel(buildTableModel(rs));
            con.close();
            //pstmt.close();
              JOptionPane.showMessageDialog(null,
                              "You choose not to update the data !");

        }catch (ClassNotFoundException err) {
            // System.out.println(e);
            err.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println(ex);
        }
    }
}   }

    public static void main(String args[]){
        JFrame frm=new JFrame("Customer Jama Baki");
        Credit b=new Credit(frm);
        //frm.setSize(650, 236);
        frm.setSize(650, 700);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setResizable(false);
        frm.setLocationRelativeTo(null);
        frm.show();
    }

}

3 个答案:

答案 0 :(得分:1)

看起来你的表中没有选择任何内容。您可以检查它是否真的被选中。如果不使用例如表中的第一行或只是忽略更新。

答案 1 :(得分:1)

int selection = table.getSelectedRow();    

if(selection == -1) {
    row = 0;
    }
    else row = selection;

根据以下评论,您可能希望向表中添加ListSelectionListener。

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new MySelectionListener(table) {
     @Override
      public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                 System.out.println(table.getSelectedRow());
            }
    }
};

答案 2 :(得分:-1)

我刚刚从jComboBox.addActionListener(new ActionListener(){ ... });

中删除了以下代码行

我正在做的是,将model添加到table两次,这是为什么它没有获得所选行。删除以下代码行后,它可以正常工作。

    table = new JTable(model);
    table3 = new JTable(model1);


    table.setRowHeight(30);
    table3.setRowHeight(30);
    JScrollPane scroll=new JScrollPane(table);
    JScrollPane scroll1=new JScrollPane(table3);
    tablePanel.add(scroll1);
    tablePanel.add(scroll);