jTDS JDBC驱动程序:getNString()抛出错误 [英] jTDS JDBC Driver: getNString() throws error

查看:371
本文介绍了jTDS JDBC驱动程序:getNString()抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jtDS JDBC驱动程序,当我调用 ResultSet.getNString(index); 时,我得到以下异常:

I am using the jtDS JDBC driver and when I call ResultSet.getNString(index); I get the following exception:

run:
Exception in thread "main" java.lang.AbstractMethodError: net.sourceforge.jtds.jdbc.JtdsResultSet.getNString(I)Ljava/lang/String;
    at javasql.ProductsController.PrePopulate(ProductsController.java:51)
    at javasql.ProductsController.<init>(ProductsController.java:37)
    at javasql.Program.main(Program.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)

当我使用 getString(index)时,它运行正常..只有 getNString()我才遇到问题,我的MSSQL表中的列定义为 NVarChar 。那么,是什么给出的? jtDS不支持 getNString()?也许它通过相同的 getString() VarChar NVarChar c>功能?

When I use getString(index) it works fine.. it's only with getNString() that I am having a problem and the column in my MSSQL table is defined as NVarChar. So, what gives? Does jtDS not support getNString()? Maybe it gets both VarChar and NVarChar via the same getString() function?

编辑:

相关的unicode问题..我可以看到尝试使用Unicode不适用于插入。我的列定义为 nvarchar(255),但以下 INSERT 语句不起作用:

Related unicode problem.. I can see that trying to use Unicode doesn't work for inserting either. My column is defined as nvarchar(255) and yet the following INSERT statement does not work:

private final String INSERT_FORMAT = "INSERT INTO Products (Name, Price, QuantityInStock) VALUES (N'%s', %s, %s)";

    public void Insert(Product p) {
            products.add(p);
            try {
                statement.addBatch(String.format(INSERT_FORMAT, p.getName(), p.getPrice(), p.getQuantityInStock()));
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }

public void SaveChanges() {
        try {
            statement.executeBatch();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        RefreshData();
    }

我输入的unicode字符在数据库中显示为问号。当你忘记在插入语句中将 N 添加到上述参数时,通常会发生这种情况..但是在这里你可以看到我清楚地指定了它。 JtDS的功能列表确实说它支持NVARCHAR,NTEXT等。但目前我只看到问题。我做错了什么?

The unicode characters that I input show up as question marks in the database. This normally happens when you forget to prepend N to the said parameter in the insert statement.. but here you can see that I clearly specified it. JtDS' feature list does say it supports NVARCHAR, NTEXT, etc.. but currently I'm only seeing problems. What am I doing wrong?

编辑2

问题1已解决。我通过SSMS将一些中文字符直接插入到数据库中,当我使用 getString(index)时,它运行正常。所以似乎jTDS对我猜的所有文本类型使用getString。奇怪的是它没有为 getNString(索引)函数抛出某种不支持的异常。

Issue 1 is resolved.. I inserted some Chinese characters directly into the database via SSMS and when I used getString(index), it worked fine. So it seems jTDS uses getString for all text types I guess. Strange then it doesnt throw some kind of Unsupported Exception for the getNString(index) function.

所以,现在我们留下了如何将unicode数据插入 NVARCHAR 列的问题。肯定有人有过这方面的经验吗?

So, now we're left with the problem of how to insert unicode data into an NVARCHAR column. Surely someone here has had experience with this?

编辑3

我改变了我的代码在每个方法中使用预准备语句,而不是尝试使用批处理并使用setInt,setString等方法。但是,我仍然遇到同样的问题...如果我使用setString()进行unicode,它会插入???进入我的数据库。如果我使用setNString()然后我得到了同样的错误,当我在做getNString时,我在这篇文章的顶部收到了...这是疯了......如果它实际上不支持unicode,那么这个驱动程序怎么会变得如此受欢迎??我在这里想念的是什么?该网站确实说它支持NVARCHAR等。所以,为了使这项工作我需要做的是什么疯狂不直观的事情?

I changed my code to use a prepared statement in each method instead of trying to use a batch and used the setInt, setString, etc methods. However, I am still having the same problems... if I use setString() for unicode, it inserts ??? into my db. If I use setNString() then I get the same error that I received at the top of this post when I was doing getNString... This is insane... how could this driver become so popular if it doesn't actually support unicode?? What am I missing here? The site does say it supports NVARCHAR, etc.. so what crazy unintuitive thing is it that I need to do in order to make this work???

推荐答案

getNString 随Java 1.6 / JDBC 4.0一起添加。看起来你的司机太老了。

getNString was added with Java 1.6/JDBC 4.0. Looks like your driver is too old.

这篇关于jTDS JDBC驱动程序:getNString()抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆