反方向头发一幅图
2021-05-31 16:56
采纳率: 75%
浏览 33

求大佬帮帮忙!!!谢谢

package edu.cn.kluniv.sjz.sis.model;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

public class ResultSetTableModel extends AbstractTableModel {
	
	private ResultSet rs;
	private ResultSetMetaData rsmd;
	
	public ResultSetTableModel(ResultSet rs) {
		this.rs = rs;
		try {
			//获取传入结果集的元数据
			rsmd = rs.getMetaData();
		}catch (SQLException e) {
			System.out.println("ResultSetMetaData:rs.getMetaData 失败!!");
			e.printStackTrace();
		}
	}
	
	public ResultSet getResultSet() {
		return rs;
	}
	
	public String getColumnName(int columnIndex)
	{
		try {
			return rsmd.getColumnName(columnIndex+1);
		}catch(SQLException e) {
			System.out.println("ResultSetTableModel:rsmd.getColumnName失败!!");
		}
		return null;
	}
	
	public int getRowCount() {
		try {
			//将结果集中的游标移动到末尾,目的是使用后可以调用rs.getrow()方法获取结果集行数
			//用于生成ResultSetTableModel的行
			rs.last();
			int rowCount = rs.getRow();
			//将结果集中的游标移动到初始化的位置
			rs.beforeFirst();
			return rowCount;
			
		}catch(SQLException e) {
			System.out.println("ResultSetTableModel:rsmd.getRowCount失败!!");
			e.printStackTrace();
		}
		return 0;
	}
	
	public int getColumnCount() {
		try {
			return rsmd.getColumnCount();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public void setValueAt(Object objValue,int rowIndex,int columnIndex) {
		try {
			//将游标移动到结果集中相对应的位置
			rs.absolute(rowIndex+1);
			//修改对应的值
			rs.updateObject(columnIndex+1,objValue);
			//提交修改
			rs.updateRow();
			//触发单元格修改事件
			fireTableCellUpdated(rowIndex,columnIndex);
			
		}catch(SQLException e) {
			System.out.println("ResultSetTableModel:rs.setValueAt失败!!");
			e.printStackTrace();
		}
	}
	public Object getValueAt(int rowIndex,int columnIndex) {
		try {
			//
			rs.absolute(rowIndex+1);
			return rs.getObject(columnIndex+1);
		}catch(SQLException e) {
			System.out.println("ResultSetTableModel:rs.getValueAt获取失败!!");
			e.printStackTrace();
		}
		return null;
	}
	
	//复写isCellEditable,让结果集中的每个单元格都可编辑
	public boolean isCellEditable(int rowIndex,int columnIndex) {
		try {
			//判断结果集是否是可更新的
			if(rs.getConcurrency()==ResultSet.CONCUR_UPDATABLE) {
				return true;
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public void deleteRow(int rowIndex) {
		try {
			rs.absolute(rowIndex+1);
			rs.deleteRow();
			fireTableRowsDeleted(rowIndex,rowIndex);
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void insertRowStudent() {
		int rowInserted = getRowCount()+1;
		try {
			rs.moveToInsertRow();
			rs.updateString("sno", "001");
			rs.updateString("sname", "待插入");
			rs.updateString("ssex", "男");
			rs.updateInt("sage",23);
			rs.updateString("sdept", "信息技术");
			rs.insertRow();
			fireTableRowsInserted(rowInserted,rowInserted);
		}catch(SQLException e) {
			System.out.println("ResultSetTableModel.insertRow插入数据失败!!");
			e.printStackTrace();
		}
	}
	public void insertRowTeacher() {
		int rowInserted = getRowCount()+1;
		try {
			rs.moveToInsertRow();
			rs.updateString("tno", "001");
			rs.updateString("tname", "待插入");
			rs.updateString("tsex", "男");
			rs.updateInt("tage",23);
			rs.updateString("tdept", "信息技术");
			rs.insertRow();
			fireTableRowsInserted(rowInserted,rowInserted);
		}catch(SQLException e) {
			System.out.println("ResultSetTableModel.insertRow插入数据失败!!");
			e.printStackTrace();
		}
	}
	
	public void insertRowCourse() {
		int rowInserted = getRowCount()+1;
		try {
			rs.moveToInsertRow();
			rs.updateString("cno", "001");
			rs.updateString("cname", "待插入");
			rs.updateString("tno", "002");
			rs.updateString("cpno","1");
			rs.updateInt("ccredit", 3);
			rs.insertRow();
			fireTableRowsInserted(rowInserted,rowInserted);
		}catch(SQLException e) {
			System.out.println("ResultSetTableModel.insertRow插入数据失败!!");
			e.printStackTrace();
		}
		
	}
}

报错如下

ResultSetTableModel:rs.getValueAt获取失败!!
com.microsoft.sqlserver.jdbc.SQLServerException: 无法从已删除行获取值。
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyCurrentRowIsNotDeleted(SQLServerResultSet.java:502)
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(SQLServerResultSet.java:2048)
	at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getObject(SQLServerResultSet.java:2356)
	at edu.cn.kluniv.sjz.sis.model.ResultSetTableModel.getValueAt(ResultSetTableModel.java:86)
	at javax.swing.JTable.getValueAt(Unknown Source)
	at javax.swing.JTable.prepareRenderer(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
	at javax.swing.plaf.ComponentUI.update(Unknown Source)
	at javax.swing.JComponent.paintComponent(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent._paintImmediately(Unknown Source)
	at javax.swing.JComponent.paintImmediately(Unknown Source)
	at javax.swing.RepaintManager$4.run(Unknown Source)
	at javax.swing.RepaintManager$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.access$1200(Unknown Source)
	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 已采纳

    connection.prepareStatement(sql,ResultSet.Type_Scroll_sensitive,ResultSet.Concur_read_only)

    你的连接是不是这样的

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题