Java 如何使用jdbc按列读取数据
我使用一个表从数据库获取数据,但我的表保留数据列。我想使用resultset从表中检索数据列。但每次我获取“列索引超出范围错误”。这是我用于从表中检索数据的代码 这是我的选择Java 如何使用jdbc按列读取数据,java,mysql,jdbc,Java,Mysql,Jdbc,我使用一个表从数据库获取数据,但我的表保留数据列。我想使用resultset从表中检索数据列。但每次我获取“列索引超出范围错误”。这是我用于从表中检索数据的代码 这是我的选择 String consultantRatio = " select count(distinct CANDYS.candidateId) from " +" (Select C.candidateID,J.joborderID from db_candidatenote C, d
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 但对于进一步的陈述,以下内容可能对您有用: 您可以尝试从元数据中读取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);
int[]colcontent=newint[6];
while(rsserversource1.next()){
int count=rsServerResource1.getMetaData().getColumnCount();
对于(int i=0;i您的数据是以行的形式出现的,并且只有一列。因此,我们应该在resultset中始终使用索引1。没有在resultset中旋转数据的规定。select语句的外观是什么?表中有多少列?@MadProgrammer我已经更新了我的question@MadProgrammer你能帮我吗?拜托,基本上或者说,结果集中只有on列,我建议问题出在query@MadProgrammer但是当运行no时,查询给出了很好的结果,它仍然显示相同的异常,问题在第}if(rsServeResource1.getInt(2)!=0)行{请注意,我没有亲自尝试,只是在运行中编写。Mybe需要进行一些更改。事实上,如果没有更改,您仍然无法指向下一列。这个问题有一些非常好的解决方案,您可以使用。代码中的缺陷是您硬编码了rsServerSource1.getInt(N),其中N硬编码为1、2、3、4、5和6。您的结果集只有一列返回,而不是6。是什么让您认为您的结果集中始终会有6列返回?我粘贴的链接中的答案有一些很好的方法,可以对任何结果集返回的所有行的所有列进行编码访问。我建议尝试一些方法在你的代码中,你可以看到他们是如何work@Hector我在这个应用程序中,我将始终有6个