Я изучаю Java-соединение с Postgresql с помощью драйвера JDBC.
Метод newTable () создает две реляционные таблицы с одной с первичным и внешним ключом в postgreSQL, используя jdbc. Код ниже работает нормально и создает две таблицы, как и ожидалось.
Но когда я пытаюсь объединить два метода try и catch и исключить перехват try в середине (начало здесь -> end здесь в разделе кода), он выполняет только первый набор результатов и создает первую таблицу и пропускает второй.
Могу ли я знать, что вызывает такое поведение.
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());
}
}
Заранее спасибо и прошу прощения за мой грязный код.