例外:UCAExc ::: 4.0.2意外令牌:2017年要求:)

问题描述 投票:0回答:1

当我向microsoft Access插入日期时,它给了我这个错误,为什么?这是什么意思?我确信查询是正确的。这是我的代码:

try {
                final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
                Date date = new Date();
                String a = "#"+sdf.format(date)+"#";
                conn=DriverManager.getConnection(dbURL);
                System.out.println("Connection ok.");
                id = Integer.parseInt(ID.getText());
                String query = "INSERT INTO Patient(ID, FName, Address, Phone, Allergies)\n" + 
                        "VALUES ('"+id+"', '"+ name.getText()+"', '"+ address.getText()+"', '"+phone.getText()+"', '"+allergies.getText()+ "');";
                PreparedStatement stmt = conn.prepareStatement(query);
                stmt.executeUpdate();
                String query2 = "INSERT INTO Visit( PatientID, ArrivalTime, HeartRate, Temprature) "+
                        "VALUES ('"+id+"','"+a+"', '"+heart.getText()+"', '"+temp.getText()+"');";

                stmt = conn.prepareStatement(query2);
                stmt.executeUpdate();
                conn.close();

            }catch(Exception e){
                System.err.println("Exception: "+e.getMessage());
            }
java ms-access ucanaccess
1个回答
0
投票

随着声明

String a = "#"+sdf.format(date)+"#";

你已经在日期字符串周围放了#分隔符。然后你的动态SQL继续将'分隔符放在那里,产生类似的东西

INSERT INTO Visit (PatientID, ArrivalTime, ...) VALUES ('1', '#12/29/2017 06:24:23 PM#', ...);

这是无效的语法。正确的文字语法将是......

INSERT INTO Visit (PatientID, ArrivalTime, ...) VALUES (1, #12/29/2017 06:24:23 PM#, ...);

...但你真的不应该使用动态SQL。您应该使用参数化查询

// test data
int id = 123;
java.util.Date date = new java.util.Date();

String sql = "INSERT INTO Visit (PatientID, ArrivalTime) VALUES (?,?);";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.setTimestamp(2, new java.sql.Timestamp(date.getTime()));
ps.executeUpdate();
© www.soinside.com 2019 - 2024. All rights reserved.