简体   繁体   English

我的JDBC查询引起了我的JDBC错误吗?

[英]Is my JDBC error caused by my SQL query?

Currently i'm writing a JDBC application to manage a MySQL database. 目前我正在编写一个JDBC应用程序来管理MySQL数据库。 I have the delete, insert and select methods functioning with the correct queries. 我有删除,插入和选择方法与正确的查询一起运行。 I'm having trouble with the Update method. 我遇到了Update方法的问题。 When using using the following code I receive a MySQL error: 使用以下代码时,我收到MySQL错误:

You have an error in your SQL syntax; 您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near "",Street",Town",City",PostCode",Age",email",RunningFee'false'Where PID=" at line 1... 检查与您的MySQL服务器版本相对应的手册,以便在第1行“”,“街道”,“城镇”,“城市”,“邮政编码”,“年龄”,电子邮件“,”运行Fee'false“,其中PID =”附近使用正确的语法。 ..

private void updateData()
{
    Connection con;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(
                "jdbc:mysql://localhost/snr","root","");

        String sql = "Update participant Set password='"+txtpassword.getText()+"'," +
                "lastName='"+txtlastName.getText()+"',firstName='"+
                txtfirstName.getText()+"',HouseNumber'"+txtHouseNumber.getText()+"',Street'"+txtStreet.getText()+"',Town'"+txtTown.getText()+"',City'"+txtCity.getText()+"',PostCode'"+txtPostCode.getText()+"',Age'"+txtAge.getText()+"',email'"+txtemail.getText()+"',RunningFee'"+cbRunningFee.isSelected()+"' Where PID='"+txtPID.getText()+"'";

        Statement statement = con.createStatement();

        statement.execute(sql);

        createMessageBox("Updated Successfully");

        clearControls();
    }
    catch(Exception e)
    {
        createMessageBox(e.getMessage());
    }
}

Is there something wrong with my SQL query? 我的SQL查询有问题吗?

Yes, your query is wrong. 是的,您的查询错了。 You're missing = on a great big bunch of set column/value pairs. 你错过了=大量set列/值对。

(And please consider using prepared statements and bind variables, SQL injection is just not something you want to be open to.) (并且请考虑使用预准备语句和绑定变量,SQL注入不是您想要开放的东西。)

Not only is your query incorrect, but it may also open you to SQL Interjection Attacks . 您的查询不仅不正确,而且还可能使您打开SQL Interjection Attacks

You need to parameterize your query by replacing the pasted-in values with question marks, preparing the statement, and executing it. 您需要通过将粘贴的值替换为问号,准备语句并执行它来参数化查询。 See the tutorial that I linked. 查看我链接的教程。

Finally, storing a password as plain text is a very, very bad idea. 最后,将密码存储为纯文本是一个非常非常糟糕的主意。

String sql = "UPDATE participant SET "+
    "password=?, lastName=?, firstName=?, HouseNumber=?, Street=?, Town=?, "+
    "City=?,PostCode?,Age=?,email=?,RunningFee=? "+
    "WHERE PID=?";
PreparedStatement upd = con.prepareStatement(sql);
upd.setString(1, txtpassword.getText());
upd.setString(2, txtlastName.getText());
// ... and so on
upd.executeUpdate();
con.commit();

Yes there is something wrong with the query. 是的,查询有问题。 Your way of building query is vulnerable to SQL Injection. 构建查询的方式容易受到SQL注入攻击。 Use Parameterized Queries instead of concatenating text like that. 使用参数化查询而不是像这样连接文本。

Read this article: Preventing SQL Injection in Java 阅读本文: 在Java中防止SQL注入

You are forgetting some = in your query. 您在查询中忘记了一些=

Try 尝试

String sql = "Update participant Set password='"+txtpassword.getText()+"'," +
            "lastName='"+txtlastName.getText()+"',firstName='"+ 
txtfirstName.getText()+"',HouseNumber='"+txtHouseNumber.getText()+"',Street='"+
txtStreet.getText()+"',Town='"+txtTown.getText()+"',City='"+txtCity.getText()+
"',PostCode='"+txtPostCode.getText()+"',Age='"+txtAge.getText()+"',email='"+
txtemail.getText()+"',RunningFee='"+cbRunningFee.isSelected()+
"' Where PID='"+txtPID.getText()+"'";

The error 'you have an error in your SQL syntax' is from the sql server and indicates that yes, you do have an error in your query. 错误“您的SQL语法中有错误”来自sql server,并指出是,您的查询中确实有错误。 In these cases I often find it useful to print the constructed query itself, just to check that it is being constructed correctly. 在这些情况下,我经常发现打印构造的查询本身很有用,只是为了检查它是否正确构造。

In your case I believe the problem is that you are missing a bunch of "="s, you also probably need to escape your single quotes in the java so they are passed through correctly (replace ' with \\'). 在你的情况下,我认为问题是你缺少一堆“=”,你也可能需要在java中转义你的单引号,以便它们正确传递(替换'with \\')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM