0
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Animesh Pandey
 */

public class dbConnect{

    jaccardIndex ji;
    public float calcJaccard(String s1, String s2) throws Exception {
        ji = new jaccardIndex();
        float sim = ji.Jaccard(s1, s2);
        System.out.println("calcJaccard");
        return sim;
    }

    @SuppressWarnings("static-access")
    public int getLength(String s) throws Exception {
        ji = new jaccardIndex();
        return ji.countElements(ji.getUniqueTokens(s, " "));
    }

    //squareMatrix sm;
    float [][] square (float [][] adjMat) {
      int size = adjMat.length;
      //System.out.println(size);
      float [][] squaredMat = new float [size][size];
      for(int i = 0; i < size; i++) {
          for(int j = 0; j < size; j++) {
              for(int k = 0; k < size; k++) {
                  squaredMat [i][j] += adjMat [i][k] * adjMat [k][j];
              }
          }
      }
      return squaredMat;
    }

   /* Variables to hold database details */
    private String dbHost = "localhost";
    private String dbPort = "3306";
    private String dbUsername = "root";  // Replace the username where the database table was created.
    private String dbPassword = "";  // Replace the password of the username.
    private String dataBase = "product";
    private String dbString   = null;

   /* Database Connection object */
    private Connection conn = null;

   //Store to DB
    @SuppressWarnings("static-access")
   public void connectToDB() throws Exception
    {
       Class.forName("com.mysql.jdbc.Driver").newInstance();

       // Load the database details into the variables.
       String dbUrl = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dataBase;
       dbString = dbUrl;
       try {
           // Create the database connection, if it is closed.
           if ((conn == null)||conn.isClosed()){
              ArrayList <String> synList = new ArrayList<String>();
              ArrayList <String> titleList = new ArrayList<String>();

              conn = DriverManager.getConnection(dbString, dbUsername, dbPassword);
              Statement st = null;
              ResultSet rs = null;
              st = conn.createStatement();
              String query = "select * from books";
              rs = st.executeQuery(query);
              while (rs.next()) {
                  String title = rs.getString(2);
                  String synopsis = rs.getString(3);
                  titleList.add(title);
                  synList.add(synopsis);
              }
              int rowCount = rs.getRow();

              float [][] jaccardMatrix = new float [rowCount][rowCount];

              ***try {
              for (int i = 0; i < synList.size(); ++i){
                  String mainStr = synList.get(i);
                  for (int j = 0; j < synList.size(); ++j){
                      String str = synList.get(j);
                      float similarity = calcJaccard(mainStr,str);
                      if (similarity != 1.0)
                          jaccardMatrix [i][j] = similarity * 5;
                      else
                          jaccardMatrix [i][j] = similarity;
                  }
              }
              } catch(Exception ex) {
                  System.out.println("Problem is Here!");
                  ex.printStackTrace();
              }***
              for (int i = 0; i < synList.size(); ++i) {
                  System.out.println(synList.get(i).length());
              }

              int [] tokenLength = new int [rowCount];
              for (int i = 0; i < rowCount; ++i) {
                  tokenLength [i] = getLength(synList.get(i));
                  System.out.println(tokenLength [i]);
              }
              //ji.countElements(ji.getUniqueTokens(synList.get(0), " "));
              /*---------------------------------------------------------------
                    Defining the threshold for being a neighbour!
               *    Adjacency Matrix
               *
               */
              float thresh = (float) 0.27;
              float ftheta = (1 - thresh)/(1 + thresh);
              float power = 1 + 2*ftheta;

              float [][] adjacencyMatrix = new float [rowCount][rowCount];
              for (int i = 0; i < rowCount; ++i){
                  for (int j = 0; j < rowCount; ++j){
                      if (jaccardMatrix [i][j] > thresh)
                          adjacencyMatrix [i][j] = 1;
                      else
                          adjacencyMatrix [i][j] = 0;
                  }
              }
              /*
               * Finding the Link Matrix
               *
               */
              float [][] linkMatrix = new float [rowCount][rowCount];
              linkMatrix = square(adjacencyMatrix);


              /*for (int i = 0; i < rowCount; ++i){
                  for (int j = 0; j < rowCount; ++j){
                      System.out.print(jaccardMatrix[i][j] + " ");
                  }
                  System.out.println("\n");
              }
              for (int i = 0; i < rowCount; ++i){
                  for (int j = 0; j < rowCount; ++j){
                      System.out.print(adjacencyMatrix[i][j] + " ");
                  }
                  System.out.println("\n");
              }*/
              for (int i = 0; i < rowCount; ++i){
                  for (int j = 0; j < rowCount; ++j){
                      if (i < j)
                          System.out.print(linkMatrix[i][j] + " ");
                      else
                          System.out.print("0.0 ");
                  }
                  System.out.println("\n");
              }
           }
       } catch (SQLException sqlex) {
               // Catch Exceptions and display messages accordingly.
               System.out.println("SQLException while connecting and inserting into " +
                                 "the database table: " + sqlex.toString());
         } catch (Exception ex) {
               System.out.println("Exception while connecting and inserting into the" +
                                  " database table: " + ex.toString());
               ex.printStackTrace();
         } finally {
               // Close the Statement and the connection objects.
               if (conn != null)
                   conn.close();
         }
    }
}

The error coming out to be is :

calcJaccard
Here!
715
826
702
1232
1048
419
815
1206
657
java.lang.ArrayIndexOutOfBoundsException: 0
        at rock.dbConnect.connectToDB(dbConnect.java:98)
        at rock.Main.Connection(Main.java:17)
        at rock.Main.main(Main.java:34)
BUILD SUCCESSFUL (total time: 0 seconds)

All I am sure of is that error is in connectToDB(). That area is in bold here!
Please help!

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

Use print statements to check the values of key variables (eg rowCount) - it loks like maybe rowCount is zero, giving a [0][0] array for which any index value will be out of bounds.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.