Java Connect.java


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.