问题描述
- 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,列值,你看看姓名是哪一列;
还有,建议你用编号做判断,你这整个代码一行注释都没有,醉了。有空学学怎么调试吧,很简单的,你设个断点出错的位置,很好排查的
解决方案二:
关于java中Jtable出现的一些问题
解决方案三:
看看aaaa类的代码
解决方案四:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.table.*;
class aaaa extends AbstractTableModel {
Vector ziduan,jilu;
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
public int getRowCount()
{
return this.jilu.size();
}
public int getColumnCount()
{
return this.ziduan.size();
}
public Object getValueAt(int hang, int lie)
{
return ((Vector)this.jilu.get(hang)).get(lie);
}
public String getColumnName(int e)
{
return (String)this.ziduan.get(e);
}
public aaaa(){
bb("select * from QQ");
}
public aaaa(String sql){
bb(sql);
}
public void bb(String a){
ziduan=new Vector();
ziduan.add("编号");
ziduan.add("姓名");
ziduan.add("性别");
ziduan.add("年龄");
ziduan.add("手机号");
ziduan.add("QQ号");
ziduan.add("名片");
ziduan.add("会员");
ziduan.add("签名");
ziduan.add("天数");
jilu=new Vector();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ct=DriverManager.getConnection("jdbc:odbc:SQL Sever","sa","923274995");
ps=ct.prepareStatement(a);
rs=ps.executeQuery();
while(rs.next())
{
Vector hang=new Vector();
hang.add(rs.getInt(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getInt(4));
hang.add(rs.getLong(5));
hang.add(rs.getLong(6));
hang.add(rs.getString(7));
hang.add(rs.getString(8));
hang.add(rs.getString(9));
hang.add(rs.getInt(10));
jilu.add(hang);
}
} catch (Exception e){}
finally
{
try {
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}
} catch (Exception e){}
}
}
}