程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> jtable-java中JTable的問題,求解,大神在哪裡

jtable-java中JTable的問題,求解,大神在哪裡

編輯:編程解疑
java中JTable的問題,求解,大神在哪裡

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class a160407 extends JFrame implements ActionListener{
JButton anl1,anl2,anl3,anl4;
JLabel bq;
JTextField wbk;
JPanel mb1,mb2;
JTable bg;
JScrollPane gd;
aaaa aa;
public static void main(String[] args){
a160407 aaa=new a160407();
}
public a160407(){

    anl1=new JButton("查詢");
    anl1.addActionListener(this);
    anl1.setActionCommand("chaxun");
    anl2=new JButton("添加");
    anl2.addActionListener(this);
    anl2.setActionCommand("tianjia");
    anl3=new JButton("刪除");
    anl3.addActionListener(this);
    anl3.setActionCommand("shanchu");
    anl4=new JButton("修改");
    anl4.addActionListener(this);
    anl4.setActionCommand("xiugai");
    bq=new JLabel("請輸入姓名");
    wbk=new JTextField(7);
    mb1=new JPanel();
    mb2=new JPanel();
    aa=new aaaa();  
    bg=new JTable(aa);
    gd=new JScrollPane(bg);

    mb1.setLayout(new FlowLayout());mb1.add(bq);mb1.add(wbk);mb1.add(anl1);

    mb2.setLayout(new FlowLayout());mb2.add(anl2);mb2.add(anl3);mb2.add(anl4);

    this.add(mb1,BorderLayout.NORTH);
    this.add(mb2,BorderLayout.SOUTH);
    this.add(gd);

    this.setResizable(false);
    this.setSize(800,600);
    this.setLocation(300,150);
    this.setTitle("QQ聯系人");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);      
}
public void actionPerformed(ActionEvent e){
    if(e.getActionCommand().equals("chaxun")){
        String xingming=this.wbk.getText().trim();
        String sql="select * from QQ where xinmin='"+xingming+"'";
        aa=new aaaa(sql);
        bg.setModel(aa);
    }
    else if(e.getActionCommand().equals("tianjia")){
        tianjia tj=new tianjia(this,"信息添加",true);
        aa=new aaaa();
        bg.setModel(aa);
        System.out.println("fffffffffffffff");
    }
    else if(e.getActionCommand().equals("shanchu")){

        int ii=this.bg.getSelectedRow();
        if(ii==-1){

            JOptionPane.showMessageDialog(this,"請選中要刪除的行");
            return;
        }   程序總是運行到下面的這句話出錯????????????????????
            String st=(String)aa.getValueAt(ii,0);我發現getValueAt(ii,0)中的0改成1或者其他不是0的數程序就正常了,請好心的大神把我解決一下!
            PreparedStatement ps=null;
            Connection ct=null;
            ResultSet rs=null;
            try{
                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
                 ct=DriverManager.getConnection("jdbc:odbc:SQL Sever","sa","923274995");    
                 ps=ct.prepareStatement("delete  from QQ where xinmin=?");
                 ps.setString(1,st);
                 ps.executeUpdate();
            }catch(Exception e2){}
            finally{
                try {
                    if(rs!=null)
                    {
                        rs.close();
                    }
                    if(ps!=null)
                    {
                        ps.close();
                    }
                    if(ct!=null)
                    {
                        ct.close();
                    }

                } catch (Exception e3){}

            }aa=new aaaa();
            bg.setModel(aa);


    }
    else if(e.getActionCommand().equals("xiugai")){
        int ii=this.bg.getSelectedRow();
        if(ii==-1){ 
            JOptionPane.showMessageDialog(this,"請選中要修改的行");
            return;
        }   
        new xiugai(this,"信息修改",true,aa,ii);

    }
}

}
要解決的問題在上面代碼一排問號??????的下面

最佳回答:


你這個 刪除記錄的sql判斷條件是xinmin,然後你getValueAt就應該是獲取xinmin,getValueAt中後面一個參數是columnIndex,列值,你看看姓名是哪一列;
還有,建議你用編號做判斷,你這整個代碼一行注釋都沒有,醉了。有空學學怎麼調試吧,很簡單的,你設個斷點出錯的位置,很好排查的

qq_34316891
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved