Advertisement
Guest User

Database HVA (©Jerry)

a guest
Nov 30th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. package hellodatabase;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Driver;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Enumeration;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. /**
  14. *
  15. * @author lennardf1989
  16. */
  17. public class Database {
  18.  
  19. private final static String DB_DRIVER_URL = "com.mysql.jdbc.Driver";
  20. private final static String DB_DRIVER_PREFIX = "jdbc:mysql://";
  21.  
  22. private Connection connection = null;
  23.  
  24. public Database(String dataBaseName, String serverURL, String userName, String passWord) {
  25. try {
  26. // verify that a proper JDBC driver has been installed and linked
  27. if (!selectDriver(DB_DRIVER_URL)) {
  28. return;
  29. }
  30.  
  31. if (serverURL == null || serverURL.isEmpty()) {
  32. serverURL = "127.0.0.1:3306";
  33. }
  34.  
  35. // establish a connection to a named database on a specified server
  36. connection = DriverManager.getConnection(DB_DRIVER_PREFIX + serverURL + "/" + dataBaseName, userName, passWord);
  37. } catch (SQLException eSQL) {
  38. logException(eSQL);
  39. }
  40. }
  41.  
  42. private static boolean selectDriver(String driverName) {
  43. // Selects proper loading of the named driver for database connections.
  44. // This is relevant if there are multiple drivers installed that match the JDBC type.
  45. try {
  46. Class.forName(driverName);
  47. // Put all non-prefered drivers to the end, such that driver selection hits the first
  48. Enumeration<Driver> drivers = DriverManager.getDrivers();
  49. while (drivers.hasMoreElements()) {
  50. Driver d = drivers.nextElement();
  51. if (!d.getClass().getName().equals(driverName)) {
  52. // move the driver to the end of the list
  53. DriverManager.deregisterDriver(d);
  54. DriverManager.registerDriver(d);
  55. }
  56. }
  57. } catch (ClassNotFoundException | SQLException ex) {
  58. logException(ex);
  59. return false;
  60. }
  61. return true;
  62. }
  63.  
  64. public void executeNonQuery(String query) {
  65. try (Statement statement = connection.createStatement()) {
  66. statement.executeUpdate(query);
  67. } catch (SQLException eSQL) {
  68. logException(eSQL);
  69. }
  70. }
  71.  
  72. public ResultSet executeQuery(String query) {
  73. Statement statement;
  74. try {
  75. statement = connection.createStatement();
  76.  
  77. ResultSet result = statement.executeQuery(query);
  78.  
  79. return result;
  80. } catch (SQLException eSQL) {
  81. logException(eSQL);
  82. }
  83.  
  84. return null;
  85. }
  86.  
  87. private static void logException(Exception e) {
  88. System.out.println(e.getClass().getName() + ": " + e.getMessage());
  89.  
  90. e.printStackTrace();
  91. }
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. Database db = new Database(
  108. "NAAM DATABASE", //naam database
  109. "127.0.0.1:3306", //localhost + port
  110. "root", //gebruikersnaam mysqlworkbench
  111. "" //wachtwoord mysqlworkbench
  112. );
  113.  
  114. db.executeQuery("SELECT * FROM helloworkbench.cijfer;");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement