獲得特殊字符,如(其他一些特殊字符也可以返回成功,然而,這種情況下,尚未進行分析)的方式:
- A,E,I,O,U, A,E,I,O,U,U,U,N,N-
,正確格式化所有返回的字符串(我指的是查詢字段)下面的過程必須遵循的。
連接到數據庫之前是東德和查詢數據庫之前,我們必須定義一個屬性參數,如字符集:
Properties props;
props = new Properties();
props.put ("charSet", "iso-8859-1");
然後,我們需要的道具添加到數據庫的連接。最後,這是您需要的最終代碼。
String dbPath = "C:/example/directory/myDatabase.accdb";
Properties props;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connURL="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+dbPath;
try {
props = new Properties();
props.put ("charSet", "iso-8859-1");
con = DriverManager.getConnection(connURL, props);
stmt = con.createStatement();
stmt.execute("select "field+" from "+tableName); // query exec
rs = stmt.getResultSet(); // query resultset
rsMetaData = rs.getMetaData(); // resultset metadata
} catch (SQLException ex) {
return false;
}
然後你只需要返回每個字段的值作爲一個字符串(記住存盤的字符串中的輔助變量,如果你想使用它不止一次,因爲你可以運行rs.getString()
一次):
String resultString;
if(rs != null){
while(rs.next()){ // this while may be surrounded with a try-catch
// Fields will be displayed for every row in the DB
// indexField must start from 1 !
for (int indexField = 1; indexField<=rsMetaData.getColumnCount(); indexField++){
resultString = rs.getString(field_index);
System.out.println(This is the field of column number "+indexField+": "+resultString);
} // for close
} // while close
} // if close