简体   繁体   中英

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.

The problem is you are missing a space between the last selected column name and 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.

Add a space before the FROM clause

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

The first and second lines of your query don't have a space between them, so you have select ... CarLot.OWNER.Last_NameFROM CarLot ... . Add the space and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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