Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>finally, i proceeded like this as you adviced. I only put the parts of the code which i think is more related to the question :)</p> <p>`</p> <pre><code>private final String createTableString = "" + "CREATE TABLE IF NOT EXISTS " + _tableName + " (" + "id INT UNSIGNED NOT NULL AUTO_INCREMENT, " + "country VARCHAR(3) NOT NULL, " + "city VARCHAR(40) NOT NULL, " + "region VARCHAR (5) NOT NULL, " + "population FLOAT NOT NULL, " + "latitude FLOAT NOT NULL, " + "longitude FLOAT NOT NULL, " + "PRIMARY KEY(id)" + " );"; private final String insertString = "" + "INSERT INTO " + _tableName + " (country, city, region, population, latitude, longitude) " + "VALUES (?,?,?,?,?,?)" + ";"; public void go() throws IOException, SQLException { loadDriver(); Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", ""); connectionProps.put("password", ""); String connectionURL = _protocol + _subprotocol + _dbName + _dbSettings; ResultSet rs = null; try { conn = DriverManager.getConnection(connectionURL, connectionProps); logger.info("Connected to {} database.", _dbName); conn.setAutoCommit(false); Savepoint savept1 = conn.setSavepoint(); Statement stmt = conn.createStatement(); try { stmt.execute(createTableString); logger.info("The table '{}' created successfully", _tableName); } catch (SQLException sqle) { logger.error("Error while creating the table '{}'", _tableName); printSQLException(sqle); } PreparedStatement pstmt = conn.prepareStatement(insertString); _allStatements.add(pstmt); /* rs: pstmt: * 1 -&gt; COUNTRY * 2 -&gt; CITY 1 -&gt; COUNTRY * 3 -&gt; ACCENTCITY 2 -&gt; CITY * 4 -&gt; REGION 3 -&gt; REGION * 5 -&gt; POPULATION 4 -&gt; POPULATION * 6 -&gt; LATITUDE 5 -&gt; LATITUDE * 7 -&gt; LONGITUDE 6 -&gt; LONGITUDE */ rs = Csv.getInstance().read(_csvFileName, null, _csvCharset); int rowCount = 0; while (rs.next()) { if (rs.getFloat(5) != 0) { // If population is not null. pstmt.setString(1, rs.getString(1)); // country pstmt.setString(2, rs.getString(3)); // city (accentcity in reality) pstmt.setString(3, rs.getString(4)); // region pstmt.setFloat(4, rs.getFloat(5)); // population pstmt.setFloat(5, rs.getFloat(6)); // latitude pstmt.setFloat(6, rs.getFloat(7)); // longitude pstmt.addBatch(); rowCount ++; } } int[] rowsUpdated = pstmt.executeBatch(); for (int i=0; i&lt;rowsUpdated.length; i++) { if (rowsUpdated[i] == -2) { logger.error("Execution {}: unknown number of rows inserted.", i); logger.error("Rolling back ..."); conn.rollback(savept1); } else { logger.trace("Successful: execution {}, {} rows updated !", i, rowsUpdated[i]); } } conn.commit(); } finally { // release all open resources to avoid unnecessary memory usage. </code></pre> <p>....`</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload