I'm making a program that uses Java as a front to do some SQL manipulations of data. I've got most of it set up, and I am able to make the connection to my database, however for some reason when I try and run my query, it returns a line saying "Could not execute query" and I cannot figure out why it can't execute the query.
public class application {
public static void main (String args[]) throws Exception,
IOException, SQLException {
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println ("Could not load the driver");
}
String user = readEntry("Enter userid: ");
String pass = readEntry("Enter password: ");
Connection conn = DriverManager.getConnection (
"jdbc:oracle:thin:@serveraddress",user,pass);
boolean done = false;
do {
printMenu();
System.out.print("Type in your option: ");
System.out.flush();
String ch = readLine();
System.out.println();
switch (ch.charAt(0)) {
case '1': studentModules(conn);
break;
//some other cases
case '0': done = true;
break;
default : System.out.println(" Not a valid option ");
} //switch
} while(!done);
} // main
private static void studentModules(Connection conn) throws SQLException, IOException {
String sqlString = null;
Statement stmt = conn.createStatement();
sqlString = "select Student_id" +
"from STUDENT;";
ResultSet rset;
ResultSet rset2;
try {
rset = stmt.executeQuery(sqlString);
} catch (SQLException e) {
System.out.println("Could not execute query");
stmt.close();
return;
}
String print;
while (rset.next()) {
print = rset.getString(1) + ": ";
sqlString = "select Module_code" +
"from EXAM" +
"where Student_id = " + rset.getString(1) + ";";
try {
rset2 = stmt.executeQuery(sqlString);
} catch (SQLException e) {
System.out.println("Could not execute query");
stmt.close();
return;
}
while (rset2.next()) {
print = print + rset.getString(1) + " ";
}
System.out.println(print);
}
stmt.close();
}
I know it's a long piece of code, but I cannot figure why it goes wrong. I know it is erroring and being caught when trying to query "SELECT Student_id FROM STUDENT", but that query isn't wrong and I can't understand why it would throw an error there?
Thanks for any help.
SQLExceptionyou should print it's stacktrace somewhere and there should be be more information about what is wrong.Student_idandfromin your query