Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. /*
  2. * Toegangssysteem database functies.
  3. *
  4. */
  5.  
  6. package outfit;
  7.  
  8. import java.sql.Timestamp;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.Date;
  16.  
  17. /**
  18. *
  19. * @author itopia
  20. */
  21. public class ToegangsLog {
  22.  
  23. private Connection conn = null;
  24. private static final String dbUser = "yftc_user";
  25. private static final String dbPassword = "YFTCnov09";
  26. private String url;
  27.  
  28. /**
  29. * Connectie met de database.
  30. * Maak zelf methoden om gegevens in de database te stoppen en eruit te halen.
  31. * @return
  32. */
  33. public Connection getConnection() {
  34. // Loading driver
  35. try {
  36. url = "jdbc:mysql://oege.ie.hva.nl:3306/yourfit_tc";
  37. Class.forName("com.mysql.jdbc.Driver");
  38. conn = DriverManager.getConnection(url, dbUser, dbPassword);
  39. } catch (ClassNotFoundException cnfex) {
  40. cnfex.printStackTrace();
  41. } catch (SQLException sqlex) {
  42. sqlex.printStackTrace();
  43. } catch (Exception excp) {
  44. excp.printStackTrace();
  45. }
  46. return conn;
  47. }
  48.  
  49. /**
  50. * test database connectie en enkele SQL queries
  51. */
  52. private void testDB() {
  53. Statement stmt;
  54. ResultSet rs;
  55. conn = getConnection();
  56. //Display URL and connection information
  57. System.out.println("URL: " + url);
  58. System.out.println("Connection: " + conn);
  59. try {
  60. //Get a Statement object
  61. stmt = conn.createStatement();
  62. // Insert an extra row
  63. java.sql.Timestamp now = new Timestamp((new Date()).getTime());
  64. PreparedStatement ps = conn.prepareStatement(
  65. "INSERT INTO kluis_logboek (datum_in, naam, datum_uit, pascode)"+
  66. " VALUES(?,?,?,null)" );
  67. ps.setTimestamp(1, now);
  68. ps.setString(2, "JavaTest");
  69. Timestamp later = new Timestamp(now.getTime()+ 3600*1000);
  70. ps.setTimestamp(3, later); // een uur later
  71. // execute the insert query
  72. ps.executeUpdate();
  73.  
  74. // Get all the records from the table
  75. rs = stmt.executeQuery("SELECT * " +
  76. "FROM kluis_logboek ORDER BY datum_in");
  77. while (rs.next()) {
  78. java.sql.Timestamp din = rs.getTimestamp("datum_in");
  79. java.sql.Timestamp duit = rs.getTimestamp("datum_uit");
  80. System.out.println("Datum in: "+din + "\tUit: "+duit);
  81. int pas = rs.getInt("pascode");
  82. String s = rs.getString("naam");
  83. System.out.println("pas= " + pas + " naam=" + s);
  84. }//end while loop
  85. } catch (SQLException ex) {
  86. ex.printStackTrace();
  87. return;
  88. }
  89. }
  90.  
  91. /**
  92. * Testmethode om database toegang standalone te testen (run deze file)
  93. * @param args
  94. */
  95. public static void main(String[] args) {
  96. new ToegangsLog().testDB();
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement