java 使用jdbc调用MYSQL 遇到一个诡异的坑,同样的插入语句,总是报语法错误。上代码:
String insertSQL = "insert into " + tablename + "(date, paraname, paravalue) values(?, ?, ?)";
PreparedStatement pstm = conn.prepareStatement(insertSQL);
int rand = 1000000000 + new Random().nextInt(1000000000);
java.sql.Date adate = new java.sql.Date(System.currentTimeMillis() - rand);
pstm.setDate(1, adate);
pstm.setString(2, points.get(0));
pstm.setFloat(3, 1.0F);
pstm.execute(insertSQL);
每次都报告语法错误:
check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?)' at line 1
几经查找,最后才发现,调用格式错误!!应该直接调用execute();不带参数!!!耗费了半天的时间,冤啊!!
所以,记录在案!
博主在使用Java JDBC执行插入MySQL数据库的SQL语句时遇到了一个奇怪的问题,每次都会提示语法错误。经过排查,发现是由于在PreparedStatement的execute方法中误传了SQL字符串导致的。正确做法应该是直接调用execute()方法,不带任何参数。这个小陷阱浪费了博主不少时间,因此他决定记录下来以警示他人。
2689

被折叠的 条评论
为什么被折叠?



