Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package dbprogram;
  2.  
  3. //Step 1: Use interfaces from java.sql package
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. public class JDBCMySQLConnection {
  9. //static reference to itself
  10. private static JDBCMySQLConnection instance = new JDBCMySQLConnection();
  11. public static final String URL = "jdbc:mysql://localhost/mydb";
  12. public static final String USER = "root";
  13. public static final String PASSWORD = "jakobsn";
  14. public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
  15.  
  16. //private constructor
  17. private JDBCMySQLConnection() {
  18. try {
  19. //Step 2: Load MySQL Java driver
  20. Class.forName(DRIVER_CLASS);
  21. } catch (ClassNotFoundException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25.  
  26. private Connection createConnection() {
  27.  
  28. Connection connection = null;
  29. try {
  30. //Step 3: Establish Java MySQL connection
  31. connection = DriverManager.getConnection(URL, USER, PASSWORD);
  32. } catch (SQLException e) {
  33. System.out.println("ERROR: Unable to Connect to Database.");
  34. }
  35. return connection;
  36. }
  37.  
  38. public static Connection getConnection() {
  39. return instance.createConnection();
  40. }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement