Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. package testswitch;
  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 Maarten
  16. */
  17.  
  18. public class Database {
  19.  
  20.  
  21. public final static String DB_DRIVER_URL = "com.mysql.jdbc.Driver";
  22. public final static String DB_DRIVER_PREFIX = "jdbc:mysql://";
  23.  
  24. private Connection connection = null;
  25.  
  26. public Database(String dataBaseName, String serverURL, String userName, String passWord) {
  27. try {
  28. // verify that a proper JDBC driver has been installed and linked
  29. if (!selectDriver(DB_DRIVER_URL)) {
  30. return;
  31. }
  32.  
  33. if (serverURL == null || serverURL.isEmpty()) {
  34. serverURL = "localhost:3306";
  35. }
  36.  
  37. // establish a connection to a named Database on a specified server
  38. connection = DriverManager.getConnection(DB_DRIVER_PREFIX + serverURL + "/" + dataBaseName, userName, passWord);
  39. } catch (SQLException eSQL) {
  40. logException(eSQL);
  41. }
  42. }
  43.  
  44. private static boolean selectDriver(String driverName) {
  45. // Selects proper loading of the named driver for Database connections.
  46. // This is relevant if there are multiple drivers installed that match the JDBC type.
  47. try {
  48. Class.forName(driverName);
  49. // Put all non-prefered drivers to the end, such that driver selection hits the first
  50. Enumeration<Driver> drivers = DriverManager.getDrivers();
  51. while (drivers.hasMoreElements()) {
  52. Driver d = drivers.nextElement();
  53. if (!d.getClass().getName().equals(driverName)) {
  54. // move the driver to the end of the list
  55. DriverManager.deregisterDriver(d);
  56. DriverManager.registerDriver(d);
  57. }
  58. }
  59. } catch (ClassNotFoundException | SQLException ex) {
  60. logException(ex);
  61. return false;
  62. }
  63. return true;
  64. }
  65.  
  66. public void executeNonQuery(String query) {
  67. try (Statement statement = connection.createStatement()) {
  68. statement.executeUpdate(query);
  69. } catch (SQLException eSQL) {
  70. logException(eSQL);
  71. }
  72. }
  73.  
  74. public ResultSet executeQuery(String query) {
  75. Statement statement;
  76. try {
  77. statement = connection.createStatement();
  78.  
  79. ResultSet result = statement.executeQuery(query);
  80.  
  81. return result;
  82. } catch (SQLException eSQL) {
  83. logException(eSQL);
  84. }
  85.  
  86. return null;
  87. }
  88.  
  89. private static void logException(Exception e) {
  90. System.out.println(e.getClass().getName() + ": " + e.getMessage());
  91.  
  92. e.printStackTrace();
  93. }
  94. }
  95.  
  96. package testswitch;
  97.  
  98. import java.io.IOException;
  99. import java.sql.Connection;
  100. import java.sql.DriverManager;
  101. import java.sql.PreparedStatement;
  102. import java.sql.ResultSet;
  103. import java.sql.SQLException;
  104. import java.sql.Statement;
  105. import javafx.event.ActionEvent;
  106. import javafx.fxml.FXML;
  107. import javafx.scene.control.Button;
  108. import javafx.scene.control.CheckBox;
  109. import javafx.scene.control.TextField;
  110. import javafx.stage.Stage;
  111. import testswitch.Database;
  112.  
  113. /**
  114. *
  115. * @author Maarten
  116. */
  117. public class gebruikerToevoegenController {
  118.  
  119. //TextFields
  120. @FXML
  121. private TextField FXVoornaam, FXTussenvoegsel, FXAchternaam, FXGebruikersnaam;
  122. @FXML
  123. private TextField FXWachtwoord, FXEmail, FXTelefoonnummer;
  124.  
  125. //Boolean checkbox positie
  126. @FXML
  127. private CheckBox ManagerPosition;
  128. @FXML
  129. private Button gebruikerButton;
  130.  
  131. public final String DB_NAME = "testDatabase";
  132. public final String DB_SERVER = "localhost:3306";
  133. public final String DB_ACCOUNT = "root";
  134. public final String DB_PASSWORD = "root";
  135.  
  136. Database database = new Database(DB_NAME, DB_SERVER, DB_ACCOUNT, DB_PASSWORD);
  137.  
  138. public void handle(ActionEvent event) throws SQLException {
  139.  
  140. String query = "INSERT INTO testDatabase.Gebruikers (Voornaam) VALUES " + FXVoornaam.getText();
  141. try {
  142. database.executeQuery(query);
  143.  
  144. } catch (Exception e) {
  145.  
  146. }
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement