Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package Controller;
  7.  
  8. import Model.Action;
  9. import Model.Thought;
  10. import java.sql.Connection;
  11. import java.sql.Date;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.util.ArrayList;
  18. import miniproject.MainScreen;
  19.  
  20. /**
  21.  *
  22.  * @author Ward
  23.  */
  24. public class GTD {
  25.     private ArrayList<Action> actions;
  26.     private ArrayList<Thought> thoughts;
  27.     private Connection con;
  28.     private MainScreen screen;
  29.    
  30.  
  31.     public GTD() throws SQLException{
  32.         actions = new ArrayList<Action>();
  33.         thoughts = new ArrayList<Thought>();
  34.  
  35.         con = DriverManager.getConnection("jdbc:mysql://databases.aii.avans.nl/wlaat_db", "wlaat", "CStP9D6j");
  36.         screen = new MainScreen(this);
  37.         getThoughts();
  38.         getActions();
  39.         screen.fillThoughtTable(thoughts);
  40.  
  41.  
  42.     }
  43.  
  44.     public void insertThought(String thought) throws SQLException{
  45.         Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
  46.         ResultSet uprs = stmt.executeQuery("SELECT * FROM thought");
  47.         uprs.moveToInsertRow();
  48.         int i = thoughts.get(thoughts.size()-1).getThoughtID();
  49.         i++;
  50.         uprs.updateInt("thoughtID", i);
  51.         uprs.updateString("thought", thought);
  52.         uprs.insertRow();
  53.         getThoughts();
  54.         screen.fillThoughtTable(thoughts);
  55.  
  56.     }
  57.     public void insertAction(String name, String description, Date date,String context,String project) throws SQLException{
  58.         Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
  59.         ResultSet uprs = stmt.executeQuery("SELECT * FROM action");
  60.         uprs.moveToInsertRow();
  61.         int i = actions.get(actions.size()-1).getActionID();
  62.         i++;
  63.         uprs.updateInt("actionID", i);
  64.         uprs.updateString("name", name);
  65.         uprs.updateString("context", context);
  66.         uprs.updateString("project", project);
  67.         uprs.updateString("description", description);
  68.         uprs.updateDate("date", date);
  69.         uprs.insertRow();
  70.         getActions();
  71.         screen.fillActionTable(actions);
  72.     }
  73.  
  74.      public void deleteThought(int thoughtID) throws SQLException{
  75.         Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
  76.         ResultSet uprs = stmt.executeQuery("SELECT * FROM thought WHERE thoughtID = "+ thoughtID );
  77.         uprs.first();
  78.         uprs.deleteRow();
  79.         getThoughts();
  80.     }
  81.           public void deleteAction(int actionID) throws SQLException{
  82.         Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );
  83.         ResultSet uprs = stmt.executeQuery("SELECT * FROM action WHERE actionID = "+ actionID );
  84.         uprs.first();
  85.         uprs.deleteRow();
  86.         getActions();
  87.     }
  88.  
  89.     public void getThoughts() throws SQLException{
  90.         Statement stmt = null;
  91.         try {
  92.             stmt = con.createStatement();
  93.  
  94.  
  95.             ResultSet rs = stmt.executeQuery("SELECT * FROM thought");
  96.  
  97.  
  98.             while (rs.next()) {
  99.  
  100.                 String s = rs.getString(1);
  101.                 int i = rs.getInt(2);
  102.                 thoughts.add(new Thought(s,i));
  103.                 System.out.println(s + i);
  104.  
  105.  
  106.  
  107.             }
  108.         }
  109.             catch(SQLException a){
  110.                 System.out.println(a);
  111.             }
  112.     }
  113.  
  114.     public void getActions() throws SQLException{
  115.         Statement stmt = null;
  116.         try {
  117.             stmt = con.createStatement();
  118.  
  119.             ResultSet rs = stmt.executeQuery("SELECT * FROM action");
  120.  
  121.             while (rs.next()) {
  122.                 String name = rs.getString(1);
  123.                 String desc = rs.getString(2);
  124.                 Date date = rs.getDate(3);
  125.                 String cont = rs.getString(4);
  126.                 String proj = rs.getString(5);
  127.                 int i = rs.getInt(6);
  128.                 actions.add(new Action(name, desc, date, cont, proj, i));
  129.                 System.out.println("" + name + ", " + desc + ", " + date + ", " + cont + ", " + proj + ", " + i);
  130.             }
  131.         }
  132.         catch(SQLException a) {
  133.             System.out.println(a);
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement