"invalid character value for cast" 通过 UCanAccess 插入时出错
"invalid character value for cast" error on INSERT via UCanAccess
我遇到了错误
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 data exception: invalid character value for cast
当我运行这个代码时:
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);
}
}
}
我做错了什么?
你会得到错误
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 data exception: invalid character value for cast
如果您在字符串值无法转换为数字时尝试通过 setString
将值分配给数字列。在您的情况下,[Id] 列几乎肯定是数字,但是
pst.setString(1, "a");
正在尝试将值 "a" 分配给该列,但 "a" 无法转换为数字。
我遇到了错误
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 data exception: invalid character value for cast
当我运行这个代码时:
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);
}
}
}
我做错了什么?
你会得到错误
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.6 data exception: invalid character value for cast
如果您在字符串值无法转换为数字时尝试通过 setString
将值分配给数字列。在您的情况下,[Id] 列几乎肯定是数字,但是
pst.setString(1, "a");
正在尝试将值 "a" 分配给该列,但 "a" 无法转换为数字。