Advertisement
lamiastella

raw_data

Jul 7th, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.45 KB | None | 0 0
  1. import java.util.*;
  2. import java.sql.*;
  3.  
  4. public class RawData {
  5.         private int numberOfLocs = -1;
  6.         private int numberOfRows = -1;
  7.        
  8.         private String tablename = " Agnes1 ";
  9. //      private String tablename = " Data ";
  10. //      private String tablename = " Visitors ";
  11.  
  12.        
  13.             public String[] getNodeNames(String restriction, int classNumber, String dateRestriction) {
  14.             Connection C = getConnection();
  15.             String[] names = null;
  16.            
  17.  
  18.             try {
  19.                 Statement Stmt = C.createStatement(
  20.                       ResultSet.TYPE_SCROLL_INSENSITIVE,
  21.                       ResultSet.CONCUR_UPDATABLE
  22.                   );
  23.                 Stmt.executeUpdate("CREATE TABLE #temp (loc VARCHAR(30))");
  24.                 String s = "WHERE class='" + classNumber + "'";
  25.                 if (!restriction.equals("")) {
  26.                     s += " AND student like '" + restriction + "'";
  27.                 }
  28.                 if (!dateRestriction.equals("")) {
  29.                     s += " AND timedate='" + dateRestriction + "'";
  30.                 }
  31.  
  32.                 Stmt.executeUpdate("INSERT INTO #temp SELECT DISTINCT fromLoc FROM Data " + s);
  33.                 Stmt.executeUpdate("INSERT INTO #temp SELECT DISTINCT toLoc FROM   Data " + s);
  34.  
  35.                 ResultSet RS = Stmt.executeQuery("SELECT DISTINCT loc FROM #temp ORDER BY loc");               
  36.  
  37.                 RS.last();
  38.    
  39.                 int n = RS.getRow();
  40.                 RS.beforeFirst();
  41.                
  42.                 if (n < 1) return null;
  43.                
  44.                 names = new String[n];
  45.                 int i = 0;
  46.                 while (RS.next()) {
  47.                     names[i++] = RS.getString("loc");
  48.                 }
  49.             }
  50.             catch (SQLException E) {
  51.                 System.out.println("SQLException: " + E.getMessage());
  52.                 System.out.println("SQLState:     " + E.getSQLState());
  53.                 System.out.println("VendorError:  " + E.getErrorCode());
  54.             }
  55.                
  56.             return names;
  57.         }
  58.  
  59.         public String[] getStudentNames(String restriction, int classNumber, String dateRestriction) {
  60.             Connection C = getConnection();
  61.             String[] names = null;
  62.            
  63.             try {
  64.                 Statement Stmt = C.createStatement(                      
  65.                        ResultSet.TYPE_SCROLL_INSENSITIVE,
  66.                        ResultSet.CONCUR_UPDATABLE
  67. );
  68.  
  69.                 Stmt.executeUpdate("CREATE TABLE #temp (student VARCHAR(30))");
  70.  
  71.                 String s = "WHERE class='" + classNumber + "'";
  72.                 if (!restriction.equals("")) {
  73.                     s += " AND student like '" + restriction + "'";
  74.                 }
  75.                 if (!dateRestriction.equals("")) {
  76.                     s += " AND timedate='" + dateRestriction + "'";
  77.                 }
  78.  
  79.                 Stmt.executeUpdate("INSERT INTO #temp SELECT DISTINCT student FROM Data " + s);
  80.  
  81.                 ResultSet RS2 = Stmt.executeQuery("SELECT DISTINCT student FROM #temp ORDER BY student");
  82.  
  83.                 RS2.last();
  84.                 int n = RS2.getRow();
  85.                 System.out.print("n has " + n + " students");
  86.  
  87.                 RS2.beforeFirst();
  88.                
  89.                 if (n < 1) return null;
  90.                
  91.                 names = new String[n];
  92.                 int i = 0;
  93.                 while (RS2.next()) {
  94.                     names[i++] = RS2.getString("student");
  95.                 }
  96.  
  97.             }
  98.             catch (SQLException E) {
  99.                 System.out.println("SQLException: " + E.getMessage());
  100.                 System.out.println("SQLState:     " + E.getSQLState());
  101.                 System.out.println("VendorError:  " + E.getErrorCode());
  102.             }
  103.                            
  104.             return names;
  105.         }
  106.  
  107.         public double[][] getTransitionMatrix(int classNumber, String dateRestriction, String studentName, Hashtable invNodeNames) {
  108.             int n = invNodeNames.size();
  109.             double[][] matrix = new double[n][n];
  110.  
  111.             Connection C = getConnection();
  112.            
  113.             try {
  114.                 Statement Stmt = C.createStatement(      
  115.                     ResultSet.TYPE_SCROLL_INSENSITIVE,
  116.                     ResultSet.CONCUR_UPDATABLE
  117.                  );
  118.  
  119.                 String s;
  120.                 s =  "SELECT fromLoc,toLoc ";
  121.                 s += "FROM data ";
  122.                 s += "WHERE class='" + classNumber + "' ";
  123.                 s += "  AND student like '" + studentName + "' ";
  124.                 s += "  AND fromLoc != 'none' ";
  125.                 s += "  AND toLoc != 'none' ";
  126.  
  127.                 if (!dateRestriction.equals("")) {
  128.                     s += " AND timedate='" + dateRestriction + "'";
  129.                 }
  130.            
  131.                 int fromLoc, toLoc;
  132.                 ResultSet RS = Stmt.executeQuery(s);
  133.                 while (RS.next()) {
  134.                     fromLoc = ((Integer)invNodeNames.get(RS.getString("fromLoc").toLowerCase())).intValue();
  135.                     toLoc =   ((Integer)invNodeNames.get(RS.getString("toLoc").toLowerCase())).intValue();
  136.                     matrix[fromLoc][toLoc]++;
  137.                 }
  138.                
  139.                 for (int i = 0; i < n; i++) {
  140.                     for (int j = 0; j < n; j++) {
  141.                         if (matrix[i][j] < .5) matrix[i][j] = -1;
  142.                     }
  143.                 }
  144.                
  145.                
  146.             }
  147.             catch (SQLException E) {
  148.                 System.out.println("SQLException: " + E.getMessage());
  149.                 System.out.println("SQLState:     " + E.getSQLState());
  150.                 System.out.println("VendorError:  " + E.getErrorCode());
  151.             }
  152.            
  153.             return matrix;
  154.         }  
  155.  
  156.  
  157.                
  158.         public ResultSet query(String q) {
  159.             ResultSet RS = null;
  160.            
  161.             Connection C = getConnection();
  162.                
  163.             try {
  164.                 Statement Stmt = C.createStatement(
  165.                       ResultSet.TYPE_SCROLL_INSENSITIVE,
  166.                       ResultSet.CONCUR_UPDATABLE
  167. );
  168.                
  169.                 String s;
  170.                 s =  "CREATE TABLE #temp (";
  171.                 s += "id       INT, ";
  172.                 s += "class    INT, ";
  173.                 s += "student  VARCHAR(30), ";
  174.                 s += "purpose  VARCHAR(50), ";
  175.                 s += "fromLoc  VARCHAR(30), ";
  176.                 s += "toLoc    VARCHAR(30)";
  177.                 s += ")";
  178.  
  179.                 Stmt.executeUpdate(s);
  180.  
  181.                 Stmt.executeUpdate("INSERT INTO #temp " + q);
  182.                
  183.                 Stmt.executeUpdate("CREATE TABLE #temp1 (loc VARCHAR(30))");
  184.  
  185.                 Stmt.executeUpdate("INSERT INTO #temp1 SELECT DISTINCT fromLoc FROM #temp");
  186.                 Stmt.executeUpdate("INSERT INTO #temp1 SELECT DISTINCT toLoc FROM #temp");
  187.  
  188.                 RS = Stmt.executeQuery("SELECT COUNT(DISTINCT loc) AS n FROM #temp1");             
  189.                 if (RS.next()) {
  190.                     numberOfLocs = RS.getInt("n");
  191.                 } else {
  192.                     numberOfLocs = -1;
  193.                 }
  194.                
  195.                 RS = Stmt.executeQuery(q);
  196.                
  197.                 RS.last();
  198.                 numberOfRows = RS.getRow();
  199.                 RS.first();
  200.             }
  201.             catch (SQLException E) {
  202.                 System.out.println("SQLException: " + E.getMessage());
  203.                 System.out.println("SQLState:     " + E.getSQLState());
  204.                 System.out.println("VendorError:  " + E.getErrorCode());
  205.             }
  206.            
  207.             return RS;
  208.         }
  209.         private Connection getConnection() {
  210.             Connection C = null;
  211.             try {
  212.  
  213.             } catch (Exception E) {
  214.                 System.err.println("Unable to load driver.");
  215.                 E.printStackTrace();
  216.             }
  217.             String dbname =   "PathfinderData";
  218.             String username = "root";
  219.             String password = "newpass";
  220.            
  221.             try {
  222.  
  223.                 String DB_URL = "jdbc:mysql://localhost:3306/pathfinderdata";
  224.                 C = DriverManager.getConnection(DB_URL, username, password);
  225.             }
  226.             catch (SQLException E) {
  227.                 System.out.println("SQLException: " + E.getMessage());
  228.                 System.out.println("SQLState:     " + E.getSQLState());
  229.                 System.out.println("VendorError:  " + E.getErrorCode());
  230.             }
  231.             return C;
  232.         }
  233.            
  234.            
  235.         public int getNumberOfLocs() { return numberOfLocs; }
  236.  
  237.         public int getNumberOfRows() { return numberOfRows; }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement