我有一個程序,從數據庫中選擇一個表和列字符串。SQLite查詢參數不工作在Java
public void selectAllFrom(String table, String column){
String sql = "SELECT ? FROM ?";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)){
pstmt.setString(1, column);
pstmt.setString(2, table);
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
System.out.println(rs.getString(column));
}
} catch (SQLException e){
System.out.println(" select didn't work");
System.out.println(e.getMessage());
}
}
出於某種原因,它不工作,這是正確的要抓
下面是connect()函數,以及:
private Connection connect(){
Connection conn = null;
// SQLite connection string
String url = "jdbc:sqlite:C:/sqlite/db/chinook.db";
try{
// creates connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established");
} catch (SQLException e){
System.out.println(e.getMessage());
System.out.println("Connection didn't work");
}
return conn;
}
我知道這個問題是不是與數據庫,因爲我能夠運行其他選擇查詢沒有參數。這是給我的問題的參數。任何人都可以告訴問題是什麼?
好奇...爲什麼你會甚至懶得去通過所有的爛攤子時,它容易得多隻是地方您的變量直接作爲@Gurwinder Singh建議的SQL字符串。 – DevilsHnd