Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package com.mkyong.common;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.Connection;
  5. import java.sql.SQLException;
  6.  
  7. public class JDBCExample {
  8.  
  9. public static void main(String[] argv) {
  10.  
  11. System.out.println("-------- MySQL JDBC Connection Testing ------------");
  12.  
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. } catch (ClassNotFoundException e) {
  16. System.out.println("Where is your MySQL JDBC Driver?");
  17. e.printStackTrace();
  18. return;
  19. }
  20.  
  21. System.out.println("MySQL JDBC Driver Registered!");
  22. Connection connection = null;
  23.  
  24. try {
  25. connection = DriverManager
  26. .getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");
  27.  
  28. } catch (SQLException e) {
  29. System.out.println("Connection Failed! Check output console");
  30. e.printStackTrace();
  31. return;
  32. }
  33.  
  34. if (connection != null) {
  35. System.out.println("You made it, take control your database now!");
  36. } else {
  37. System.out.println("Failed to make connection!");
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement