Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package integracja;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /**
- *
- * @author piotr
- */
- public class DBHelper {
- private static final String userName = "sql7143441";
- private static final String password = "CQvdmnCWAb";
- private static final String serverName = "jdbc:mysql://sql7.freesqldatabase.com/sql7143441?zeroDateTimeBehavior=convertToNull";
- private Connection connection;
- public Connection getConnection() throws SQLException {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Where is your MySQL JDBC Driver?");
- e.printStackTrace();
- return null;
- }
- System.out.println("MySQL JDBC Driver Registered!");
- connection = null;
- try {
- connection = DriverManager
- .getConnection(serverName, userName, password);
- } catch (SQLException e) {
- System.out.println("Connection Failed! Check output console");
- e.printStackTrace();
- return null;
- }
- if (connection != null) {
- System.out.println("You made it, take control your database now!");
- return connection;
- } else {
- System.out.println("Failed to make connection!");
- return null;
- }
- }
- public void endConnection() throws SQLException{
- connection.close();
- System.out.println("Connection closed!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement