【问题标题】:How to read data column wise using jdbc如何使用 jdbc 明智地读取数据列
【发布时间】:2015-05-28 09:00:59
【问题描述】:

我正在使用表格从数据库中获取数据,但我的表格包含数据列明智。我想使用结果集从表格中明智地检索数据列。但每次我得到“列索引超出范围错误。”这是我用来从表中检索数据的代码

这是我的选择声明

 String consultantRatio = " select count(distinct CANDYS.candidateId) from "
                     +" (Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J " 
                     +" where C.noteid=J.noteid and C.action like 'Engaged' " 
                     +" and C.dateadded between "+ "\""+ new java.sql.Timestamp(startDate.getTime())+ "\" and "+ "\""+ new java.sql.Timestamp(endDate.getTime())+ "\"  ) N,"
                     +" (Select distinct Note.candidateId, JNote.jobOrderID " 
                     +" from db_candidatenote Note,db_jobordernote JNote, corporateuser User where " 
                     +" Note.noteid=JNote.noteid "
                     +" and Note.commentingPersonID = User.userid " 
                     +" and User.name= "+"\""+consultantNameList.get(i)+"\""
                     +" and Note.action='Sourcing') CANDYS "
                     +" where N.candidateID= CANDYS.candidateId "
                     +" and N.joborderid=CANDYS.jobOrderID "                

                     +" union"

                     +" select count(distinct CANDYS.candidateId) from "
                     +" (Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J " 
                     +" where C.noteid=J.noteid and C.action like 'Qualification' " 
                     +" and C.dateadded between "+ "\""+ new java.sql.Timestamp(startDate.getTime())+ "\" and "+ "\""+ new java.sql.Timestamp(endDate.getTime())+ "\"  ) N,"
                     +" (Select distinct Note.candidateId, JNote.jobOrderID " 
                     +" from db_candidatenote Note,db_jobordernote JNote, corporateuser User where " 
                     +" Note.noteid=JNote.noteid "
                     +" and Note.commentingPersonID = User.userid " 
                     +" and User.name= "+"\""+consultantNameList.get(i)+"\""
                     +" and Note.action='Sourcing') CANDYS "
                     +" where N.candidateID= CANDYS.candidateId "
                     +" and N.joborderid=CANDYS.jobOrderID "

                     +" union "

                     +" select count(distinct CANDYS.candidateId) from "
                     +" (Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J " 
                     +" where C.noteid=J.noteid and C.action like 'Internal Submission' "
                     +" and C.dateadded between "+ "\""+ new java.sql.Timestamp(startDate.getTime())+ "\" and "+ "\""+ new java.sql.Timestamp(endDate.getTime())+ "\"  ) N,"
                     +" (Select distinct Note.candidateId, JNote.jobOrderID " 
                     +" from db_candidatenote Note,db_jobordernote JNote, corporateuser User where " 
                     +" Note.noteid=JNote.noteid "
                     +" and Note.commentingPersonID = User.userid " 
                     +" and User.name= "+"\""+consultantNameList.get(i)+"\""
                     +" and Note.action='Sourcing') CANDYS "
                     +" where N.candidateID= CANDYS.candidateId "
                     +" and N.joborderid=CANDYS.jobOrderID "

                     +" union "

                    +" select count(distinct CANDYS.candidateId) from sendout S,"
                    +" (Select distinct Note.candidateId, JNote.jobOrderID " 
                    +" from db_candidatenote Note,db_jobordernote JNote, corporateuser User where " 
                    +" Note.noteid=JNote.noteid "
                    +" and Note.commentingPersonID = User.userid " 
                    +" and User.name= "+"\""+consultantNameList.get(i)+"\""
                    +" and Note.action='Sourcing' ) CANDYS "
                    +" where S.candidateID= CANDYS.candidateId "
                    +" and S.joborderid=CANDYS.jobOrderID "
                    +" and S.dateadded between "+ "\""+ new java.sql.Timestamp(startDate.getTime())+ "\" and "+ "\""+ new java.sql.Timestamp(endDate.getTime())+ "\" "

                    +" union"

                    +"  select count(distinct CANDYS.candidateId) from "
                    +"  (Select C.candidateID,J.joborderID from db_candidatenote C, db_jobordernote J " 
                    +"  where C.noteid=J.noteid and C.action like '%Interview%' " 
                    +"  and C.dateadded between "+ "\""+ new java.sql.Timestamp(startDate.getTime())+ "\" and "+ "\""+ new java.sql.Timestamp(endDate.getTime())+ "\"  ) N,"
                    +"  (Select distinct Note.candidateId, JNote.jobOrderID " 
                    +"  from db_candidatenote Note,db_jobordernote JNote, corporateuser User where " 
                    +"  Note.noteid=JNote.noteid "
                    +"  and Note.commentingPersonID = User.userid " 
                    +"  and User.name= "+"\""+consultantNameList.get(i)+"\""
                    +"  and Note.action='Sourcing') CANDYS "
                    +"  where N.candidateID= CANDYS.candidateId "
                    +"  and N.joborderid=CANDYS.jobOrderID "

                    +"  union "

                    +"  select count(distinct CANDYS.candidateId ) from placement P,(Select distinct Note.candidateId, JNote.jobOrderID " 
                    +"  from db_candidatenote Note,db_jobordernote JNote, corporateuser User where " 
                    +"  Note.noteid=JNote.noteid "
                    +"  and Note.commentingPersonID = User.userid " 
                    +"  and User.name= "+"\""+consultantNameList.get(i)+"\""
                    +"  and Note.action='Sourcing') CANDYS "
                    +"  where P.candidateID= CANDYS.candidateId "
                    +"  and P.joborderid=CANDYS.jobOrderID "
                    +"  and P.dateadded between "+ "\""+ new java.sql.Timestamp(startDate.getTime())+ "\" and "+ "\""+ new java.sql.Timestamp(endDate.getTime())+ "\" " ;



                 while (rsServeResource1.next()) {                          

                    // these variables will be divided by actual milestone numbers like source to engage / engage etc within that time range ..

                    if(rsServeResource1.getInt(1)!=0){
                    sourceToEngage = rsServeResource1.getInt(1);
                    }if(rsServeResource1.getInt(2)!=0){
                    sourceToQualification =rsServeResource1.getInt(2);
                    }if(rsServeResource1.getInt(3)!=0){
                    sourceToIs = rsServeResource1.getInt(3);
                    } if(rsServeResource1.getInt(4)!=0){
                    sourceToPresent =rsServeResource1.getInt(4);
                    } if(rsServeResource1.getInt(5)!=0){
                    sourceToInterview = rsServeResource1.getInt(5);
                    } if(rsServeResource1.getInt(6)!=0){
                    sourceToPlacament = rsServeResource1.getInt(6);
                }

每次我运行代码时都会出现以下异常。

Column Index out of range, 2 > 1. 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.checkColumnBounds(ResultSetImpl.java:830)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2714)
at com.msh.servlet.MileStoneMonthlyJob.execute(MileStoneMonthlyJob.java:530)
at org.quartz.core.JobRunShell.run(JobRunShell.java:223)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)

完成

所以问题出在哪里,我无法让它工作。请有人帮忙。

【问题讨论】:

  • select 语句是什么样的?表中有多少列?
  • @MadProgrammer 看到我更新了我的问题
  • @MadProgrammer 你能帮忙吗??请
  • 嗯,基本上,错误说明了一切,结果集中只有 on 列,我建议问题出在查询中
  • @MadProgrammer 但查询在运行时给出了很好的结果

标签: java mysql jdbc


【解决方案1】:

Resultset 中只有一列...因此,如果在没有列的情况下尝试指向下一列,则会抛出 IndexOutOfRangeException。

但对于进一步的陈述,以下内容可能对您有用:

您可以尝试从元数据中读取 Columncount,例如:

rsServeResource1.getMetaData().getColumnCount();

然后循环抛出你的行。应该是这样的:

int[] colcontent = new int[6];
while (rsServeResource1.next()) {
 int count = rsServeResource1.getMetaData().getColumnCount();

 for (int i = 0; i <= count; i++){

  colcontent[i] = rs.getInt(1);

 }
}

sourceToEngage = colcontent(0);
sourceToQualification =colcontent(1);
sourceToIs = colcontent(2);
sourceToPresent =colcontent(3);
sourceToInterview = colcontent(4);
sourceToPlacament = colcontent(5);

猜猜现在应该正确填写属性了吗?

【讨论】:

  • 不,它仍然显示相同的异常,问题出在 }if(rsServeResource1.getInt(2)!=0){
  • 请注意我没有自己尝试,只是在运行中写的。 Mybe 需要进行一些更改。事实上,如果没有,你仍然不能指向下一列。
  • 这个问题有一些非常好的解决方案,您可以使用stackoverflow.com/questions/696782/…
  • 代码中的缺陷是您硬编码了 rsServeResource1.getInt(N),其中 N 硬编码为 1、2、3、4、5 和 6。您的结果集只有一列返回不是 6。是什么让您认为结果集中总是返回 6 列?我粘贴的那个链接中的答案有一些很好的方法来编码访问任何结果集返回的所有行中的所有列。我建议在您的代码中尝试其中一些,以便您自己了解它们是如何工作的
  • @Hector i 这个应用程序我将永远拥有 6 个
【解决方案2】:

您的数据以行的形式出现,并且只有一列。所以我们应该在结果集中使用索引总是1。没有规定在结果集中旋转数据

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2021-03-15
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多