Posted By

itguy51 on 07/29/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

chinyeh


Java Connect.java


 / Published in: Java
 

  1. import java.sql.*;
  2.  
  3. public class Connect
  4. {
  5. public static void main (String[] args)
  6. {
  7. Connection conn = null;
  8. String userName = "Username";
  9. String password = "Password";
  10. String url = "jdbc:mysql://192.168.1.121/drupal?user=" + userName + "&password=" + password;
  11. System.out.println(url);
  12. try
  13. {
  14.  
  15. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  16. conn = DriverManager.getConnection (url);
  17. System.out.println ("Database connection established");
  18. }
  19. catch (Exception e)
  20. {
  21. System.err.println ("Cannot connect to database server");
  22. }
  23. finally
  24. {
  25. if (conn != null)
  26. {
  27. try
  28. {
  29. conn.close ();
  30. System.out.println ("Database connection terminated");
  31. }
  32. catch (Exception e) { /* ignore close errors */ }
  33. }
  34. }
  35. }
  36. }

Report this snippet  

You need to login to post a comment.