0

private static boolean insertIntoNewTable() {

    Connection dbConnection = getDBConnection();
    System.out.println("CONNECTED TO DB");
    PreparedStatement preparedStatement = null;
    String countString;
    String fileName;
    String seed;
    ResultSet rs = null;

    BufferedReader br = new BufferedReader(new FileReader("/home/aoblah/Documents/Metaphor-July/Russian/MotherRussia"));
    String line;
    while(null != (line = br.readLine())){
        splitsVille = line.split(":");
        fileName = splitsVille[0].trim();
        seed = splitsVille[1].trim();
        countString = "SELECT COUNT(*) FROM metaphor_repository.source_domain_russian_OY2_v3 where filename = ? AND seed = ?" ;
        preparedStatement = dbConnection.prepareStatement(countString);
        preparedStatement.setString(1, fileName);
        preparedStatement.setString(2, seed);

        rs = preparedStatement.executeQuery();
        if (rs.next()) {
            int numberOfRows = rs.getInt(1);
            System.out.println("numberOfRows= " + numberOfRows);
        } else {
            System.out.println("error: could not get the record counts");
        }


    }

    rs.close();
    preparedStatement.close();
    dbConnection.close();

    return true;        
}

I need the row count to be generated by this code but all I get are zeroes. When I execute the same query in MySQL workbench I get correct answer. Please help me find the problem.

I figure out what the problem is. The second columen in the where clause contains Russian characters and they showed up as "?????" when I printed it out. They printout fine in the console when I print them out separately.

dirdir69
  • 47
  • 7
  • possible duplicate of [Java PreparedStatement UTF-8 character problem](http://stackoverflow.com/questions/3828818/java-preparedstatement-utf-8-character-problem) – Gord Thompson Aug 04 '14 at 20:53

2 Answers2

2

You need to flush the results. The jdbc is buffering the insertions and flushing them before closing the connection. After inserting use dbConnection.commit();

Finally the problem was the encoding in the database connection and was solved by setting UTF-8 in the connection: jdbc:mysql://server/database?characterEncoding=UTF-8 as suggested in Java PreparedStatement UTF-8 character problem

Community
  • 1
  • 1
Troveldom
  • 356
  • 1
  • 10
  • The problem lies before that. When I print the preparedStatement, the second column shows up as ???? instead of Russian Characters – dirdir69 Aug 04 '14 at 19:00
  • 1
    Sorry, didn't see that. Have you tried this -> http://stackoverflow.com/questions/3828818/java-preparedstatement-utf-8-character-problem – Troveldom Aug 04 '14 at 19:04
0

My first thought is that the values you are assigning to your fileName and seed variables don't create a WHERE clause that evaluates to true for any rows in your table. I would throw in a println to display those values and confirm that they are what you expect them to be. Also, some databases can be case sensitive in WHERE clauses so you may have to account for that.

BruceRudd
  • 126
  • 5
  • I figured out what the problem is but I don't know how to fix it. The second "?" seed contains Russian characters and when I printed the "prepraedStatement" out they showed up as ?????. – dirdir69 Aug 04 '14 at 18:45