Advertisement
lamiastella

sql

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