java - 如何使用jdbc按列读取数据

标签 java mysql jdbc

enter image description here我正在使用表从数据库获取数据,但我的表按列保存数据。我想使用结果集从表中按列检索数据。但每次我收到“列索引超出范围错误”。这是我用来从表中检索数据的代码

这是我的选择语句

 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)

完成

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

最佳答案

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

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

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

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

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

关于java - 如何使用jdbc按列读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30501844/

相关文章:

java - JLine,如何在 Windows 中获取完整文件名

java - Mysql流式结果集和jOOQ fetchLazy

java - 用sql查找圆内的点

java - GPS 服务器连接中使用的最佳协议(protocol)是什么?

java - Collection.contains 产生错误的结果

java - SQL异常 : ERROR: syntax error at or near "\"

hibernate - hibernate 缓存保存了什么?

php - 将数据从mysql导出到excel并通过android下载

python - 在共享服务器 : No module named MySQLdb? 上安装 Django

python - 无法使用 django 框架连接 mysql 数据库