UCanAccess:来自空间

时间:2017-07-31 20:01:39

标签: java sql jdbc ucanaccess

我正在尝试将数字/文本插入表中,但当程序尝试插入文本时,我收到错误
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.2 unexpected token: EX1 required: )

以下是我的相关代码:

try (Connection conn = DriverManager.getConnection(
   "jdbc:ucanaccess://" + dbPath  
   +   ";newdatabaseversion=V2010"
   )) {
   DatabaseMetaData dmd = conn.getMetaData();
     try (ResultSet rs = dmd.getTables(null, null, tableName, new String[] { "TABLE" })) {
          try (Statement s = conn.createStatement()) {
                ResultSet resultSet;
                String tableName = "Database";
                s.executeUpdate("CREATE TABLE " + tableName +" ( [SUBJID] 
                INTEGER, [PARAMCD] TEXT, [PARAM] TEXT, [AVAL] DOUBLE)");
                String s1="Text Ex1";
                String s2="Text Ex2";
                int a=3;
                double b=2;
                resultSet = s.executeQuery("SELECT * FROM " + tableName);
                s.executeUpdate("INSERT INTO " + tableName
                    + " ( SUBJID, PARAMCD, PARAM, AVAL)"
                    + " VALUES ( "+ a+", " + s1 + ", " + s2 +" , " + b +" )");

在我看来,当出现空格时会导致错误。我已经尝试将s1更改为“TextEx1”,将s2更改为“TextEx2”,这会给我net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::4.0.2 user lacks privilege or object not found: TEXTEX1的错误。

提前感谢任何人提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

你想做这样的事情:

String sql = "INSERT INTO [" + tableName + "] (SUBJID, PARAMCD, PARAM, AVAL) VALUES (?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, a);
ps.setString(2, s1);
ps.setString(3, s2);
ps.setDouble(4, b);
ps.executeUpdate();