0

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.

Chase
  • 23
  • 3
  • 3
    Please provide a [mcve], your current code is missing essential parts for reproducibililty. Note that using `executeQuery` to create tables is odd and likely won't work. – Mark Rotteveel Jan 05 '19 at 11:29
  • I repeat my earlier comment: you need to post a [mcve] so we can try it ourself, or see what exactly is executed (type of statements). – Mark Rotteveel Jan 06 '19 at 08:44

0 Answers0