Datenbank Verbindung

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • Datenbank Verbindung

      Java-Quellcode: DatabaseConnection

      1. import java.sql.Connection;
      2. import java.sql.DriverManager;
      3. import java.sql.PreparedStatement;
      4. import java.sql.ResultSet;
      5. import java.sql.SQLException;
      6. /**
      7. * Database Class with Connect, Disconnect and Resultset.
      8. *
      9. * @author Tim Lorse
      10. * @version Version 1.0
      11. */
      12. public class Database {
      13. /**
      14. * Connection for Database HyperSQL(HSQLDB) on Localhost with Login SA.
      15. *
      16. * @return Connection if Successful
      17. */
      18. public Connection connect() {
      19. return connect("SA", "");
      20. }
      21. /**
      22. * Connection for Database MySQL on Localhost with Login root.
      23. *
      24. * @return Connection if Successful
      25. */
      26. public Connection connect_mysql(){
      27. return connect_mysql("root", "");
      28. }
      29. /**
      30. * Connection for Database HyperSQL(HSQLDB) on Localhost without Login User.
      31. *
      32. * @param usr Username to Connect with the Database.
      33. * @param passwd Password to Authentication the User.
      34. * @return Connection if Successful
      35. */
      36. public Connection connect(String usr, String passwd) {
      37. return connect("jdbc:hsqldb:hsql://localhost/", usr, passwd);
      38. }
      39. /**
      40. * Connection for Database MySQL on Localhost without Login User.
      41. *
      42. * @param usr Username to Connect with the Database.
      43. * @param passwd Password to Authentication the User.
      44. * @return Connection if Successful
      45. */
      46. public Connection connect_mysql(String usr, String passwd){
      47. return connect_mysql("jdbc:mysql://localhost:3306", usr, passwd);
      48. }
      49. /**
      50. * Connection for Database HyperSQL(HSQLDB) without Login User, Password and
      51. * URL
      52. *
      53. * @param url URL to the Database for Example<br>
      54. * <strong>HSQLDB: </strong>"jdbc:hsqldb:hsql://localhost/"
      55. * @param usr Username to Connect with the Database.
      56. * @param passwd Password to Authentication the User.
      57. * @return Connection if Successful
      58. */
      59. public Connection connect(String url, String usr, String passwd) {
      60. return connect(url, usr, passwd, "org.hsqldb.jdbcDriver");
      61. }
      62. /**
      63. * Connection for Database MySQL without Login User, Password and
      64. * URL
      65. *
      66. * @param url URL to the Database for Example<br>
      67. * <strong>MySQL: </strong>"jdbc:mysql://localhost:3306"
      68. * @param usr Username to Connect with the Database.
      69. * @param passwd Password to Authentication the User.
      70. * @return Connection if Successful
      71. */
      72. public Connection connect_mysql(String url, String usr, String passwd){
      73. return connect(url, usr, passwd, "com.mysql.jdbc.Driver");
      74. }
      75. /**
      76. * Connection without any User, Password or Driver.
      77. *
      78. * @param url URL to the Database for Example <br>
      79. * <strong>HSQLDB: </strong>"jdbc:hsqldb:hsql://localhost/"<br>
      80. * <strong>MySQL: </strong>"jdbc:mysql://localhost:3306"
      81. * @param usr Username to Connect with the Database.
      82. * @param passwd Password to Authentication the User.
      83. * @param driver Database driver. For Example<br>
      84. * <strong>HSQLDB: </strong>"org.hsqldb.jdbcDriver"<br>
      85. * <strong>MySQL: </strong>"com.mysql.jdbc.Driver"
      86. * @return Connection if Successful
      87. */
      88. public Connection connect(String url, String usr, String passwd, String driver) {
      89. Connection con = null;
      90. try {
      91. // Treiber Laden
      92. Class.forName(driver);
      93. con = DriverManager.getConnection(url, usr, passwd);
      94. } catch (ClassNotFoundException cnfe) {
      95. System.err.println("connect: Database Driver not found: " + cnfe);
      96. } catch (SQLException ex) {
      97. System.err.println("connect: " + ex);
      98. }
      99. return con;
      100. }
      101. /**
      102. * Disconnect the Database
      103. * @param con Connection
      104. * @return True or False
      105. */
      106. public boolean disconnect(Connection con) {
      107. boolean ret = false;
      108. if (con != null) {
      109. try {
      110. if (!con.isClosed()) {
      111. con.close();
      112. ret = true;
      113. }
      114. } catch (SQLException ex) {
      115. System.err.println("Exception in disconnect: " + ex);
      116. }
      117. }
      118. return ret;
      119. }
      120. /**
      121. * Resultset from a complete Table
      122. *
      123. * @param con Connection
      124. * @param table Tablename
      125. * @param attribut Attributname
      126. * @param value Value
      127. * @return Result
      128. */
      129. public static ResultSet getResut(Connection con, String table, String attribut, String value) {
      130. ResultSet rs = null;
      131. try (PreparedStatement ptsm = con.prepareStatement("SELECT * FROM " + table + " WHERE " + attribut + " LIKE " + value)) {
      132. rs = ptsm.executeQuery();
      133. } catch (SQLException ex) {
      134. System.err.println("getResult: " + ex);
      135. }
      136. return rs;
      137. }
      138. }
      Alles anzeigen