How Can We Help?
< All Topics
Print

PostgreSQL Java Connection

Connection to PostgreSQL for Java Applications

To establish a connection between the PostgreSQL database and your Java application, you need to create the corresponding configuration file with your connection data as described below. Subsequently, you could check that everything works fine with provided application sample.

1. Log into BitssClooud.

2. Create Java environment with PostgreSQL 9.5.5 database:

3. Check your e-mail – your inbox should have a message with database login and password:

                                                          4. Click the Config button next to the application server (Tomcat 8) in your environment.

In the opened tab create the mydb.cfg file in the temp folder.

5. Add all necessary configurations:host=jdbc:postgresql://postgres{node_id}-{your_env_name}.{hoster_domain}/{db_name}
username={get in the email from BitssClooud }
password={get in the email from BitssClooud }
driver=org.postgresql.Driver

Note: You can mention all connecting settings in your code (application) apparently. In the given example we put all the settings to the file, which is read by our application. 

6. As an example, you can see the code of our application which connects to our database.DbManager.java :  

package connection;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class DbManager {
 
    private final static String createTable = "CREATE TABLE example (id INT, data VARCHAR(100))";
 
    public Connection createConnection() throws IOException, ClassNotFoundException, SQLException {
 
        Connection connection;
         
        Properties prop = new Properties();
        System.out.println("test");
        prop.load(new FileInputStream(System.getProperty("user.home") + "/mydb.cfg"));
        System.out.println("user.home: "+System.getProperty("user.home"));
        String host = prop.getProperty("host").toString().trim();
        String username = prop.getProperty("username").toString().trim();
        String password = prop.getProperty("password").toString().trim();
        String driver = prop.getProperty("driver").toString().trim();
 
        System.out.println("host: " + host + "\username: " + username + "\password: " + password + "\ndriver: " + driver);
 
        Class.forName(driver);
        System.out.println("--------------------------");
        System.out.println("DRIVER: " + driver);
        connection = DriverManager.getConnection(host, username, password);
        System.out.println("CONNECTION: " + connection);
 
        return connection;
    }
 
    public void runSqlStatement() {
        try {
            Statement statement = createConnection().createStatement();
            boolean rs = statement.execute(createTable);
 
        } catch (IOException ex) {
            Logger.getLogger(DbManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DbManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

7. The next step is to upload the .war file to BitssClooud Deployment Manager. As an example we used dbconnexample.war file (click to download it) which contains the appropriate jdbc-connector.Click the link dbconnexample to download the package with the sources of our project.

8. Deploy the uploaded WAR file to the environment.

9. Click Open in Browser next to the application server (Tomcat 8).

10. Click Open in Browser next to the PostgreSQL database node to see the created table in the database Admin panel.

Table of Contents