- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试通过 java 执行上述查询,但在第 6 行出现以下错误:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND lexicon_lemma = ?)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1446)
我找不到语法问题是什么...有什么建议吗?这是我的查询:
public static void InsertEntry(String lexiconType, String lemma, String lang) throws SQLException {
String selectSQL = "SELECT id FROM lexicons WHERE (lexicon_type = ? AND lexicon_lemma = ?)";
PreparedStatement preparedStatement = connection.prepareStatement(selectSQL);
preparedStatement.setString(1, lexiconType);
preparedStatement.setString(2, lemma);
ResultSet rs = preparedStatement.executeQuery(selectSQL);
int id = -1;
if (rs.next()) {
id = rs.getInt(1);
System.out.println(id);
} else {
String insertSQL = "INSERT INTO lexicons "
+ "(lexicon_type,lexicon_lemma,lexicon_lang) VALUES"
+ "(?,?,?)";
PreparedStatement prepStatement = connection.prepareStatement(insertSQL);
prepStatement.setString(1, lexiconType);
prepStatement.setString(2, lemma);
prepStatement.setString(3, lang);
prepStatement .executeUpdate();
prepStatement.close();
}
}
最佳答案
没有必要将 SQL 语句变量传递给 PreparedStatement
的 executeQuery
。这是您案例中的主要问题。
PreparedStatement preparedStatement = connection.prepareStatement(selectSQL);
preparedStatement.setString(1, lexiconType);
preparedStatement.setString(2, lemma);
// error is in the following statement
ResultSet rs = preparedStatement.executeQuery(selectSQL);
当您重置要执行的sql时,该语句不会被识别为准备好的语句,而是父Statement
的实例,该实例正在寻找引号之间的有效输入对于不存在的刺痛等。因此是语法错误。
更改:
ResultSet rs = preparedStatement.executeQuery(selectSQL);
致:
ResultSet rs = preparedStatement.executeQuery();
它应该可以工作。
引用:
关于java - 线程 "main"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : You have an error in your SQL syntax 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29251602/
我有一个像下面这样的代码。我已经尝试了堆栈中的其他解决方案,但似乎没有任何效果。生成错误的 Java 代码如下所示。 public Connection getConnection() {
我有这段代码: String check="SELECT COUNT(*) as check FROM recordstudent WHERE STUDENT_ID="+T_STUDENT_I
在我的项目中,我编写了一些代码来从数据库表中搜索员工姓名并将其加载到 jcombobox 中。 表名:employee 列名:empid(Primarykey), emp_name, position
我有一个奇怪的问题。当我在以下代码段中执行查询时,它会抛出 MySQLSyntaxErrorException 并显示以下消息: You have an error in your SQL synta
我在这里看到了很多关于我的问题的答案,但找不到解决方案。我正在读取 Excel 文件并存储在 mysql 数据库中。这是我的代码 PreparedStatement sql_statement = (
我正在尝试更新我的 jtable(更改值并按 Enter 键),但出现错误。由于大小原因,错误未完整。我认为其余部分只是 c3p0 池连接工具生成的不相关信息。 假设 起初,我认为这可能是 c3p0
我在 spring 框架的 mapper.xml 中遇到一些问题 失败: check the manual that corresponds to your MySQL server version
这个问题已经有答案了: mysql prepared statement error: MySQLSyntaxErrorException (2 个回答) 已关闭 8 年前。 我花了几个小时研究看起来
我试图从 java 桌面程序在 mysql 中创建一个表,但我收到了 MySqlSyntaxErrorException。 查询是: CREATE TABLE FileXFascia(fila0 In
这是错误: run: Mon Mar 25 05:22:00 SGT 2019 WARN: Establishing SSL connection without server's identity
我使用准备语句编写了 select 语句。每次尝试运行它都会出现此错误。我是怎么克服这个错误的?我的 jdbc 连接器是 mysql-connector-java-5.1.13-bin.jar。我的代
session.getAttribute('loginId')//giving 1 ResultSet rs=st.executeQuery("select * from interest where
String sql = "INSERT INTO order " + "(customerid, pant, shirt, date) " + "VALUES ('" + jTextField
我正在使用 Java 将一些数据插入到 mysql 中。但是将撇号(标点符号)放入数据中(附近的每周跟踪器表明工作增长)它给了我 MySQLSyntaxErrorException。请给我适当的解决方
我对使用 Java 的 MySQL 还很陌生,但是我已经执行了一些成功的 INSERT 查询,但是似乎无法在没有 MySQLSyntaxErrorException 异常的情况下执行 CREATE T
我目前正在使用 hibernate 构建一个基于网络的小工具。我使用 Tomcat v7.0 服务器在 eclipse 中测试该站点。我的数据库在 Linux 上使用 MySQL 5.6 版。 现在,
我看过几个相关的问题及其答案,但这对我的案子没有帮助。 我有一个 Spring boot 应用程序,我在其中使用 Jhipster 创建了实体,另外我自己添加了一些手动变更日志变更集。 我有一个医生实
当我执行以下查询时抛出以下异常 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an er
我的目标是向我创建的现有表中添加一个新行。但是,我收到以下错误。 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have
我有以下 Java 代码: String query = "Select 1 from myTable where name = ? and age = ?"; PreparedStatement s
我是一名优秀的程序员,十分优秀!