Question

I have one Class ArrayList Named "Sentence". each Sentence contains value "Words". I have to check each words into MySQL database whether it is a stopwords or not. Let say, i have 200 "Sentence" and each Sentence have a various number of "Words".

I've check the "Words" one by one into the database, when it hit over 2500 number of "Words" (sometimes it hits until 3500th Words before the connection fail), it turns Error. But it will be fine if the number of "Words" below 2500.

This is my Query code

public Query() throws SQLException, ClassNotFoundException{
    try{
        cd = new Connect();
        connect = cd.getConnection();
        statement = (Statement) connect.createStatement();

    }catch(SQLException e){
        System.err.println("Database connecting failed");
    }
}

public boolean isStopWords(String word) throws SQLException{

    String query = "SELECT stopwords FROM stopword WHERE stopwords = '"+word+"'";

    rs = statement.executeQuery(query);

    if(!rs.next()) return false;
    else return true;
}

This is my Connection code

public Connect() throws SQLException{
    try{
        Class forName = Class.forName ("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException ex){

    }
    connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "root", null);
}

public Connection getConnection(){
    return connect;
}

This is my method to retrieve the Word from Class Sentence.String[] tempWords is the value of Words in one object Sentence. And the Sentence itself will looping according to the size of the ArrayList Sentence

protected ArrayList<String> eraseStopWord (String[] tempWords) throws SQLException, ClassNotFoundException{
    result = new ArrayList<String>();
    db = new Query();
    for(int i=0;i<tempWords.length;i++){

         if(!db.isStopWords(tempWords[i])){
             result.add(tempWords[i]);
         }   
    }

    return result;

}

Why its fine until it hits approximately 2500 "Word"? (sometimes its fine until 3500th words before the connection fail, it just stable below 2500th of words)

UPDATE : now i know where the connection stopped. It stopped at 153rd sentence looping. so its not depend on the words. stil look after the bug

  • I found the answer. i open the connection in looping, so MySQL take too much connection (152 connection)
Was it helpful?

Solution 3

My fault. In my previous code, i make a connection everytime the Sentence created (db = new Query)

    protected ArrayList<String> eraseStopWord (String[] tempWords) throws SQLException, ClassNotFoundException{
    result = new ArrayList<String>();
    db = new Query();
    for(int i=0;i<tempWords.length;i++){

         if(!db.isStopWords(tempWords[i])){
             result.add(tempWords[i]);
         }   
    }

    return result;

}

so, when it reach the 153rd sentence, it will stopped because of too many connection The solution is write the open connection code before Sentence looping

OTHER TIPS

For words you can use a in clause.

String query = "SELECT stopwords FROM stopword WHERE stopwords in '("+words+")'";

This will reduce the number of hits. Better to make use of a PreparedStatement instead of Statement. This will save you from sql injections.

You need close the latest statement for query execution.

Look at line :

if(!rs.next()) return false;
else return true;

Change with this :

bool returnValue = true;
if (!rs.next()) {
   returnValue = false;
   statement.close();  // it will release the Statement object
}

return returnValue;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top