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

求大佬帮帮忙!!!谢谢

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条)

报告相同问题?

悬赏问题

  • ¥15 #求带 python,使用pycharm爬取智联招聘数据资源并保存为Excel文件,同时以图表形式呈现
  • ¥15 学校优化算法怎么样,国内用的多吗?
  • ¥15 微信购物商城购物车的所有功能以及购物车,和首页的搜索功能
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位