Advertisement
Guest User

Untitled

a guest
Mar 7th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. package SQLConnection;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class SQLUtility {
  11.  
  12. private static Connection minConnection;
  13. private static Statement stmt;
  14. private static PreparedStatement prepStatement;
  15.  
  16. private static final String databaseUsed = "DAOS";
  17.  
  18. public static Connection openConnection() {
  19.  
  20. try {
  21. minConnection = DriverManager.getConnection(
  22. // "jdbc:sqlserver://EAA-SH-STHA-AO;databaseName=Yodafitness;user=sa;password=HelloWorld;");
  23. "jdbc:sqlserver://CRYONICS-LAPTOP\\SQLEXPRESS;databaseName=" + databaseUsed
  24. + ";user=sa;password=daos;");
  25.  
  26. stmt = minConnection.createStatement();
  27.  
  28. return minConnection;
  29.  
  30. } catch (Exception e) {
  31. System.out.println("fejl: " + e.getMessage());
  32. return null;
  33. }
  34.  
  35. }
  36.  
  37. public static void closeConnection() {
  38. if (stmt != null) {
  39. try {
  40. stmt.close();
  41. } catch (SQLException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. if (prepStatement != null) {
  46. try {
  47. prepStatement.close();
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. if (minConnection != null) {
  53. try {
  54. minConnection.close();
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60.  
  61. public static ResultSet makeQuery(String input) {
  62. try {
  63. ResultSet res = stmt.executeQuery(input);
  64.  
  65. return res;
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. return null;
  69. }
  70. }
  71.  
  72. // Dropper den existerende og opretter en ny.
  73. public static void makeProc(String procName, String query) {
  74. dropProc(procName);
  75. String procedure = "CREATE PROCEDURE " + "." + procName + " AS " + query;
  76. try {
  77. stmt.executeUpdate(procedure);
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82.  
  83. public static void dropProc(String procName) {
  84. String procedure = doesProcExistString(procName) + "DROP PROCEDURE " + procName;
  85. try {
  86. stmt.executeUpdate(procedure);
  87. } catch (SQLException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91.  
  92. public static String doesProcExistString(String procName) {
  93. return "IF(OBJECT_ID('" + procName + "') IS NOT NULL) ";
  94. }
  95.  
  96. public static ResultSet runProc(String procName) {
  97. String procedure = "EXEC " + procName;
  98. try {
  99. ResultSet res = stmt.executeQuery(procedure);
  100. return res;
  101. } catch (SQLException e) {
  102. e.printStackTrace();
  103. return null;
  104. }
  105. }
  106.  
  107. public static PreparedStatement createPrepStatement(String query) {
  108. prepStatement = null;
  109. if (minConnection == null) {
  110. System.out.println("WTF");
  111. }
  112. try {
  113. prepStatement = minConnection.prepareStatement(query);
  114. } catch (SQLException e) {
  115. e.printStackTrace();
  116. }
  117. return prepStatement;
  118. }
  119.  
  120. public static int getMaxInt(String columnName, String tableName) {
  121. String query = "select max(" + columnName + ") as id from " + tableName;
  122.  
  123. ResultSet set = makeQuery(query);
  124.  
  125. int number = -1;
  126. try {
  127. while (set.next()) {
  128. number = set.getInt("id");
  129. }
  130. } catch (SQLException e) {
  131. e.printStackTrace();
  132. }
  133.  
  134. return number;
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement