search

Home  >  Q&A  >  body text

jdbc - About the error: java.sql.SQLException: ResultSet.next not called

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. Test code:

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

3. Error message:

开始尝试连接数据库!
连接成功
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)

After calling ResultSet.next(), the cursor moves to the next line. What about calling it multiple times like this?
I have really checked a lot of information. I hope the master will be merciful. I am really just a novice with a very weak understanding. Please don't report it.
If I could really find information to solve the problem, I wouldn't come here to ask! ! ! !

黄舟黄舟2731 days ago866

reply all(1)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-28 09:26:44

    Take Case 1 as an example

    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));
    }

    reply
    0
  • Cancelreply