JDBC插入实数数组 [英] JDBC insert real array
问题描述
我正在尝试将一个真实数组插入到postgresql数组中:
I am attempting to insert a real array into a postgresql array:
表定义为:
String sqlTable = "CREATE TABLE IF NOT EXISTS ccmBlock"
+ " sampleId INTEGER,"
+ " block REAL[])";
插入内容是:
String sqlInsert = "INSERT INTO ccmBlock"
+ "(sampleId, block) VALUES"
+ "(?,?)";
PreparedStatement preparedStatement = theConnection.prepareStatement(sqlInsert);
preparedStatement.setInt(1, 1);
Object[] theArray = {.11f, .22f, .33f};
Array a = theConnection.createArrayOf("real", theArray);
preparedStatement.setArray(2, a);
我收到一条消息:org.postgresql.util.PSQLException:无法找到提供的真实名称的服务器阵列类型.
I get a message: org.postgresql.util.PSQLException: Unable to find server array type for provided name real.
但在其文档页面上: http://www.postgresql.org/docs/8.4/static/datatype-numeric.html
表8-2.数值类型
名称StorageSize描述范围
Name StorageSize Description Range
真实 4字节可变精度,不精确的6位十进制数字
real 4 bytes variable-precision, inexact 6 decimal digits precision
推荐答案
Postgresql JDBC驱动程序对类型的命名有自己的想法.您可以在
The Postgresql JDBC driver has it's own idea about the naming of types. You can look them up in the TypeInfoCache class.
在您的情况下,正确的名称是 float4
,因此该行应为:
In your case, the correct name is float4
, so the line would go:
Object[] theArray = {.11f, .22f, .33f};
Array a = theConnection.createArrayOf("float4", theArray);
@JBNizet在一个类似的问题中建议@JBNizet来建议此注册表.
Props goes to @JBNizet for suggesting this registry in a similar question.
这篇关于JDBC插入实数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!