我编写了一个使用连接和子查询的MySQL查询,最后在最外面的SELECT
语句中获取了所需的列。它类似于:
SELECT X,Y,Z
FROM
(SELECT A,B,C,X
FROM T1
JOIN
(SELECT D,E,F,Y
FROM T2) ALIAS ON CONDITION....
依此类推。现在,我想做Z/Y
,所以我做了
SELECT X,Y,Z,Z/Y
FROM
YADA YADA YADA
这适用于MySQL,但是当我在Java程序中做同样的事情时,它会抛出一个错误,说:
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 'Z/Y'
我如何克服这个问题?
答案 0 :(得分:0)
事实证明,我忘记在倒数第二列添加一个逗号,这导致了问题。感觉就像一个doofus!
答案 1 :(得分:0)
是的,你是。我也是。在光明的一面,至少我要在我的oracle帐户上重置密码。
import java.sql.*;
public class TwoAndTwo {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost/foobar?"
+ "user=root&password=root");
statement = connect.createStatement();
resultSet = statement
.executeQuery("select 2+2 as computation");
resultSet.next();
System.out.println( resultSet.getString("computation"));
} catch (Exception e) { throw e; } finally { close(); }
}
private void close() {
try { if (resultSet != null) { resultSet.close(); }
if (statement != null) { statement.close(); }
if (connect != null) { connect.close(); }
} catch (Exception e) {
e.printStackTrace(); }
}
public static void main(String[] argv) {
TwoAndTwo tnt = new TwoAndTwo();
try { tnt.readDataBase();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
$ javac TwoAndTwo.java
$ java -cp mysql-connector-java-5.1.29-bin.jar:. TwoAndTwo
4