Skip to content

Instantly share code, notes, and snippets.

@psrdotcom
Last active July 23, 2019 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psrdotcom/9c43e5c77c663bc910ecd2f5fb569e08 to your computer and use it in GitHub Desktop.
Save psrdotcom/9c43e5c77c663bc910ecd2f5fb569e08 to your computer and use it in GitHub Desktop.
Postgre SQL Connection Testing with Java
/*
* This Java source file was to test the database connection.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Library {
public static void main(String args[]) {
String url = "jdbc:postgresql://localhost:5432/testdb";
String user = "postgres";
String password = "123*";
try {
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(url, user, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException | ClassNotFoundException ex) {
System.out.println("Exception: " + ex.getMessage());
}
}
}
@psrdotcom
Copy link
Author

Connect to the Postgre SQL Database and test connection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment