最近在学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;
}
}