Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.sql.DatabaseMetaData;
  2. import java.sql.DriverManager;
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5.  
  6. public class JDBCExample {
  7.  
  8.     public static void main(String[] argv) {
  9.  
  10.         System.out.println("-------- PostgreSQL "
  11.                 + "JDBC Connection Testing ------------");
  12.  
  13.         try {
  14.  
  15.             Class.forName("org.postgresql.Driver");
  16.  
  17.         } catch (ClassNotFoundException e) {
  18.  
  19.             System.out.println("Where is your PostgreSQL JDBC Driver? "
  20.                     + "Include in your library path!");
  21.             e.printStackTrace();
  22.             return;
  23.  
  24.         }
  25.  
  26.         System.out.println("PostgreSQL JDBC Driver Registered!");
  27.  
  28.         Connection connection = null;
  29.  
  30.         try {
  31.  
  32.             connection = DriverManager.getConnection(
  33.                     "jdbc:postgresql://127.0.0.1:5432/asldb", "nico",
  34.                     "");
  35.         } catch (SQLException e) {
  36.  
  37.             System.out.println("Connection Failed! Check output console");
  38.             e.printStackTrace();
  39.             return;
  40.  
  41.         }
  42.  
  43.         if (connection != null) {
  44.             System.out.println("You made it, take control your database now!");
  45.         } else {
  46.             System.out.println("Failed to make connection!");
  47.         }
  48.         try {
  49.             connection.close();
  50.         } catch (SQLException e) {
  51.             // TODO Auto-generated catch block
  52.             e.printStackTrace();
  53.         }
  54.     }
  55.  
  56. }
Add Comment
Please, Sign In to add comment