Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. /**
  8.  * The class SQLController provides methods to get access to the MySQL database via the installed JDBC connector. With its functions, you can
  9.  * get FractalData objects by ID, check und modify DATETIME-flags or probably repair inconsistent data.
  10.  */
  11.  
  12. public class SQLController {
  13.  
  14.     private Connection connection = null;
  15.     private String sqlJdbcUrl;
  16.     private String sqlUser;
  17.     private String sqlPass;
  18.     /**
  19.      * Constructor
  20.      * sets the private variables to the right values
  21.      */
  22.     SQLController(String sqlJdbcUrl, String sqlUser, String sqlPass) {
  23.         this.sqlJdbcUrl = sqlJdbcUrl;
  24.         this.sqlUser = sqlUser;
  25.         this.sqlPass = sqlPass;
  26.  
  27.     }
  28.    
  29.     /**
  30.      * Destructor
  31.      * just for security, that the connection is def. closed
  32.      */
  33.     protected void finalize() throws Throwable {
  34.         this.connectionClose();
  35.     }
  36.    
  37.     /**
  38.      * Internal function to build up connection to the JDBC adapter
  39.      */
  40.     public void connectionOpen() {
  41.         try {
  42.             Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  43.             this.connection = DriverManager.getConnection (this.sqlJdbcUrl, this.sqlUser, this.sqlPass);
  44.        
  45.         } catch (ClassNotFoundException e) {
  46.             ProtocolHandler.fatal("Klasse nicht gefunden", e);
  47.         } catch (SQLException e) {
  48.             ProtocolHandler.error("SQL Fehler", e);
  49.         } catch (Exception e) {
  50.             ProtocolHandler.error("MySQL - Verbindung konnte nicht hergestellt werden", e);
  51.         }
  52.     }
  53.    
  54.    
  55.     /**
  56.      * Internal function to close connection to the JDBC adapter
  57.      */
  58.     public void connectionClose() {
  59.         try {
  60.             if (this.connection != null) this.connection.close();
  61.         } catch (Exception e) {
  62.             ProtocolHandler.error("MySQL - Verbindung konnte nicht geschlossen werden", e);
  63.         }          
  64.     }
  65.    
  66.     /**
  67.      * Checks for invalid flag combinations (e.g. timeStarted is set, but timeFinalized is not).  This could be the case,
  68.      * if the server get cancelled or crashed during calculation. If the function find such data, it will correct them and
  69.      * set them back to 'accepted', so they will be started again.
  70.      */
  71.     public void doStartupChecks() {
  72.         String queryString;
  73.         try {
  74.             // prepare connection and execute query (search and modify by one query)
  75.             Statement stmt = this.connection.createStatement();
  76.             queryString = "UPDATE jobs SET timeAccepted = NOW(), timeStarted = NULL WHERE timeStarted IS NOT NULL AND timeCancelled IS NULL AND timeFinalized IS NULL";
  77.             stmt.executeUpdate(queryString);
  78.         } catch (Exception e) {
  79.             ProtocolHandler.fatal("Schwerwiegender Fehler bei den MySQL Startup Checks", e);
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * Returns a whole dataset of the given job ID
  85.      * @param jobID
  86.      * @return FractalData object
  87.      */
  88.     public FractalData getJobByID(int p_jobID) {
  89.  
  90.         FractalData data = new FractalData();
  91.         String queryString ="";
  92.  
  93.         try {      
  94.             // prepare connection, execute query and get result
  95.             Statement stmt = this.connection.createStatement();
  96.             queryString = "SELECT * FROM jobs WHERE jobID = " + p_jobID;
  97.             ResultSet rs = stmt.executeQuery(queryString);
  98.             rs.next();
  99.  
  100.             // get fields for the FractalData object
  101.             data.positionX = rs.getDouble("positionX");
  102.             data.positionY = rs.getDouble("positionY");
  103.             data.distanceX = rs.getDouble("distanceX");
  104.             data.distanceY = rs.getDouble("distanceY");
  105.             data.resolutionX = rs.getInt("resolutionX");
  106.             data.resolutionY = rs.getInt("resolutionY");
  107.             data.iterationDepth = rs.getInt("iterationDepth");     
  108.             data.filename = rs.getString("filename");
  109.                    
  110.         } catch (Exception e) {
  111.             ProtocolHandler.error("MySQL Fehler in getJobByID", e);
  112.         }
  113.         // return generated object
  114.         return data;
  115.     }
  116.  
  117.  
  118.     /**
  119.      * Modifies one of the DATETIME-flags in the database
  120.      * @param p_field Column (DATETIME-flag) to modify
  121.      * @param p_jobID Specific job ID
  122.      * @param p_value New value (e.g. NULL or SQL command NOW)
  123.      */
  124.     public void updateStatus(String p_field, int p_jobID, String p_value) {
  125.        
  126.         try {      
  127.             // prepare connection and execute query
  128.             Statement stmt = this.connection.createStatement();
  129.             String queryString = "UPDATE jobs SET " + p_field + "= " + p_value + " WHERE jobID = "+p_jobID;
  130.             stmt.executeUpdate(queryString);
  131.         } catch (Exception e) {
  132.             ProtocolHandler.error("MySQL Fehler in updateStatus", e);
  133.         }
  134.     }
  135.  
  136.     /**
  137.      * Checks if the given status is set
  138.      * @param p_field Column (DATETIME-flag) to check
  139.      * @param p_jobID Specific job ID
  140.      * @return Returns TRUE if the flag is set, otherwise FALSE
  141.      */
  142.     public boolean isStatus(String p_field, int p_jobID) {
  143.         try {
  144.             // prepare connection and execute query        
  145.             Statement stmt = this.connection.createStatement();
  146.             String queryString = "SELECT jobId FROM jobs WHERE jobId = " + p_jobID + " AND " + p_field + " IS NOT NULL";
  147.             ResultSet rs = stmt.executeQuery(queryString);
  148.  
  149.             // if the query delivers a row, return true -- otherwise false
  150.             if (rs.next()) return true;
  151.             else return false;
  152.            
  153.         } catch (Exception e) {
  154.             ProtocolHandler.error("MySQL Fehler in isStatus", e);
  155.         }
  156.         return false;
  157.     }
  158.    
  159.     /**
  160.      * Returns the next undone job
  161.      * @return Returns the next jobId (int), where the no flag is set, except of timeAccepted
  162.      */
  163.     public int getUndoneJob() {
  164.         try {
  165.             // prepare connection and execute query
  166.             Statement stmt = this.connection.createStatement();
  167.             String queryString = "SELECT jobId FROM jobs WHERE timeStarted IS NULL AND timeCancelled IS NULL AND timeFinalized IS NULL";
  168.             ResultSet rs = stmt.executeQuery(queryString);
  169.             // if the query delivers a row, return it
  170.             if (rs.next()) {
  171.                 return rs.getInt("jobID");
  172.             } else {
  173.                 return -1;
  174.             }
  175.         } catch (Exception e) {
  176.             ProtocolHandler.error("MySQL Fehler in getUndoneJobb", e);
  177.         }
  178.         return 0;
  179.     }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement