Guest User

Untitled

a guest
Apr 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. public static void main (String[] args)
  2. {
  3. Connection conn = null;
  4.  
  5. try
  6. {
  7. String userName = "testuser";
  8. String password = "testpass";
  9. String url = "jdbc:mysql://localhost/test";
  10. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  11. conn = DriverManager.getConnection (url, userName, password);
  12. System.out.println ("Database connection established");
  13. }
  14. catch (Exception e)
  15. {
  16. System.err.println ("Cannot connect to database server");
  17. }
  18. finally
  19. {
  20. if (conn != null)
  21. {
  22. try
  23. {
  24. conn.close ();
  25. System.out.println ("Database connection terminated");
  26. }
  27. catch (Exception e) { /* ignore close errors */ }
  28. }
  29. }
  30. }
Add Comment
Please, Sign In to add comment