“invalid character value for cast” error on INSERT via UCanAccess

萝らか妹 提交于 2020-01-03 04:33:05

问题


I'm getting the error

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 data exception: invalid character value for cast

when I run this code:

 package aoa;
    import java.sql.*;
    public class Aoa {


        public static void main(String[] args)  {
             Connection cn;
      Statement st;
      ResultSet re;
      String ID ="username"; 
      String NAME="password";  


      try{

        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
        cn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\MUHAMMAD SHAHAB\\STUD1.accdb");
        st = cn.createStatement();
       String q = "INSERT INTO STUD1 ([Id], [Address]) VALUES (?, ?)";
    PreparedStatement pst = cn.prepareStatement (q);
    pst.setString(1, "a");
    pst.setString(2, "b");
    pst.executeUpdate();

        System.out.println("inserted"); }    

        catch(ClassNotFoundException | SQLException e)
        {
          System.out.println(e);
        }

    }       
    }

What am I doing wrong?


回答1:


You will get the error

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 data exception: invalid character value for cast

if you try to assign a value to a numeric column via setString when the string value cannot be cast to a number. In your case, the [Id] column is almost certainly numeric, but

pst.setString(1, "a");

is trying to assign the value "a" to that column, and "a" cannot be converted to a number.



来源:https://stackoverflow.com/questions/38987096/invalid-character-value-for-cast-error-on-insert-via-ucanaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!