Guest User

Untitled

a guest
Mar 1st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class einlesen {
  4. static Statement sql_stmt = null;
  5. static Connection conn = null;
  6. public static void main(String[]args){
  7. loadmenue();
  8. }
  9. public static void loadmenue(){
  10. System.out.println(" ");
  11. System.out.println("++++Menue+++++");
  12. System.out.println("anzeigen");
  13. System.out.println("einfuegen");
  14. System.out.println("loeschen");
  15. System.out.println("beenden");
  16. System.out.println(" ");
  17.  
  18. Eingabe tastatur = Eingabe.oeffnen("");
  19. String s = tastatur.readString();
  20. if(s.equals("anzeigen"))
  21. {
  22. anzeigen();
  23. }
  24. else if(s.equals("einfuegen")){
  25. System.out.println("Albumtitel");
  26. String a = tastatur.readString();
  27. System.out.println("Interpret");
  28. String i = tastatur.readString();
  29. System.out.println("Erscheinungsjahr");
  30. int abc = tastatur.readint();
  31. einfuegen(a,i,abc);
  32.  
  33. }
  34. else if(s.equals("loeschen")){
  35. String loe = tastatur.readString();
  36. loeschen(loe);
  37. }else if(s.equals("beenden")){
  38. System.exit(-1);
  39. }else{System.out.println("unkown command");
  40. loadmenue();
  41. }
  42. }
  43. public static void anzeigen(){
  44. Statement sql_stmt = null;
  45. Connection conn = null;
  46. try {
  47. Class.forName("org.hsqldb.jdbcDriver");
  48. conn = DriverManager.getConnection
  49. ("jdbc:hsqldb:hsql://localhost/", "sa", "");
  50. sql_stmt = conn.createStatement();
  51. }
  52. catch (Exception e) {
  53. System.err.println("Folgender Fehler ist aufgetreten: " + e);
  54. System.exit(-1); }
  55.  
  56. try {
  57. ResultSet rset = sql_stmt.executeQuery(
  58. "select Albumtitel,Interpret from Album");
  59. System.out.println("Alben:");
  60. while(rset.next()) {
  61. System.out.println(rset.getString("Albumtitel") + " von: " +
  62. rset.getString("Interpret"));
  63. }
  64. rset.close();
  65. loadmenue();
  66. }
  67. catch(SQLException se) {System.out.println("Error: " + se); }
  68. try {
  69. sql_stmt.close(); conn.close();
  70. }
  71. catch (SQLException e) {
  72. System.out.println("Fehler beim Schliessen der DB: " + e);
  73. }
  74. }
  75.  
  76. public static void einfuegen(String t, String i, int j){
  77. Statement sql_stmt = null;
  78. Connection conn = null;
  79. try {
  80. Class.forName("org.hsqldb.jdbcDriver");
  81. conn = DriverManager.getConnection
  82. ("jdbc:hsqldb:hsql://localhost/", "sa", "");
  83. sql_stmt = conn.createStatement();
  84. }
  85. catch (Exception e) {
  86. System.err.println("Folgender Fehler ist aufgetreten: " + e);
  87. System.exit(-1); }
  88.  
  89. try {
  90.  
  91. String sqlQuery = null;
  92. Statement stmt = conn.createStatement();
  93. sqlQuery = ("INSERT INTO Album (Albumtitel, Interpret, Erscheinungsjahr) Values('"+t+"', '"+i+"', "+j+")");
  94. stmt.executeUpdate(sqlQuery);
  95. loadmenue();
  96. }
  97. catch(SQLException se) {System.out.println("Error: " + se); }
  98. try {
  99. sql_stmt.close(); conn.close();
  100. }
  101. catch (SQLException e) {
  102. System.out.println("Fehler beim Schliessen der DB: " + e);
  103. }
  104. }
  105. public static void loeschen(String loe){
  106. Statement sql_stmt = null;
  107. Connection conn = null;
  108. try {
  109. Class.forName("org.hsqldb.jdbcDriver");
  110. conn = DriverManager.getConnection
  111. ("jdbc:hsqldb:hsql://localhost/", "sa", "");
  112. sql_stmt = conn.createStatement();
  113. }
  114. catch (Exception e) {
  115. System.err.println("Folgender Fehler ist aufgetreten: " + e);
  116. System.exit(-1); }
  117.  
  118. try {
  119.  
  120. String sqlQuery = null;
  121. Statement stmt = conn.createStatement();
  122. sqlQuery = ("DELETE FROM Album WHERE Albumtitel='"+loe+"'");
  123. stmt.executeUpdate(sqlQuery);
  124. loadmenue();
  125. }
  126. catch(SQLException se) {System.out.println("Error: " + se); }
  127. try {
  128. sql_stmt.close(); conn.close();
  129. }
  130. catch (SQLException e) {
  131. System.out.println("Fehler beim Schliessen der DB: " + e);
  132. }
  133. }
  134.  
  135. }
Add Comment
Please, Sign In to add comment