查询语句:
if(f.getSource()==chaxun) //处理查询事件
{
String equipment1=equipmentselect2.getSelectedItem(); try
{
storesystem store1=new storesystem();
Connection con1=store1.getConnection();
Statement st1=con1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql="Select equipment AS 设备,date AS 日期,time AS 时间,voltage AS 电压,current0 AS 电流,state AS 运行状态,frequency AS 累计故障次数 FROM shuju WHERE equipment ='"+equipment1+"'";
ResultSet rs1=st1.executeQuery(sql);
rs1.last(); // 指到定位到最后一行
int row = rs1.getRow(); // 获得总行数
rs1.beforeFirst(); // 定位到开始
ResultSetMetaData rsmd1 = rs1.getMetaData(); // 计算字段个数
int col = rsmd1.getColumnCount(); // 计算字段个数
a = new Object[row][col];
for (int index = 0; rs1.next(); index++)
{
for (int j = 0; j < col; j++)
{
a[index][j] = rs1.getString(j + 1);
}
}
d=a;
table1=new JTable(a,columnNames);
table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane pane1=new JScrollPane(table1);
chaxunframe.remove(pane); //移去pane
pane=pane1;
pane1.setBounds(40,130,528,350);
chaxunframe.add(pane1);
rs1.close();
}
catch(Exception e){e.printStackTrace();}
}
删除语句:
if(f.getSource()==shanchu) //处理删除事件
{
storesystem store2=new storesystem();
Connection con2=store2.getConnection();
Statement st2=con2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
row =table1.getSelectedRow();
if (row>=0)
{
try {
b=d[row];
String sql1="DELETE * FROM shuju WHERE equipment='"+ b[0]+"' AND date='"+b[1]+"' AND time='"+b[2]+"' ";
st2.executeUpdate(sql1);
a[row]=null; table1.updateUI();
}
catch(Exception e){e.printStackTrace();}
}
else
{
JOptionPane.showConfirmDialog(null,"请选择需要删除的行","提示信息",JOptionPane.OK_CANCEL_OPTION);
}
}
加入删除模块后,编译报错为:未报告的异常错误sqlException;必须对其进行捕获或声明以便抛出。然后指向删除语句中的这一句:Statement st2=con2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
高手指教该如何更改?
if(f.getSource()==chaxun) //处理查询事件
{
String equipment1=equipmentselect2.getSelectedItem(); try
{
storesystem store1=new storesystem();
Connection con1=store1.getConnection();
Statement st1=con1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql="Select equipment AS 设备,date AS 日期,time AS 时间,voltage AS 电压,current0 AS 电流,state AS 运行状态,frequency AS 累计故障次数 FROM shuju WHERE equipment ='"+equipment1+"'";
ResultSet rs1=st1.executeQuery(sql);
rs1.last(); // 指到定位到最后一行
int row = rs1.getRow(); // 获得总行数
rs1.beforeFirst(); // 定位到开始
ResultSetMetaData rsmd1 = rs1.getMetaData(); // 计算字段个数
int col = rsmd1.getColumnCount(); // 计算字段个数
a = new Object[row][col];
for (int index = 0; rs1.next(); index++)
{
for (int j = 0; j < col; j++)
{
a[index][j] = rs1.getString(j + 1);
}
}
d=a;
table1=new JTable(a,columnNames);
table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane pane1=new JScrollPane(table1);
chaxunframe.remove(pane); //移去pane
pane=pane1;
pane1.setBounds(40,130,528,350);
chaxunframe.add(pane1);
rs1.close();
}
catch(Exception e){e.printStackTrace();}
}
删除语句:
if(f.getSource()==shanchu) //处理删除事件
{
storesystem store2=new storesystem();
Connection con2=store2.getConnection();
Statement st2=con2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
row =table1.getSelectedRow();
if (row>=0)
{
try {
b=d[row];
String sql1="DELETE * FROM shuju WHERE equipment='"+ b[0]+"' AND date='"+b[1]+"' AND time='"+b[2]+"' ";
st2.executeUpdate(sql1);
a[row]=null; table1.updateUI();
}
catch(Exception e){e.printStackTrace();}
}
else
{
JOptionPane.showConfirmDialog(null,"请选择需要删除的行","提示信息",JOptionPane.OK_CANCEL_OPTION);
}
}
加入删除模块后,编译报错为:未报告的异常错误sqlException;必须对其进行捕获或声明以便抛出。然后指向删除语句中的这一句:Statement st2=con2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
高手指教该如何更改?
建议你将Statement st2=con2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
改为
Statement st2=con2.createStatement();然后再看看程序有问题么
Statement st2;
try {
st2=con2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch (sqlException){
...
}
我看了你的代码,你对引发错误的判断本身可能就有问题,真正引发问题的可能并不是SQL语句的执行问题,而是你对JTable的删除行没有处理好。
在数据库删除对应数据之后,应该是在JTable的model调用removeRow删除对应的数据,而你采用的是将对应数据设置为null,这样table在绘制界面的时候会报错
public class tablemodel extends DefaultTableModel
{
private Object a[][];
public tablemodel(Object [][] a)
{
this.a=a;
}
@Override
public int getRowCount()
{
if (a==null) return 0;
return a.length;
}
}
后使用ftm.removeRow(row);
然后还是那样报错。。伤心。