Advertisement
Guest User

Untitled

a guest
Nov 7th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package integracja;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11.  
  12. /**
  13.  *
  14.  * @author piotr
  15.  */
  16. public class DBHelper {
  17.  
  18.     private static final String userName = "sql7143441";
  19.     private static final String password = "CQvdmnCWAb";
  20.     private static final String serverName = "jdbc:mysql://sql7.freesqldatabase.com/sql7143441?zeroDateTimeBehavior=convertToNull";
  21.     private  Connection connection;
  22.  
  23.     public Connection getConnection() throws SQLException {
  24.         try {
  25.             Class.forName("com.mysql.jdbc.Driver");
  26.         } catch (ClassNotFoundException e) {
  27.             System.out.println("Where is your MySQL JDBC Driver?");
  28.             e.printStackTrace();
  29.             return null;
  30.         }
  31.  
  32.         System.out.println("MySQL JDBC Driver Registered!");
  33.         connection = null;
  34.  
  35.         try {
  36.             connection = DriverManager
  37.                     .getConnection(serverName, userName, password);
  38.  
  39.         } catch (SQLException e) {
  40.             System.out.println("Connection Failed! Check output console");
  41.             e.printStackTrace();
  42.             return null;
  43.         }
  44.  
  45.         if (connection != null) {
  46.             System.out.println("You made it, take control your database now!");
  47.             return connection;
  48.         } else {
  49.             System.out.println("Failed to make connection!");
  50.             return null;
  51.         }
  52.     }
  53.    
  54.         public  void endConnection() throws SQLException{
  55.         connection.close();
  56.         System.out.println("Connection closed!");
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement