简体   繁体   English

使用JDBC的JAVA中的SQL语法

[英]SQL Syntax in JAVA using JDBC

Statement stmt = null;

    String query = "SELECT CarLot.CAR.Make, CarLot.CAR.Model, CarLot.CAR.Year, CarLot.CAR.Sold_Status, CarLot.OWNER.First_Name,CarLot.OWNER.Last_Name"+
            "FROM CarLot.CAR, CarLot.OWNER" +
                    " WHERE CarLot.CAR.Sold_Status = TRUE";

    try {
        con = getConnection();
        stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(query);

         String heading1 = "    Make"; String heading2 = "    Model"; String
         heading3 = "    Year"; String heading4="Sold Status";
         System.out.printf("%-20s %-20s %-20s %-20s\n", heading1, heading2,
         heading3,heading4);
         System.out.println("--------------------------------------------------------------------------\n");

        while (rs.next()) {

            String make = rs.getString("car.Make");
            String model = rs.getString("car.Model");
            int carYear = rs.getInt("car.Year");
            boolean soldStatus = rs.getBoolean("car.Sold_Status");
            String firstName = rs.getString("owner.First_Name");

            System.out.printf("%-2s %-20s %-20s %-20s %-20s\n", 
              make, model, carYear,soldStatus,firstName);

             }
        System.out.println("\n\n");
    } finally {
        stmt.close();

    }

The problem that I am having is a SQL syntax error, the syntax works in MySql workbench 6.0 but wont work in my Java App through JDBC, Im new to SQl and JDBC, I realize my result set may not be 100% for this query and that also may be the problem, its from a shared method, im more concerned with the SQL query statement in this case. 我遇到的问题是SQL语法错误,该语法在MySql Workbench 6.0中有效,但在Java应用中无法通过JDBC使用,对于SQl和JDBC来说我是新手,我意识到此查询的结果集可能不是100%,这也可能是问题,它来自共享方法,在这种情况下,我更关心SQL查询语句。

The problem is you are missing a space between the last selected column name and FROM . 问题是您在最后选择的列名和FROM之间缺少空格。 To paraphrase your query, you have: 为了解释您的查询,您需要:

String query = "SELECT ..., CarLot.OWNER.Last_Name"+ // oops, no space!
        "FROM CarLot.CAR, CarLot.OWNER" +
         " WHERE CarLot.CAR.Sold_Status = TRUE";

This error is very common, because it's hard to see a missing space at the end of a line, but there is a code style that I use to combat this - I put spaces at the start of the line, like this: 这个错误是很常见的,因为它很难看到一个失踪的空间在一行的末尾,但我用来对付这样的代码风格-我把空间在该行的开始 ,就像这样:

String query = "SELECT CarLot.CAR.Make, CarLot.CAR.Model, CarLot.CAR.Year, CarLot.CAR.Sold_Status, CarLot.OWNER.First_Name,CarLot.OWNER.Last_Name"+
    " FROM CarLot.CAR, CarLot.OWNER" +
    " WHERE CarLot.CAR.Sold_Status = TRUE";

Every line starts with quote-space, ie " ... , so now it's really obvious when a line-broken string is missing a space, and further because SQL is (generally) whitespace insensitive, you can put spaces at the end too without causing any problems. 每行都以双引号开头,即" ... ,因此当断行的字符串缺少空格时,这是很明显的,而且由于SQL(通常)对空格不敏感,因此您也可以在末尾放置空格而不必造成任何问题。

Add a space before the FROM clause FROM子句之前添加一个空格

String query = "SELECT CarLot.CAR.Make, CarLot.CAR.Model, CarLot.CAR.Year, CarLot.CAR.Sold_Status, CarLot.OWNER.First_Name,CarLot.OWNER.Last_Name" +
           " FROM CarLot.CAR, CarLot.OWNER" +
                    " WHERE CarLot.CAR.Sold_Status = TRUE";

Aside: Consider using PreparedStatement to protect against SQL injection attacks 另外:考虑使用PreparedStatement防止SQL注入攻击

The first and second lines of your query don't have a space between them, so you have select ... CarLot.OWNER.Last_NameFROM CarLot ... . 查询的第一行和第二行之间没有空格,因此可以select ... CarLot.OWNER.Last_NameFROM CarLot ...。 Add the space and see what happens. 添加空间,看看会发生什么。

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

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