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获取下看看内容是什么

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算