大家帮我看看哈。急!
我点击“删除”按钮后怎么让表格中的相对应的数据删除掉啊,数据库里面的数据倒是删除了,但是java表格中没有反应。我也重新获取新表格了,肯定是我获取方法不对了,但是我不知道怎么修改,所以请大家帮帮看看下。我把代码贴出来。
主要是想点击“删除”后,表格中相对应的行就会删掉。
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
class Win extends JFrame implements ActionListener
{
JLabel lable = new JLabel("要删除的学生学号:");
JTextField field = new JTextField(10);
JButton bu = new JButton("删除");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
Connection con;
Statement stmt;
ResultSet rs,rc;
Object a[][];
//JButton bt_save;
int i=0;
Object header[]={"学生学号","学生姓名","外语","高数","java"};
Win()
{
this.add(p1,BorderLayout.NORTH);
this.add(p2,BorderLayout.CENTER);
this.add(p3,BorderLayout.SOUTH);
p1.add(lable);
p1.add(field);
p3.add(bu);
bu.addActionListener(this);
a=new Object[9][5];
try
{
Class.forName("java1.0.jdbc.odbc.JdbcOdbcDriver");
}
catch(
ClassNotFoundException e)
{
System.out.println("Error:"+e);
}
try{
con=DriverManager.getConnection("jdbc:odbc:java1.0","","MICROSOF-CCBF6F");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from 学生成绩表");
while(rs.next())
{
a[i][0]=rs.getInt(1);
a[i][1]=rs.getString(2);
a[i][2]=rs.getString(3);
a[i][3]=rs.getInt(4);
a[i][4]=rs.getString(5);
i++;
}
}
catch(
SQLException e)
{
System.out.println("SqlError:"+e);
}
JTable table=new JTable(a,header);
Container con=getContentPane();
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
setBounds(500, 100, 400, 300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
validate();
}
public static void main(String args[])
{
new Win();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == bu){
try
{
Class.forName("java1.0.jdbc.odbc.JdbcOdbcDriver");
}
catch(Class
NotFoundException e1)
{
System.out.println("Error:"+e1);
}
try{
con=DriverManager.getConnection("jdbc:odbc:java1.0","","MICROSOF-CCBF6F");
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String delPei = "DELETE FROM 学生成绩表 WHERE 学生学号 = '"+field.getText()+"'";
stmt.executeUpdate(delPei);
field.setText("");
//table.removeAll();
rc = stmt.executeQuery("select * from 学生成绩表");
while(rc.next())
{
a[i][0]=rc.getInt(1);
a[i][1]=rc.getString(2);
a[i][2]=rc.getString(3);
a[i][3]=rc.getInt(4);
a[i][4]=rc.getString(5);
i++;
}
}
catch(SQLException e1)
{
System.out.println("SqlError:"+e1);
}
JTable table = new JTable(a,header);
Container con = getContentPane();
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);