I have a sequence of string that vary a lot in length: from 30 to 282420 chars . I want to store each string into a postgres database using jdbc.
String query = "INSERT INTO " + tableName +
" (cbsID, ownerID, naam, soortRegio, jaar, centroidLat, centroidLon, borders) " +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement ps = connection.prepareStatement(query);
for (Marker marker: gemeenteMarkers)
{
Area gemeente = (Area) marker.getProperty("area");
String sloc = "";
// ... do some computations
// Compute sloc, a string that may contain over 250.000 characters
ps.setInt (1, gemeente.getAreaId ());
ps.setInt (2, gemeente.getParentId ());
ps.setString (3, gemeente.getAreaName ());
ps.setString (4, "GM");
ps.setInt (5, 2014);
ps.setFloat (6, gemeente.getCentroid ().getLat ());
ps.setFloat (7, gemeente.getCentroid ().getLon ());
ps.setString (8, slocs);
System.out.printf ("%s - %d locations, len = %d\n", gemeente.getAreaName (), locs.size (), slocs.length ());
ps.addBatch ();
} // for
int [] affected = ps.executeBatch ();
The information is stored in the database all right except for slocs
, which contains the mentioned string and is stored in the column `borders. See the screenshot of pdAdmin below.
Borders is defined as TEXT. As you can see in most cases it is not stored, except when it is short, typical smaller than 7000 chars. The code contains a print
statement and a part of the output is shown below:
Appingedam - 1649 locations, len = 32980
Bedum - 1210 locations, len = 24200
Bellingwedde - 1500 locations, len = 30000
Ten Boer - 1186 locations, len = 23720
Delfzijl - 16 locations, len = 320
Groningen - 2662 locations, len = 53240
Grootegast - 4843 locations, len = 96860
Haren - 1940 locations, len = 38800
Hoogezand-Sappemeer - 1481 locations, len = 29620
Leek - 2575 locations, len = 51500
Loppersum - 2991 locations, len = 59820
Marum - 1936 locations, len = 38720
Almere - 213 locations, len = 4260
Stadskanaal - 2701 locations, len = 54020
Slochteren - 1555 locations, len = 31100
Veendam - 1098 locations, len = 21960
Vlagtwedde - 2621 locations, len = 52420
Zeewolde - 25 locations, len = 500
Winsum - 2992 locations, len = 59840
Zuidhorn - 5282 locations, len = 105640
Dongeradeel - 256 locations, len = 5120
Achtkarspelen - 4644 locations, len = 92880
Ameland - 158 locations, len = 3160
het Bildt - 2337 locations, len = 46740
Franekeradeel - 50 locations, len = 1000
Harlingen - 50 locations, len = 1000
Heerenveen - 5195 locations, len = 103900
In the postgres manual I found
If you desire to store long strings with no specific upper limit, use text or character varying without a length specifier, rather than making up an arbitrary length limit.
There appears to be some length limit though as only the short texts show up in the pgAdmin screen. Does anyone have any idea what I am doing wrong?
Edit 1
Sorry for being not clear. I have added to the code example and I have included output from the program and from pgAdmin to show the data in the table.
Edit 2
I have added an executeBatch
to the example. It was present in the original code, the output does not change.