I have 3 database tables matieres_demandees(idmatiere,name,ID_pre...), courses_history(IDetudiant , ID_matiere,note...)and no_pre(id matiere , name...).
In matieres_demandees , some recordes have many ID_pre and others only one. In case of one ID_pre , I have to join it with ID_matiere in courses_history table (matieres_demandees.ID_pre=courses_history.ID_matiere) and see if note of ID_pre is below 40 , in this case , I have to insert name and id_matiere in no_pre table.
In case of the presence of multiple ID_pre separated by "/"like(ID1/ID2...) in records,I have to split on "/" and then join each ID_pre with courses_history.ID_matiere (matieres_demandees.ID_pre=courses_history.ID_matiere)and see if the note of ID_pre is below 40. If at least one of the multiple IDs have a note <40 then I have to insert name and id matiere into no_pre.
In other words , I have multiple records , for each one I have to see the note(s) of ID_pre (s) , if one note is <40 the I have to do Insert into ...
I am working on this code but I have this error (SEVERE: null java.sql.SQLException: Before start of result set)
please help
cn3.R = cn3.sat.executeQuery("select distinct matieres_demandees.ID_pre,matieres_demandees.ID_matiere,matieres_demandees.name
from matieres_demandees,courses_history
where courses_history.ID_etudiant="+id+" and exists
(select matieres_demandees.ID_pre from matieres_demandees)");
int n=1;
while (cn3.R.next()) {
String name = cn3.R.getString("matieres_demandees.name");
String idmatiere = cn3.R.getString("matieres_demandees.ID_matiere");
String idpre = cn3.R.getString("matieres_demandees.ID_pre");
if (idpre.contains("/")) {
String[] pre2 = idpre.split("/");
for (int i = 0; i < pre2.length; i++) {
System.out.println(pre2[i]);
System.out.println("here");
int nat = 0;
cn3.R = cn3.sat.executeQuery("select courses_history.note
from matieres_demandees,courses_history
where courses_history.ID_matiere='"+pre2[i]+"' and courses_history.ID_etudiant="+id);
int ab=cn3.R.getInt("courses_history.note");
if (ab < 40) {
nat = nat + 1;
} else {
nat = nat + 0;
}
if(nat >= 1) {
cn3.sat.executeUpdate("insert into no_pre "+name+","+idmatiere);
}
}
} else {
int nat=0;
cn3.R = cn3.sat.executeQuery("select courses_history.note
from matieres_demandees,courses_history
where courses_history.ID_matiere='"+idpre+"' and courses_history.ID_etudiant="+id);
int ab1=cn3.R.getInt("courses_history.note");
if (ab1 < 40) {
nat = nat + 1;
} else {
nat = nat + 0;
}
if(nat >= 1) {
cn3.sat.executeUpdate("insert into no_pre "+name+","+idmatiere);
}
}
n++;
}
SEVERE: null java.sql.SQLException: Before start of result set
this kind of errors usually happens when your cursor is not positioned at the right place. You need to call something like rs.next()
, which you did with your first result while (cn3.R.next())
but you didn't with the second and third result set after you called n3.sat.executeQuery
inside your if
and else
statement.
like this:
if (idpre.contains("/")){
//...some code
for (int i=0;i<pre2.length;i++){
//some code...
cn3.R = n3.sat.executeQuery("select courses_history.note from matieres_demandees,courses_history where courses_history.ID_matiere='"+pre2[i]+"' and courses_history.ID_etudiant="+id);
while(cn3.R.next()){
//some code.....
}
}
}
else {
//some code
cn3.R= cn3.sat.executeQuery("select courses_history.note from matieres_demandees,courses_history where courses_history.ID_matiere='"+idpre+"' and courses_history.ID_etudiant="+id);
while(cn3.R.next()){
//some code.....
}
}
EDIT: Also before you executeQuery for the second time, you might want to create a different connection for the second query, I am not sure it if it necessary but I usually do.
This is the template I usually use, you can use it as a reference. I hope this helps:
Statement stmt = null;
ResultSet rs = null;
stmt = this.con.createStatement();
try {
stmt = this.con.createStatement();
SQL = "YOUR QUERY \n" +
"and more query";
logger.debug(SQL);
rs = stmt.executeQuery(SQL);
while (rs.next()) {
//do stuff you want with result set
}
} catch (Exception e) {
logger.debug(e.getMessage(), e);
} finally {
close(rs, stmt);
}
//execute another query
try {
stmt = this.con.createStatement();
SQL = "another query to be execute";
logger.debug(SQL);
rs = stmt.executeQuery(SQL);
while (rs.next()) {
//do whateever you want to the result
}
} catch (Exception e) {
logger.debug(e.getMessage(), e);
} finally {
close(rs, stmt);
}
I hope this helps and welcome to StackOverflow. If you find this answer or any other answer solves your problem please mark it as the solution. This will help the community and fellow programmers who run into the same problem in the future. Thanks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.