jdbc - 關於錯誤:java.sql.SQLException: 未呼叫 ResultSet.next的問題
黄舟
黄舟 2017-06-28 09:24:44
0
1
870

1.` public Res evaluateCourse(@FormParam("stuId") int stuId, @FormParam("score") String score) {

        Res res = new Res();
        Connection con = null;
        ResultSet rs = null;
        String[] temp =  score.split(",");
        int count = 0;
        try{
            con = getConnection();
            Statement sm = con.createStatement();
            String selectStuId = "select evaluateId from evaluate where stuId = " + stuId;
            rs = sm.executeQuery(selectStuId);

            for(String s: temp) {
                int scoreInt = Integer.parseInt(s);
                count++;
                switch (count) {
                    case 1:
                        int TEACHPROGRAM = scoreInt;
                        System.out.println("TEACHPROGRAM = " + TEACHPROGRAM);
                        String insert1 = "insert into evaluateDesc(EVALUATEID,ATTRNAME,ATTRSCORE) values(" + rs.getInt("evaluateId")
                                + "," + TEACHPROGRAM + ")";
                        System.out.println(insert1);
                        rs = sm.executeQuery(insert1);
                        if(rs.next()){
                            System.out.println("TEACHPROGRAM = " + rs.getInt(TEACHPROGRAM));
                        }
                        continue;
                    case 2:
                        int LECTURECONTENT = scoreInt;
                        System.out.println("LECTURECONTENT = " + LECTURECONTENT);
                        String insert2 = "insert into evaluateDesc(evaluateId,attrName,attrScore) values (" + rs.getInt("evaluate")
                                + "," + LECTURECONTENT + ")";
                        rs = sm.executeQuery(insert2);
                        if(rs.next()){
                            System.out.println("LECTURECONTENT = " + rs.getInt(LECTURECONTENT));
                        }
                        continue;
                    case 3:
                        int LECTUREMETHOD = scoreInt;
                        System.out.println("LECTUREMETHOD = " + LECTUREMETHOD);
                        String insert3 = "insert into evaluateDesc(evaluateId,attrName,attrScore) values (" + rs.getInt("evaluate")
                                + "," + LECTUREMETHOD + ")";
                        rs = sm.executeQuery(insert3);
                        if(rs.next()){
                            System.out.println("LECTUREMETHOD = " + rs.getInt(LECTUREMETHOD));
                        }
                        continue;
                    case 4:
                        int TSINTERACTION = scoreInt;
                        System.out.println("TSINTERACTION = " + TSINTERACTION);
                        String insert4 = "insert into evaluateDesc(evaluateId,attrName,attrScore) values (" + rs.getInt("evaluate")
                                + "," + TSINTERACTION + ")";
                        rs = sm.executeQuery(insert4);
                        if(rs.next()){
                            System.out.println("TSINTERACTION = " + rs.getInt(TSINTERACTION));
                        }
                        continue;
                    case 5:
                        int EVAMETHOD = scoreInt;
                        System.out.println("EVAMETHOD = " + EVAMETHOD);
                        String insert5 = "insert into evaluateDesc(evaluateId,attrName,attrScore) values (" + rs.getInt("evaluate")
                                + "," + EVAMETHOD + ")";
                        rs = sm.executeQuery(insert5);
                        if(rs.next()){
                            System.out.println("EVAMETHOD = " + rs.getInt(EVAMETHOD));
                        }
                        continue;
                    case 6:
                        int EVALUATE = scoreInt;
                        System.out.println("EVALUATE = " + EVALUATE);
                        String insert6 = "insert into evaluateDesc(evaluateId,attrName,attrScore) values (" + rs.getInt("evaluate")
                                + "," + EVALUATE + ")";
                        rs = sm.executeQuery(insert6);
                        if(rs.next()){
                            System.out.println("EVALUATE = " + rs.getInt(EVALUATE));
                        }
                        break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        res.setMsg("评教成功");
        return res;
    }
   `
   

2.測試程式碼:

    void testEvaluateCourse() {
        CourseEvaluateServiceImpl ce = new CourseEvaluateServiceImpl();
        String str = "15,15,10,0,0,0";
        ce.evaluateCourse(1,str.toString());
    }

3.錯誤訊息:

开始尝试连接数据库!
连接成功
TEACHPROGRAM = 15

java.sql.SQLException: 未调用 ResultSet.next
    at oracle.jdbc.driver.GeneratedScrollableResultSet.getInt(GeneratedScrollableResultSet.java:565)
    at oracle.jdbc.driver.GeneratedResultSet.getInt(GeneratedResultSet.java:1350)
    at serviceImpl.CourseEvaluateServiceImpl.evaluateCourse(CourseEvaluateServiceImpl.java:45)
    at service.CourseEvaluateService$evaluateCourse.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at serviceImpl.CourseEvaluateServiceImplTest.testEvaluateCourse(CourseEvaluateServiceImplTest.groovy:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.textui.TestRunner.doRun(TestRunner.java:116)
    at com.intellij.junit3.JUnit3IdeaTestRunner.doRun(JUnit3IdeaTestRunner.java:113)
    at junit.textui.TestRunner.doRun(TestRunner.java:109)
    at com.intellij.junit3.JUnit3IdeaTestRunner.startRunnerWithArgs(JUnit3IdeaTestRunner.java:45)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

呼叫ResultSet.next()之後遊標會移動到下一行,那麼像這樣多次呼叫呢?
我真的查了很多資料了,希望大神手下留情,我真的只是個理解能力特別薄弱的小白,別舉報了。
如果我真的能查到資料解決問題,我是不會來這裡問的! ! ! !

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回覆(1)
扔个三星炸死你

以Case 1 為例

rs = sm.executeQuery(selectStuId);

//执行下面的rs.getInt("evaluateId"),之前没有调用过next,会出错
String insert1 = "insert into evaluateDesc(EVALUATEID,ATTRNAME,ATTRSCORE) values(" + rs.getInt("evaluateId")+ "," + TEACHPROGRAM + ")";

rs = sm.executeQuery(insert1);
//这是对的
if(rs.next()){
    System.out.println("TEACHPROGRAM = " + rs.getInt(TEACHPROGRAM));
}
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板