I am learning java connection with postgresql using jdbc driver.
The method newTable() creates two relational tables with one with a primary and a foreign key in postgreSQL using jdbc. The code below runs fine and creates two tables as expected.
But when I try to combine the two try and catch method and eliminate the try catch in the middle(start here -> end here in the code section), it only executes the first resultset and creates the first table and skips the second one.
May I know what causes this behavior.
public class createTables {
private static Connection con; private static ResultSet rs; private static Statement st;
public createTables(String url, String username, String password){
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(url, username, password);
st = con.createStatement();
} catch (Exception e) {
System.err.println(e.getClass().getName()+": "+e.getMessage());
}}
// create new table in postgreSQL
public void newTable() {
try {
//calls a method which returns string to create sql statement for table with a primary key
rs = st.executeQuery(tablePrimary());
//starts here
} catch (Exception e) {
System.err.println(e.getClass().getName()+": "+e.getMessage());
}
try {
//ends here
//calls a method which returns a string to create sql statement for a table with a foreign key
rs = st.executeQuery(tableForeign());
} catch (Exception e) {
System.err.println(e.getClass().getName()+": "+e.getMessage());
}
}
Thanks in advance and please excuse my messy code.