Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. package opgD;
  2.  
  3. import java.awt.List;
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6.  
  7. public class Service {
  8.  
  9. private static Connection conn = null;
  10.  
  11. public static Connection createConnection() {
  12. try {
  13. String userName = "root";
  14. String password = "whenever";
  15. String url = "jdbc:mysql://localhost/carletti1";
  16. Class.forName("com.mysql.jdbc.Driver").newInstance();
  17. conn = DriverManager.getConnection(url, userName, password);
  18. System.out.println("\tDatabase connection established");
  19. } catch (Exception e) {
  20. System.err.println("Cannot connect to database server: " + e);
  21. throw new RuntimeException(e.getMessage());
  22. }
  23. return conn;
  24.  
  25. }
  26.  
  27. public static void closeConnection() {
  28. if (conn == null)
  29. return;
  30.  
  31. try {
  32. conn.close();
  33. System.out.println("\tDatabase connection closed");
  34. } catch (SQLException e) {
  35. System.out.println("Cann't close connection\n " + e.getMessage());
  36. throw new RuntimeException(e.getMessage());
  37. }
  38. }
  39.  
  40. public static void startTransaction() {
  41. try {
  42. conn.setAutoCommit(false);
  43. conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  44. } catch (SQLException e) {
  45. throw new RuntimeException(e.getMessage());
  46. }
  47. }
  48.  
  49. public static void commit() {
  50. try {
  51. conn.commit();
  52. } catch (SQLException e) {
  53. System.out.println(e);
  54. }
  55. }
  56.  
  57. public static void rollback() {
  58. try {
  59. conn.rollback();
  60. } catch (SQLException e) {
  61. throw new RuntimeException(e.getMessage());
  62. }
  63. }
  64.  
  65. public static String produkttyper() {
  66. try {
  67. PreparedStatement p = conn.prepareStatement("SELECT navn FROM produkttype ORDER BY navn;");
  68. ResultSet rs = p.executeQuery();
  69.  
  70. while(rs.next()) {
  71. System.out.println(rs.getString("navn"));
  72. }
  73. rs.close();
  74. p.close();
  75. } catch (SQLException e) {
  76. throw new RuntimeException(e.getMessage());
  77. }
  78. return "";
  79. }
  80.  
  81. public static String mellemvarerAfProdukttype(String produkttype) {
  82. try {
  83. PreparedStatement p = conn.prepareStatement("SELECT DISTINCT M.mel_id, P.navn, RF.raeknr, D.navn FROM produkttype P INNER JOIN mellemvare M ON P.prod_id = M.prod_id INNER JOIN retur R ON M.mel_id = R.mellemvare_id INNER JOIN delbehandling D ON R.del_id = D.del_id INNER JOIN raekkefoelge RF ON D.del_id = RF.delbehandling_id WHERE R.afsluttet = 0 AND P.navn = ? ORDER BY M.mel_id;");
  84. p.setString(1, produkttype);
  85. ResultSet rs = p.executeQuery();
  86.  
  87. while(rs.next()){
  88. System.out.println("Mellemvare id: " + rs.getString("M.mel_id"));
  89. System.out.println("Er nået til delbehandling: " +rs.getString("RF.raeknr"));
  90. System.out.println( "Navn på delbehandlingen: " + rs.getString("D.navn"));
  91. System.out.println("--------------------");
  92. }
  93. rs.close();
  94. p.close();
  95. } catch (SQLException e) {
  96. throw new RuntimeException(e.getMessage());
  97. }
  98. return " ";
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement