我有一个长度变化很大的字符串序列:从30到282420个字符.我想使用jdbc将每个字符串存储到postgres数据库中.
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 ();
除slocs
之外,所有信息都存储在数据库中,slocs
包含上述字符串,并存储在"borders"列中.请参见下面的pdAdmin屏幕截图.
边框定义为文本.正如您所见,在大多数情况下,它不会被存储,除非它很短,通常小于7000个字符.该代码包含一条print
语句,部分输出如下所示:
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
在我找到的postgres手册中
如果希望存储没有特定上限的长字符串,请使用
虽然pgAdmin屏幕上只显示短文本,但似乎有一些长度限制.有人知道我做错了什么吗?
Edit 1
抱歉我不清楚.我已经添加到代码示例中,并包含了程序和pgAdmin的输出,以在表中显示数据.
Edit 2
我在示例中添加了executeBatch
.它出现在原始代码中,输出不变.