Guest User

Untitled

a guest
Mar 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Collections;
  8. import java.util.Date;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12.  
  13. public class CrowdSource_Database {
  14.  private Connection connection;
  15.      
  16.      public CrowdSource_Database() throws SQLException {
  17.          try {
  18.             Class.forName("org.hsqldb.jdbcDriver");
  19.         } catch (ClassNotFoundException e) {
  20.             e.printStackTrace();
  21.         }
  22.             connection = DriverManager.getConnection("jdbc:hsqldb:file:"
  23.             + "/Users/Aamer/Documents/Java/CS_DB","SA","");
  24.            
  25.             Statement delayStmt = connection.createStatement();
  26.             try {delayStmt.execute("SET WRITE_DELAY FALSE");}  //Always update data on disk
  27.             finally {delayStmt.close();}
  28.            
  29.             Statement sqlStmt = connection.createStatement();
  30.             try {
  31.              sqlStmt.execute("CREATE TABLE messages(contact VARCHAR(255) NOT NULL,"+
  32.                              "message VARCHAR(4096) NOT NULL,timeposted BIGINT NOT NULL ,up INT NOT NULL DEFAULT 0, down INT NOT NULL DEFAULT 0, score LONG NOT NULL DEFAULT 0 )");
  33.             } catch (SQLException e) {
  34.              System.out.println("Warning: Database table \"messages\" already exists.");
  35.             } finally {
  36.              sqlStmt.close();
  37.              connection.commit();
  38.             }
  39.            
  40.        
  41.      }
  42.      
  43.      
  44.      public void close() throws SQLException {
  45.          connection.close();
  46.      }
  47.      
  48.      public void incrementUp(String contact_ ) throws SQLException {
  49.          String stmt = "UPDATE messages SET up = up+1 WHERE contact= contact_";
  50.             Statement updatevalue = connection.createStatement();
  51.          
  52.          try{
  53.              updatevalue.execute(stmt);
  54.         } finally {
  55.              updatevalue.close();
  56.              connection.commit();
  57.         }
  58.      }
  59.      
  60.      public void reset(){
  61.          try {
  62.                 Statement stmt = connection.createStatement();
  63.  
  64.                
  65.                 String sql = "TRUNCATE messages";
  66.  
  67.                
  68.                 sql = "DELETE FROM messages";
  69.  
  70.                
  71.                 stmt.executeUpdate(sql);
  72.             } catch (SQLException e) {
  73.             }
  74.      }
  75.        
  76.        public void incrementDown(String contact_ ) throws SQLException {
  77.              String stmt = "UPDATE messages SET down = down+1 WHERE contact= contact_";
  78.                 Statement updatevalue = connection.createStatement();
  79.              
  80.              try{
  81.                  updatevalue.execute(stmt);
  82.             } finally {
  83.                  updatevalue.close();
  84.                  connection.commit();
  85.             }
  86.            
  87.            
  88.          
  89.          
  90.      }
  91.        
  92.        public void addMessage(ScamQuery m) throws SQLException {
  93.            
  94.              String stmt = "INSERT INTO MESSAGES(contact,message,timeposted) VALUES (?,?,?)";
  95.               PreparedStatement insertMessage = connection.prepareStatement(stmt);
  96.              
  97.               try {
  98.                insertMessage.setString(1, m.getContact()); //set value of first "?" to "Alastair"
  99.                insertMessage.setString(2, m.getMessage());
  100.                insertMessage.setLong(3, System.currentTimeMillis());
  101.                insertMessage.executeUpdate();
  102.                
  103.               } finally { //Notice use of finally clause here to finish statement
  104.                insertMessage.close();
  105.                connection.commit();
  106.  
  107.               }
  108.  
  109.          }
  110.        
  111.        public List<ScamQuery> getQueries() throws SQLException {
  112.              
  113.              String stmt = "SELECT nick,message,timeposted,up,down,score FROM messages "+
  114.              "ORDER BY timeposted";
  115.  
  116.              List<ScamQuery> store = new LinkedList<ScamQuery>();
  117.              PreparedStatement recentMessages = connection.prepareStatement(stmt);
  118.              try {
  119.                  ResultSet rs = recentMessages.executeQuery();
  120.                  try {
  121.                      while (rs.next())
  122.                          store.add(new ScamQuery(rs.getString(1), rs.getString(2), new Date(rs.getLong(3)), rs.getInt(4), rs.getInt(5), rs.getLong(6) ));
  123.  
  124.                  } finally {
  125.                      rs.close();
  126.                  }
  127.              }
  128.                 finally {
  129.                     recentMessages.close();
  130.                 }
  131.                 Collections.reverse(store);
  132.             return store;
  133.  
  134.              
  135.          }
  136.        
  137.        public long confidence (int up, int down){
  138.            
  139.             int n = up + down;
  140.             if(n==0){
  141.                 return 0;
  142.             }
  143.            
  144.             double prop = up/n;
  145.             double z = 1.96; //95% confidence interval
  146.            
  147.             double confd = (prop + z*z/(2*n) - z * Math.sqrt((prop*(1-prop)+z*z/(4*n))/n))/(1+z*z/n);
  148.             long conf = Math.round(confd);
  149.             return conf;
  150.            
  151.         }
  152.        
  153.        public long calculateScore(String _contact) throws SQLException{
  154.            String _up = "SELECT up FROM messages WHERE contact=_contact";
  155.            String _down = "SELECT down FROM messages WHERE contact=_contact";
  156.            int up;
  157.            int down;
  158.            PreparedStatement upstmt = connection.prepareStatement(_up);
  159.            PreparedStatement downstmt = connection.prepareStatement(_down);
  160.            try{
  161.                ResultSet ups = upstmt.executeQuery();
  162.                ResultSet downs = downstmt.executeQuery();
  163.                try{
  164.                    up = ups.getInt(1);
  165.                    down = downs.getInt(1);
  166.                }
  167.                finally{
  168.                    ups.close();
  169.                    downs.close();
  170.                }
  171.            
  172.            }
  173.            finally{
  174.              
  175.                    upstmt.close();
  176.                    downstmt.close();
  177.                  
  178.            }
  179.            long score = confidence(up, down);
  180.            return score;
  181.            
  182.              
  183.        }
  184.        
  185.        public void updateScore(String _contact) throws SQLException{
  186.            @SuppressWarnings("unused")
  187.         long newScore = calculateScore(_contact);
  188.            
  189.            String stmt = "UPDATE messages SET score = newScore WHERE contact= contact_";
  190.             Statement updatevalue = connection.createStatement();
  191.          
  192.          try{
  193.              updatevalue.execute(stmt);
  194.         } finally {
  195.              updatevalue.close();
  196.              connection.commit();
  197.         }
  198.        }
  199. }
Add Comment
Please, Sign In to add comment