Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kth.csc.inda.KebabFredag;
- import java.sql.*;
- import java.util.Properties;
- import java.util.TreeSet;
- import java.io.FileNotFoundException;
- import java.util.Collection;
- /**
- * Reads the event from the sqlserver
- * @author Jonas Daniels
- * version 1.0
- *
- */
- public class SQLReader {
- //implements DataReader
- public SQLReader() {
- // connect to the SQL server
- }
- public static void main(String args[]) throws InstantiationException, IllegalAccessException {
- System.out.println ("Main started.");
- try {
- readAllEvents();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static Collection<Event> readAllEvents() throws ClassNotFoundException, SQLException, FileNotFoundException, InstantiationException, IllegalAccessException {
- Collection<Event> collection = new TreeSet<Event>();
- System.out.println ("Start of getDBConnection.");
- Connection db=null;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- System.out.println ("driver.newInstance gotten.");
- //Properties settings = new Properties ();
- //settings.setProperty ("user", System.getProperty ("user.name"));
- String url = "jdbc:mysql://free-mysql.BizHostNet.com:3306/1303092403";
- String user= "1303092403";
- String password="kebab";
- db = DriverManager.getConnection (url, user, password);
- System.out.println("Database connection established");
- String query = "SELECT * FROM kebab";
- ResultSet i = db.createStatement().executeQuery (query);
- while(i.next()){
- collection.add(readEvent(i));
- }
- return collection;
- }
- private static Event readEvent(ResultSet rs) throws FileNotFoundException,
- SQLException, ClassNotFoundException {
- String place;
- String headline;
- String description;
- // String attendence;
- int[] dateAndTime;
- // String tempdate;
- String attendents;
- headline = rs.getString(1);
- place = rs.getString(2);
- description = rs.getString(3);
- dateAndTime = timeAndDate(rs.getString(4));
- attendents = rs.getString(5);
- Event readEvent = new Event(headline, place, description, dateAndTime);
- String[] people = attendents.split(";");
- for (int i = 0; i < people.length; i++) {
- readEvent.addAttendant(people[i]);
- }
- System.out.println (readEvent.getHeadline());
- System.out.println (readEvent.getDescription());
- System.out.println (readEvent.getPlace());
- System.out.println (readEvent.getDateAndTime());
- System.out.println (readEvent.getAttendants());
- return readEvent;
- }
- private static int[] timeAndDate(String time) {
- int[] dateVector = {0,0,0,0,0};
- String[] tempArray;
- tempArray = time.split(":");
- dateVector[4] = Integer.parseInt(tempArray[1]);
- tempArray = tempArray[0].split(" ");
- dateVector[3] = Integer.parseInt(tempArray[1]);
- tempArray = tempArray[0].split("-");
- dateVector[0] = Integer.parseInt(tempArray[0]);
- dateVector[1] = Integer.parseInt(tempArray[1]);
- dateVector[2] = Integer.parseInt(tempArray[2]);
- return dateVector;
- }
- }
Add Comment
Please, Sign In to add comment