Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. package Util;
  2.  
  3. import com.sun.rowset.CachedRowSetImpl;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. /**
  12. * Created by Philipp Coblenz on 28.02.2017.
  13. */
  14.  
  15. public class DBConnect {
  16.  
  17. private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  18. private static Connection dbConnection = null;
  19. private static final String conString = "jdbc:oracle:thin:ws1617-team1/X20t7Fkbccx3@132.187.8.167:3306/xe";
  20.  
  21.  
  22. public static void connect() throws SQLException, ClassNotFoundException {
  23. Class.forName(JDBC_DRIVER);
  24. System.out.println("JDBC Driver found");
  25. try {
  26. dbConnection = DriverManager.getConnection(conString);
  27. } catch (SQLException e) {
  28. System.out.println("Could not establish DB connection.\n" + e);
  29. e.printStackTrace();
  30. throw e;
  31. }
  32. }
  33.  
  34.  
  35. public static void disconnect() throws SQLException {
  36. try {
  37. if (dbConnection != null && !dbConnection.isClosed()) {
  38. dbConnection.close();
  39. }
  40. } catch (Exception e){
  41. throw e;
  42. }
  43. }
  44.  
  45. public static ResultSet search(String sqlStatement) throws SQLException, ClassNotFoundException {
  46. Statement statement = null;
  47. ResultSet resultSet = null;
  48. CachedRowSetImpl crs = null;
  49. try {
  50. connect();
  51. statement = dbConnection.createStatement();
  52. resultSet = statement.executeQuery(sqlStatement);
  53.  
  54. crs = new CachedRowSetImpl();
  55. crs.populate(resultSet);
  56. } catch (SQLException e) {
  57. throw e;
  58. } finally {
  59. if (resultSet != null) {
  60. resultSet.close();
  61. }
  62. if (statement != null) {
  63. statement.close();
  64. }
  65. disconnect();
  66. }
  67. return crs;
  68. }
  69.  
  70.  
  71. public static void dbUpdate(String sqlStmt) throws SQLException, ClassNotFoundException {
  72. Statement statement = null;
  73. try {
  74. connect();
  75. statement = dbConnection.createStatement();
  76. statement.executeUpdate(sqlStmt);
  77. } catch (SQLException e) {
  78. throw e;
  79. } finally {
  80. if (statement != null) {
  81. statement.close();
  82. }
  83. disconnect();
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement