【问题标题】:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;
【发布时间】:2016-05-28 11:02:10
【问题描述】:

大家好,我有这个错误消息,我在服务器和客户端之间连接,从 mysql 服务器中选择数据并将其插入 mysql 客户端,但插入语句没有发生

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''context'.login VALUES (1,'Alamal','alamal','alamal','alamal)' 附近使用正确的语法

这是代码..

<%
try{
Connection con1;
Connection con2;
Class.forName("com.mysql.jdbc.Driver");
con1=(Connection)DriverManager.getConnection("jdbc:mysql://192.168.101.1:3306/context","hospital","0000");
PreparedStatement ps1=(PreparedStatement)con1.prepareStatement("SELECT * from hospital");
String str;
ResultSet rs1=ps1.executeQuery();

while(rs1.next()){
con2=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/context","root","");
con2.setAutoCommit(true);
con2.createStatement();
    int id=rs1.getInt("ID");
    String username=rs1.getString("UserName");
String password=rs1.getString("Password");
String hname=rs1.getString("HospitalName");
    String haddress=rs1.getString("HospitalAddress");
    PreparedStatement state= (PreparedStatement)con2.prepareStatement("INSERT INTO 'context'.'login' VALUES(?,?,?,?,?);");
    state.setInt(1, id);
    state.setString(2, username);
    state.setString(3, password);
    state.setString(4, hname);
    state.setString(5, haddress);
    state.executeUpdate();
    con2.close();      
    }
   con1.close();

} catch(Exception ex){
    out.print(ex);
}
%>

【问题讨论】:

  • 'context'.'login' 中删除单引号会发生什么?
  • 出现这条信息 com.mysql.jdbc.exceptions.jdbc4MYSQLSyntaxErrorException: Table 'context.login' doesn't exist
  • 然后让舒尔确实存在。

标签: mysql jsp


【解决方案1】:

你在编写 SQL 查询时犯了一个愚蠢的错误,现在它可以正常工作了。

<%
try{
Connection con1;
Connection con2;
Class.forName("com.mysql.jdbc.Driver");
con1=(Connection)DriverManager.getConnection("jdbc:mysql://192.168.101.1:3306/context","hospital","0000");
PreparedStatement ps1=(PreparedStatement)con1.prepareStatement("SELECT * from hospital");
String str;
ResultSet rs1=ps1.executeQuery();

while(rs1.next()){
con2=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/context","root","");
con2.setAutoCommit(true);
con2.createStatement();
    int id=rs1.getInt("ID");
    String username=rs1.getString("UserName");
String password=rs1.getString("Password");
String hname=rs1.getString("HospitalName");
    String haddress=rs1.getString("HospitalAddress");
    PreparedStatement state= (PreparedStatement)con2.prepareStatement("INSERT INTO `context`.`login` VALUES(?,?,?,?,?);");
    state.setInt(1, id);
    state.setString(2, username);
    state.setString(3, password);
    state.setString(4, hname);
    state.setString(5, haddress);
    state.executeUpdate();
    con2.close();      
    }
   con1.close();

} catch(Exception ex){
    out.print(ex);
}
%>

【讨论】:

    【解决方案2】:

    SQL 中的引号 (') 用于表示字符串文字,而不是对象名称。放下它们,你应该会没事的:

    INSERT INTO context.login VALUES(?,?,?,?,?)
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 2021-02-23
      • 2017-02-10
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2012-08-12
      相关资源
      最近更新 更多