fanyuzhou 2019-07-18 20:12 采纳率: 0%
浏览 393

Invalid value for getInt() - 'wc'

最近在学jdbc的时候遇到了Invalid value for getInt() - 'wc'这个异常。

     数据库里设计了两张表,一张学生表stu,有三个字段:id,name,sex,主键是id;另一张是班级表class,有三个字段class_id,name,score,主键是name,外键用stu表的name关联class表的name。

     问题是查找id为X的学生的成绩,将学生姓名和成绩放入集合中并遍历打印输出。

     运行的时候报错Invalid value for getInt() - 'wc',下面附上源码,求大神解决一下
    public class Demo {
        public static void main(String[] args) throws SQLException {
            HashMap<String, Integer> map = new HashMap<>();
            for (int i = 1; i <= 6; i++) {
                Student stu = StuTest.stuQuery(i);
                System.out.println(stu.toString());
                map.putAll(StuTest.stu_scoreQuary(stu.getName()));//这里报错
                }

            Set<Entry<String, Integer>> entrySet = map.entrySet();
            for (Entry<String, Integer> entry : entrySet) {
                System.out.println(entry);
            }
        }
    }
package com.stu.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class StuTest {
    public static Student stuQuery(int id) throws SQLException {
        Connection conn = StuJDBC.getConnection();

        String sql ="select * from stu where id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);

        ResultSet rs = pstmt.executeQuery();
        Student stu = new Student();
        while (rs.next()) {
            stu.setId(rs.getInt(1));
            stu.setName(rs.getString(2));
            stu.setSex(rs.getString(3));
        }
        StuJDBC.release(conn, pstmt, rs);
        return stu;
    }

    public static HashMap<String, Integer> stu_scoreQuary(String name) throws SQLException {
        Connection conn = StuJDBC.getConnection();

        String sql = "select * from class where name=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, name);

        ResultSet rs = pstmt.executeQuery();
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        while (rs.next()) {
            String stu = rs.getString(1);
            Integer score = rs.getInt(2); //报错信息指向了这一行
            map.put(stu, score);
        }
        return map;
    }
}

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-07-18 21:02
    关注

    Integer score = rs.getInt(2);
    你的第2列是不是int值,你用getString获取下看看内容是什么

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向