使用uCanAccess驱动程序将字符串发送到MS Access DB时,发生强制转换的无效字符值

时间:2019-02-12 06:09:01

标签: java sql ms-access

我想将名称值存储到数据库中,名称列的数据类型为短文本,但由于强制转换的无效字符值,我总是会收到错误消息。

我已经尝试使用普通的Statement类而不是PreparedStatement,但是遇到了同样的问题。 这是相同的代码

public void actionPerformed(ActionEvent e) {
    System.out.println(cid);
    id = Integer.parseInt(pid.getText());
    name = pname.getText();
    quant = Integer.parseInt(quantity.getText());
    pr = Integer.parseInt(price.getText());
    tx = Integer.parseInt(tax.getText());
    System.out.println(name);
    String query = "Insert into Product values ( ? , ? , ? , ? , ? , ?)";
    try {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        Connection con = DriverManager.getConnection("jdbc:ucanaccess://src\\ERP_System\\Database\\ERPdb.accdb");
        PreparedStatement stmt = con.prepareStatement(query);
        stmt.setInt(1,id);
        stmt.setInt(2,cid);
        System.out.println("Hello");
        stmt.setString(3, name); // error is triggerred by this statement
        System.out.println("Hello2");
        stmt.setInt(3,pr);
        stmt.setInt(4,tx);
        stmt.setInt(5,quant);
        ResultSet rs;
        stmt.execute();    
        System.out.println("Hello3");
    } catch (ClassNotFoundException | SQLException ex) {
        ex.printStackTrace();
    }
}

I keep getting the error as 

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.4 data exception: invalid character value for cast
 at net.ucanaccess.jdbc.UcanaccessPreparedStatement.setString(UcanaccessPreparedStatement.java:742)
 at ERP_System.AddProduct.actionPerformed(AddProduct.java:104)
 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
 at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
.
.
.
.
.
The dots are just to represent more and more lines of errors.



Here are the details for the product table

[1]: https://i.stack.imgur.com/fFN5m.png

1 个答案:

答案 0 :(得分:0)

问题出在我正在使用的查询中。

代替这个

String query = "Insert into Product values ( ? , ? , ? , ? , ? , ?)";

我将其更改为此

String query = "Insert into Product (P_ID,C_ID,P_Name,Price,Tax,Quantity) values ( ? , ? , ? , ? , ? , ?)";

现在,代码可以完美运行了。 谢谢所有试图帮助我的人,非常感谢。

相关问题