Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CreatingConnection {
- private static Connection getDBConnection() {
- Connection dbConnection = null;
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- }
- try {
- dbConnection = DriverManager.getConnection("jdbc:postgresql::port/db","postgres", "user");
- return dbConnection;
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return dbConnection;
- }
- static void createDbUserTable() throws SQLException {
- Connection dbConnection = null;
- Statement statement = null;
- String createTableSQL = "CREATE TABLE DBUSER("
- + "USER_ID NUMBER(5) NOT NULL, "
- + "USERNAME VARCHAR(20) NOT NULL, "
- + "MESSAGE VARCHAR(20) NOT NULL, "
- + ")";
- try {
- dbConnection = getDBConnection();
- System.out.println(dbConnection);
- statement = dbConnection.createStatement();
- statement.execute(createTableSQL);
- System.out.println("Table "dbuser" is created!");
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement