Guest User

Untitled

a guest
Dec 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package kth.csc.inda.KebabFredag;
  2.  
  3. import java.sql.*;
  4. import java.util.Properties;
  5. import java.util.TreeSet;
  6. import java.io.FileNotFoundException;
  7. import java.util.Collection;
  8. /**
  9. * Reads the event from the sqlserver
  10. * @author Jonas Daniels
  11. * version 1.0
  12. *
  13. */
  14. public class SQLReader {
  15. //implements DataReader
  16. public SQLReader() {
  17. // connect to the SQL server
  18. }
  19.  
  20. public static void main(String args[]) throws InstantiationException, IllegalAccessException {
  21. System.out.println ("Main started.");
  22. try {
  23. readAllEvents();
  24. } catch (FileNotFoundException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. } catch (SQLException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. } catch (ClassNotFoundException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public static Collection<Event> readAllEvents() throws ClassNotFoundException, SQLException, FileNotFoundException, InstantiationException, IllegalAccessException {
  37. Collection<Event> collection = new TreeSet<Event>();
  38. System.out.println ("Start of getDBConnection.");
  39.  
  40. Connection db=null;
  41. Class.forName("com.mysql.jdbc.Driver").newInstance();
  42. System.out.println ("driver.newInstance gotten.");
  43.  
  44. //Properties settings = new Properties ();
  45. //settings.setProperty ("user", System.getProperty ("user.name"));
  46. String url = "jdbc:mysql://free-mysql.BizHostNet.com:3306/1303092403";
  47. String user= "1303092403";
  48. String password="kebab";
  49. db = DriverManager.getConnection (url, user, password);
  50. System.out.println("Database connection established");
  51.  
  52. String query = "SELECT * FROM kebab";
  53. ResultSet i = db.createStatement().executeQuery (query);
  54.  
  55.  
  56. while(i.next()){
  57. collection.add(readEvent(i));
  58. }
  59. return collection;
  60. }
  61.  
  62. private static Event readEvent(ResultSet rs) throws FileNotFoundException,
  63. SQLException, ClassNotFoundException {
  64. String place;
  65. String headline;
  66. String description;
  67. // String attendence;
  68. int[] dateAndTime;
  69. // String tempdate;
  70. String attendents;
  71.  
  72.  
  73. headline = rs.getString(1);
  74.  
  75. place = rs.getString(2);
  76.  
  77. description = rs.getString(3);
  78.  
  79. dateAndTime = timeAndDate(rs.getString(4));
  80.  
  81. attendents = rs.getString(5);
  82. Event readEvent = new Event(headline, place, description, dateAndTime);
  83. String[] people = attendents.split(";");
  84. for (int i = 0; i < people.length; i++) {
  85. readEvent.addAttendant(people[i]);
  86. }
  87. System.out.println (readEvent.getHeadline());
  88. System.out.println (readEvent.getDescription());
  89. System.out.println (readEvent.getPlace());
  90. System.out.println (readEvent.getDateAndTime());
  91. System.out.println (readEvent.getAttendants());
  92. return readEvent;
  93. }
  94.  
  95. private static int[] timeAndDate(String time) {
  96. int[] dateVector = {0,0,0,0,0};
  97. String[] tempArray;
  98. tempArray = time.split(":");
  99.  
  100. dateVector[4] = Integer.parseInt(tempArray[1]);
  101. tempArray = tempArray[0].split(" ");
  102. dateVector[3] = Integer.parseInt(tempArray[1]);
  103. tempArray = tempArray[0].split("-");
  104. dateVector[0] = Integer.parseInt(tempArray[0]);
  105. dateVector[1] = Integer.parseInt(tempArray[1]);
  106. dateVector[2] = Integer.parseInt(tempArray[2]);
  107. return dateVector;
  108. }
  109. }
Add Comment
Please, Sign In to add comment