Last active
July 23, 2019 06:53
-
-
Save psrdotcom/9c43e5c77c663bc910ecd2f5fb569e08 to your computer and use it in GitHub Desktop.
Postgre SQL Connection Testing with Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Connect to the Postgre SQL Database and test connection