Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. package com.lab1;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5.  
  6. import org.apache.commons.dbcp2.BasicDataSource;
  7.  
  8. public class DatabaseConnector {
  9.  
  10.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  11.     static final String DB_URL = "jdbc:mysql://localhost:9797/";
  12.  
  13.     static final String USER = "root";
  14.     static final String PASS = "9797";
  15.    
  16.     BasicDataSource ds;
  17.    
  18.     public void connect(String dbName) throws Exception {
  19.         ds = new BasicDataSource();
  20.         ds.setDriverClassName(JDBC_DRIVER);
  21.         ds.setUsername(USER);
  22.         ds.setPassword(PASS);
  23.         ds.setUrl(DB_URL);
  24.     }
  25.    
  26.     public Connection getConnection () throws SQLException {
  27.         if (ds == null)
  28.             throw new SQLException("Not connected to DB");
  29.  
  30.         return ds.getConnection();
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement