Hello!
I'm writing a Java program which inserts lines from a given Text-File into a MySQL Database. My code works fine for smaller text files (< 1KB), however for larger TextFiles (> 1MB), I'm getting the following Runtime Error:
This error typically occurs about 30,000 words in (about 250 KB). I'm not sure why this is happening or how to get around it.
Code below:
My InsertRecord classfile:
This specific program is writing to a Table named "Lexicon", and a Database named "WordGuess".
import java.sql.DriverManager;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import FileManager.MetaTextFile;
public class InsertRecord {
private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:8889/";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
public static void insertRecord(String db, String tbl) throws SQLException, IOException {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
MetaTextFile txt = new MetaTextFile(tbl);
txt.putFileIntoArray(tbl);
String[] FileArray = txt.getArray();
for (int i=0; i<FileArray.length; i++){
String insert_statement = "INSERT INTO " + tbl + " VALUE (NULL, "
+ "'" + FileArray[i] + "'" + ")";
try {
dbConnection = getDBConnection(db);
preparedStatement = dbConnection.prepareStatement(insert_statement);
// execute create SQL statement
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
}
}
private static Connection getDBConnection(String db) {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION + db, DB_USER,DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}