1
我有以下簡單的代碼來連接和查詢與準備語句的數據庫(它只是爲了學習的目的)。JDBC PreparedStatement奇怪的行爲
import java.sql.*;
public class Prepared {
public static void main(String[] args) throws SQLException,Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/customerdb","myuser","luca01123581321");
String query="SELECT id,? FROM person WHERE id>=?";
PreparedStatement prep=conn.prepareStatement(query,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
prep.setString(1,"name");
prep.setInt(2,2);
prep.executeQuery();
ResultSet res=prep.getResultSet();
ResultSetMetaData rsmd=res.getMetaData();
System.out.println("number of columns: "+rsmd.getColumnCount());
while (res.next()) {
int id=res.getInt(1);
String name=res.getString(2);
System.out.println(name);
}
}
}
當我執行查詢,並嘗試打印結果我得到:
number of columns:2
name
name
name
name
換句話說
我想找回在第二列中的值(即「名」),其是SQL類型的VARCHAR。但不是獲取所有記錄的「名稱」的值......那是怎麼回事?