Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串文本中的Informix JDBC、货币和小数分隔符存在问题_Java_Jdbc_Informix_Currency - Fatal编程技术网

Java 字符串文本中的Informix JDBC、货币和小数分隔符存在问题

Java 字符串文本中的Informix JDBC、货币和小数分隔符存在问题,java,jdbc,informix,currency,Java,Jdbc,Informix,Currency,我对使用MONEY数据类型的JDBC应用程序有问题。 当我在货币栏中插入: insert into _money_test (amt) values ('123.45') 我有一个例外: Character to numeric conversion error 使用ODBC驱动程序从本机Windows应用程序执行相同的SQL。 我住在波兰,有波兰的地方和我的国家 数字的小数部分,所以我尝试: insert into _money_test (amt) values ('123,45')

我对使用MONEY数据类型的JDBC应用程序有问题。 当我在货币栏中插入:

insert into _money_test (amt) values ('123.45')
我有一个例外:

Character to numeric conversion error
使用ODBC驱动程序从本机Windows应用程序执行相同的SQL。 我住在波兰,有波兰的地方和我的国家 数字的小数部分,所以我尝试:

insert into _money_test (amt) values ('123,45')
它成功了。 我检查了PreparedStatement中必须使用点分隔符:
123.45
。 当然,我可以使用:

insert into _money_test (amt) values (123.45)
但有些代码是“通用”的,它从csv文件导入数据,将数字输入字符串文字是安全的

如何强制JDBC在文本中使用DBMONEY(或简单的点)

我的工作站是WinXP。 我有ODBC和JDBCInformix客户端,版本为3.50TC5/JC5。 我已将DBMONEY设置为just dot:

DBMONEY=.
编辑:

Jython中的测试代码:

import sys
import traceback
from java.sql import DriverManager
from java.lang import Class

Class.forName("com.informix.jdbc.IfxDriver")

QUERY = "insert into _money_test (amt) values ('123.45')"

def test_money(driver, db_url, usr, passwd):
    try:
        print("\n\n%s\n--------------" % (driver))
        db = DriverManager.getConnection(db_url, usr, passwd)
        c = db.createStatement()
        c.execute("delete from _money_test")
        c.execute(QUERY)
        rs = c.executeQuery("select amt from _money_test")
        while (rs.next()):
            print('[%s]' % (rs.getString(1)))
        rs.close()
        c.close()
        db.close()
    except:
        print("there were errors!")
        s = traceback.format_exc()
        sys.stderr.write("%s\n" % (s))



print(QUERY)
test_money("com.informix.jdbc.IfxDriver", 'jdbc:informix-sqli://169.0.1.225:9088/test:informixserver=ol_225;DB_LOCALE=pl_PL.CP1250;CLIENT_LOCALE=pl_PL.CP1250;charSet=CP1250', 'informix', 'passwd')
test_money("sun.jdbc.odbc.JdbcOdbcDriver", 'jdbc:odbc:test', 'informix', 'passwd')
使用点和逗号运行money literal时的结果:

C:\db_examples>jython ifx_jdbc_money.py
insert into _money_test (amt) values ('123,45')


com.informix.jdbc.IfxDriver
--------------
[123.45]


sun.jdbc.odbc.JdbcOdbcDriver
--------------
there were errors!
Traceback (most recent call last):
    File "ifx_jdbc_money.py", line 16, in test_money
        c.execute(QUERY)
SQLException: java.sql.SQLException: [Informix][Informix ODBC Driver][Informix]Character to numeric conversion error


C:\db_examples>jython ifx_jdbc_money.py
insert into _money_test (amt) values ('123.45')


com.informix.jdbc.IfxDriver
--------------
there were errors!
Traceback (most recent call last):
    File "ifx_jdbc_money.py", line 16, in test_money
        c.execute(QUERY)
SQLException: java.sql.SQLException: Character to numeric conversion error



sun.jdbc.odbc.JdbcOdbcDriver
--------------
[123.45]
报告说:

java.math.BigDecimal货币(p,s)1

因此,您需要使用而不是
java.lang.String
来表示值、设置值和获取值


只需将其作为参数传递,即可将
String
转换为
BigDecimal
。另一种方法是调用
BigDecimal

方法。我使用PreparedStatement解决了这个问题。我认为“字符到数字转换错误”是Informix JDBC驱动程序中的一个错误

在我经常使用的另一个数据库PostgreSQL中,通过本机JDBC驱动程序或JDBC-ODBC桥运行查询并没有什么区别。我发现PostgreSQL不接受数字形式
123.45
。PostgreSQL接受带点的字符串文字,但该点被处理为1000分隔符。唯一正确接受的值是字符串文字,其中逗号分隔小数部分

编辑


它可以通过在服务器端设置
DBMONEY=。
来解决,然后所有连接(ODBC、JDBC)都将使用该设置。

您可以发布用于插入的确切Java代码吗?我刚刚添加了使用ODBC和JDBC驱动程序的Jython代码。如果我知道列的类型,那么我应该使用
123.45
(作为数字,而不是字符串)。但是从csv构建“常规”插入查询导入数据的ODBC版本可以安全地将所有参数作为字符串处理。现在似乎是时候重新组织代码并使用PreparedStatement了。