Advertisement
Guest User

Check Database Connection

a guest
May 2nd, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.80 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class CheckDatabaseConnection {
  4.     private static final String DATABASE = "silvestri";
  5.     private static final String USERNAME = "readonly";
  6.     private static final String PASSWORD = "readonly";
  7.  
  8.  
  9.     static String driver = "com.mysql.jdbc.Driver";
  10.     static String url = "jdbc:mysql://cs.stcc.edu/" + DATABASE +
  11.                         "?user=" + USERNAME + "&password=" + PASSWORD;
  12.        
  13.         public static void main(String[] args) {
  14.             try {
  15.                 Class.forName(driver).newInstance();
  16.                 System.out.println("Loaded driver");
  17.  
  18.                 Connection conn = DriverManager.getConnection(url);
  19.                 System.out.println("Connected to database");
  20.  
  21.                 conn.close();
  22.                 System.out.println("Closed connection");   
  23.             }
  24.             catch (Exception e) {
  25.                 System.err.println(e.getMessage());
  26.             }
  27.         }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement