Advertisement
Guest User

Untitled

a guest
Dec 31st, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package fr.nytsa.NBC;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class SqlConnection {
  8.  
  9. private Connection connection;
  10.  
  11. private String urlbase, host, database, user, pass;
  12.  
  13. public SqlConnection(String urlbase, String host, String database, String user, String pass) {
  14. this.urlbase = urlbase;
  15. this.host = host;
  16. this.database = database;
  17. this.user = user;
  18. this.pass = pass;
  19. }
  20.  
  21. public void connection() {
  22. if(!isConnected()){
  23. try {
  24. try {
  25. Class.forName("com.mysql.jdbc.Driver");
  26. System.out.println("Driver ok");
  27. } catch (ClassNotFoundException e) {
  28. e.printStackTrace();
  29. }
  30. connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
  31. System.out.println("Connected ok");
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37.  
  38. public void disconnect() {
  39. if(isConnected()){
  40. try {
  41. connection.close();
  42. System.out.println("Connected off");
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48.  
  49. public boolean isConnected(){
  50. return connection != null;
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement