每当我运行此查询时,我都会收到语法错误说"遇到"年"在第1行。我的列名是YEAR,由于某种原因它不会运行,尽管所有其他列都可以正常工作。
public void getYears() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection(url,user,pWord);
Statement st = conn.createStatement();
rs = st.executeQuery("SELECT YEAR FROM VEHICLES");
while(rs.next()) {
jComboBox1.addItem(rs.getString("YEAR"));
}
conn.close();
} catch (SQLException | ClassNotFoundException ex) {
Logger.getLogger(VehicleSearch.class.getName()).log(Level.SEVERE, null, ex);
}
}
我的其他列的代码相同,运行时运行正常,VARCHAR(15)中的YEAR列
答案 0 :(得分:4)
"年份"是reserved word in Apache Derby。要将其用作列名,必须通过用双引号("
)包围它来对其进行转义:
rs = st.executeQuery("SELECT \"YEAR\" FROM VEHICLES");
// Here ---------------------^-----^
答案 1 :(得分:0)
您可以使用单引号('
):
rs = st.executeQuery("SELECT 'YEAR' FROM VEHICLES");