《Sqlite:未實現驅動和亂碼問題解決》要點:
本文介紹了Sqlite:未實現驅動和亂碼問題解決,希望對您有用。如果有疑問,可以聯系我們。
1、sqlite默認使用UTF-8編碼,從數據庫(UTF-8)查出來的時候遍歷ResultSet時候一定使用:rs.getBytes(columnName),不能使用re.getString(XXX)
public static String resultSetToJson(ResultSet rs) throws SQLException, JSONException, UnsupportedEncodingException {
// json數組
JSONArray array = new JSONArray();
// 獲取列數
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// 遍歷ResultSet中的每條數據
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// 遍歷每一列
for (int i = 1; i <= columnCount; i++) {
String value = null;
String columnName = metaData.getColumnLabel(i);//列名稱
if (rs.getString(columnName) != null&&!rs.getString(columnName).equals("")) {
value = new String(rs.getBytes(columnName));//列的值,有數據則轉碼
} else {
value = "";//列的值,為空,直接取出去 } jsonObj.put(columnName, value);}
array.add(jsonObj); } return array.toString();}
2、Sqlite報錯 not implemented by SQLite JDBC driver,參照下面代碼解決
/**執行插入和更改操作,無返回ResultSetpublic static void updateUser() throws Exception {
String rs = SqliteDB.GetHtttpLink("http://127.0.0.1:18181/db/upuser");
JSONArray array = JSONArray.parseArray(rs);
for (int i = 0; i < array.size(); i++) {
JSONObject obj = array.getJSONObject(i);
int id = Integer.parseInt(obj.getString("id"));
String sql = "insert into user values(?,?,?,?,?,?,?,?)";
Connection con = SqliteDB.getCon();
PreparedStatement pst = con.prepareStatement(sql);
pst.setInt(1, id);
pst.addBatch();//注意此種形式可以解決報錯
pst.executeBatch(); }}注意此種形式可以解決報錯
/**執行查詢操作
public static void selectUser() throws SQLException {Connection connection = SqliteDB.getCon();
PreparedStatement pst = connection.prepareStatement("select comment from user;");
ResultSet resultSet = pst.executeQuery();//后面這個不能帶sql
while (resultSet.next()) {
String s = resultSet.getString("comment");
}}
歡迎參與《Sqlite:未實現驅動和亂碼問題解決》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/9182.html