2023-12-12

Converting and printing resultset from SQL

I'm quite new in SQL and Java and have a question regarding a sample exercise I am doing. I am trying to convert this table:

SELECT id, lname, fname, credit, year, course 
FROM records
WHERE id is NOT NULL
ORDER BY last_name, first_name;
ID lName fName credit year course
01001 Adams John PHD N 1110
01001 Adams John PHD N 2001
01001 Adams John PHD N 2115
02003 Alexander Amy MD N 1002
02003 Alexander Amy MD N 1016
02003 Alexander Amy MD N 1019
02003 Alexander Amy MD N 1008
02003 Alexander Amy MD N 2110
02003 Alexander Amy MD N 2010
02003 Alexander Amy MD N 2030
02003 Alexander Amy MD N 2200
02003 Alexander Amy MD N 2201
02003 Alexander Amy MD N 2310
01400 Anderson Louise PHD N 2311
01400 Anderson Louise PHD N 2361
01400 Anderson Louise PHD N 2062
01400 Anderson Louise PHD N 2008
01400 Anderson Louise PHD N 2002

into Converting the column Year N to:

Year 01: Has 1110

Year 02: Has 2110

Year 03: Does not have (1110; 2110; 3110)

So that the program will print out something like this:

ID lName fName credit year
0100 Adams John PHD 01
02003 Alexander Amy MD 02
01400 Anderson Louise PHD 03

I have a resultset coded something like this:


String SQL = " SELECT id, lname, fname, credit, year, course "
           + " FROM records "
           + " WHERE id is NOT NULL "
           + " ORDER BY last_name, first_name; "
PreparedStatement printSQL = connection.prepareStatement(SQL);
ResultSet rs = printSQL.executeQuery();

while(rs.next()) {
     if(year.equals("N") && course.equals("1110")) {
          year = "01";
     }
     else if(year.equals("N") && course.equals("2110")) {
          year = "02";
     }
     else if(year.equals("N") && (!course.equals("1110") 
            && !course.equals("2110")&& !course.equals("3110"))){
          year = "03";
     }
}

But it only either checks the first or the last person and not all the row that either has 1110, 2110 or doesn't have 1110, 2110 and 3110. Any help is appreciated!



No comments:

Post a Comment