Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package Database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. /**
  8. * Class for connection to MySQL database.
  9. *
  10. */
  11. public class DBConnector {
  12.  
  13. private Connection connection = null;
  14.  
  15. private static final String IP = "ServerIp"; //Use Localhost if hosted local
  16. private static final int PORT = 3306; //Port, default MySQL server port
  17.  
  18. /**
  19. *the database/Schema to use on the MySQL server.
  20. */
  21. public static final String DATABASE = "fogdb"; //Database name / Schema name
  22. private static final String USERNAME = "servletuser"; //username with access to the DB
  23. private static final String PASSWORD = "ServletSucks"; //password
  24.  
  25. /**
  26. *
  27. * Creates the connection to the database.
  28. *
  29. * @throws ClassNotFoundException if jdbc driver is not configured
  30. * @throws InstantiationException if it fails to instantiate an object
  31. * @throws IllegalAccessException if the password/username is wrong
  32. * @throws SQLException if an sql statement is not valid sql
  33. */
  34. public DBConnector() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
  35. Class.forName("com.mysql.jdbc.Driver").newInstance();
  36. String url = "jdbc:mysql://" + IP + ":" + PORT + "/" + DATABASE;
  37. this.connection = (Connection) DriverManager.getConnection(url, USERNAME, PASSWORD);
  38. }
  39.  
  40. /**
  41. * gived the Connection object of the database connetion.
  42. *
  43. * @return the connection to the database.
  44. */
  45. public Connection getConnection() {
  46. return this.connection;
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement