Guest User

Untitled

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