Guest User

Untitled

a guest
Jan 13th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. /**
  7. *
  8. * @author alaa
  9. */
  10.  
  11. // import com.sun.org.apache.bcel.internal.util.ClassPath;
  12.  
  13. import java.sql.*;
  14.  
  15. public class MysqlConnect
  16. {
  17. public static void main (String[] args)
  18. {
  19. Connection conn = null;
  20.  
  21. try
  22. {
  23. //String username = "root";
  24. //String password = "password";
  25. //String database = "quiz";
  26.  
  27. String url = "jdbc:mysql://127.0.0.1/quiz?user=root&password=password";
  28.  
  29. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  30. conn = DriverManager.getConnection (url);
  31. System.out.println ("Database connection established");
  32. }
  33. catch (Exception e)
  34. {
  35. // System.out.println(System.getenv("CLASSPATH"));
  36. System.err.println ("Cannot connect to database server");
  37. }
  38. finally
  39. {
  40. if (conn != null)
  41. {
  42. try
  43. {
  44. conn.close ();
  45. System.out.println(System.getenv("CLASSPATH"));
  46.  
  47. System.out.println ("Database connection terminated");
  48. }
  49. catch (Exception e) { /* ignore close errors */ }
  50. }
  51. }
  52. }
  53. }
Add Comment
Please, Sign In to add comment