代码之家  ›  专栏  ›  技术社区  ›  user3452354

UCAExc:::3.0.7意外标记:logDate

  •  0
  • user3452354  · 技术社区  · 9 年前

    大家好。我正在做一个个人项目,需要与ms access 2016数据库交互。我的java应用程序从用户获取数据,这些信息存储在对象[]中。我试图将obj数组的元素插入数据库中的表中。这是我的代码:

    Connection conn = null;
    PreparedStatement pstmnt = null;
    String sql = null;
    ResultSetMetaData md = null;
    Statement stm = null;
    ResultSet rs = null;
    int i = 0;
    String q = "SELECT * from QueryData";
    try{
        conn = DriverManager.getConnection("jdbc:ucanaccess://filePath");
        stm = conn.createStatement();
        rs = stm.executeQuery(q);
        md = rs.getMetaData();
        int count = md.getColumnCount();
        String[] colName = new String[count];
        for (int n = 1; n <= count; n++)
            colName[n-1] = md.getColumnLabel(n);
        while ( i <= data.length) {//data being the object array containing the data to be inserted in db
            query = "INSERT into QueryData ('"+colName[i]+"') VALUES ('"+data[i]+"')";
                //The following code is where I get the exception
            pstmnt = conn.prepareStatement(query);
            //some more code follows..
    

    1 回复  |  直到 9 年前
        1
  •  2
  •   PhillipXT    9 年前

    您将列名用引号括起来,这是不允许的。您可以改为使用方括号(尽管不是必须的,除非字段名中有空格,这是访问允许的)。

    query = "INSERT into QueryData (["+colName[i]+"]) VALUES ('"+data[i]+"')";
    

    你 可以 也需要使用 # 而不是 ' 以限定日期值。过去使用的访问 # 对于日期分隔符,我不确定较新版本是否接受 ' :

    query = "INSERT into QueryData (["+colName[i]+"]) VALUES (#"+data[i]+"#)";