Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1.  
  2. import java.sql.*;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. public class DBSUebung {
  8. /**
  9. * Get a new database connection
  10. *
  11. * @return
  12. * @throws SQLException
  13. */
  14. public Connection getConnection() throws SQLException {
  15. String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
  16. String url="jdbc:db2://localhost:50001/DB2DB1";
  17. String user="db2inst1";
  18. String password="ws2011";
  19.  
  20. Connection conn = null;
  21.  
  22. try {
  23. //Load class into memory
  24. Class.forName(jdbcClassName);
  25. //Establish connection
  26. conn = DriverManager.getConnection(url, user, password);
  27.  
  28. } catch (ClassNotFoundException e) {
  29. e.printStackTrace();
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. }
  33.  
  34. return conn;
  35. }
  36.  
  37. /**
  38. * Run a SQL command which does not return a recordset:
  39. * CREATE/INSERT/UPDATE/DELETE/DROP/etc.
  40. *
  41. * @throws SQLException If something goes wrong
  42. */
  43. public ResultSet executeQuery(Connection conn, String command) throws SQLException {
  44. Statement stmt = null;
  45. ResultSet rs = null;
  46. try {
  47. stmt = conn.createStatement();
  48. rs = stmt.executeQuery(command); // This will throw a SQLException if it fails
  49. } finally {
  50.  
  51. // This will run whether we throw an exception or not
  52. //if (stmt != null) { stmt.close(); }
  53. }
  54.  
  55. return rs;
  56. }
  57.  
  58. /**
  59. * Connect to MySQL and do some stuff.
  60. */
  61. public void run() throws SQLException {
  62.  
  63. // Connect to MySQL
  64. Connection conn = null;
  65. conn = this.getConnection();
  66.  
  67. ResultSet rs = null;
  68. ResultSet rs_inner = null;
  69.  
  70. String inputstr = "Schwarz";
  71.  
  72. System.out.println("MOVIES");
  73.  
  74. rs = this.executeQuery(conn, "SELECT DISTINCT * FROM movie m WHERE title LIKE '%" + inputstr + "%' ORDER BY title asc");
  75.  
  76. while(rs.next()) {
  77. System.out.print(rs.getString("title") + ", " + rs.getString("year"));
  78.  
  79. //genre
  80. rs_inner = this.executeQuery(conn, "SELECT genre FROM genre WHERE movie_id = '" + rs.getString("mid") + "'");
  81.  
  82. while(rs_inner.next()) {
  83. System.out.print(", " + rs_inner.getString("genre"));
  84. }
  85.  
  86. System.out.println("\r");
  87.  
  88. //actor
  89. // TODO: Limit 5
  90. rs_inner = this.executeQuery(conn, "SELECT * FROM ((SELECT * FROM actor) UNION (SELECT * FROM actress)) act WHERE movie_id = '" + rs.getString("mid") + "' ORDER BY act.name asc FETCH FIRST 5 ROWS ONLY");
  91.  
  92. while(rs_inner.next()) {
  93. System.out.println(" " + rs_inner.getString("name"));
  94. }
  95.  
  96. System.out.println("\r");
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. ResultSet rs_movies = null;
  105. System.out.println("ACTORS");
  106.  
  107. rs = this.executeQuery(conn, "SELECT DISTINCT name FROM ((SELECT * FROM actor) UNION (SELECT * FROM actress)) act WHERE name LIKE '%" + inputstr + "%'");
  108.  
  109. while(rs.next()) {
  110. System.out.println(rs.getString("name"));
  111.  
  112. // played in
  113. System.out.println(" PLAYED IN");
  114.  
  115. rs_movies = this.executeQuery(conn, "SELECT * FROM movie m JOIN ((SELECT * FROM actor) UNION (SELECT * FROM actress)) act ON m.mid = act.movie_id WHERE act.name = '" + rs.getString("name") + "'");
  116.  
  117. while(rs_movies.next()) {
  118. System.out.println(" " + rs_movies.getString("title"));
  119. }
  120.  
  121. // Co-stars
  122. // TODO: Anzahl der Filme, in dem beide mitgespielt haben, sortiert nach I. Anzahl II. Nam
  123. System.out.println(" CO-STARS");
  124.  
  125. rs_inner = this.executeQuery(conn, "SELECT name, COUNT(movie_id) as amount FROM ((SELECT * FROM actor) UNION (SELECT * FROM actress)) act WHERE act.movie_id IN (SELECT movie_id FROM ((SELECT * FROM actor) UNION (SELECT * FROM actress)) act WHERE name LIKE '%" + inputstr + "%' AND name <> '" + rs.getString("name") + "') GROUP BY name ORDER BY amount desc, act.name asc FETCH FIRST 5 ROWS ONLY");
  126.  
  127. while(rs_inner.next()) {
  128. System.out.println(" " + rs_inner.getString("name") + " (" + rs_inner.getString("amount") + ")");
  129. }
  130.  
  131. System.out.println("\r");
  132.  
  133. }
  134. }
  135.  
  136. /**
  137. * Connect to the DB and do some stuff
  138. */
  139. public static void main(String[] args) throws SQLException {
  140. DBSUebung app = new DBSUebung();
  141. app.run();
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement