0

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?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Mcolo
  • 149
  • 2
  • 10
  • maybe `id` is not a string? Also how about posting your complete stacktrace so we can see where the error is being thrown from – Scary Wombat Aug 17 '16 at 02:15
  • ok i am just going to post the complete stacktrace @Scary Wombat – Mcolo Aug 17 '16 at 02:26
  • Please also show the definition of your `STUD1` table – Scary Wombat Aug 17 '16 at 02:27
  • my output screen i just showing me this:**net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 data exception: invalid character value for cast BUILD SUCCESSFUL (total time: 4 seconds) ** @ScaryWombat – Mcolo Aug 17 '16 at 02:53
  • you were right@ScaryWombat Id was not a string so that's why i was getting that thing on my output screen......now it's working thanx for guiding me – Mcolo Aug 17 '16 at 03:38

1 Answers1

4

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.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418