《Sqlite:未實(shí)現(xiàn)驅(qū)動(dòng)和亂碼問(wèn)題解決》要點(diǎn):
本文介紹了Sqlite:未實(shí)現(xiàn)驅(qū)動(dòng)和亂碼問(wèn)題解決,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
1、sqlite默認(rèn)使用UTF-8編碼,從數(shù)據(jù)庫(kù)(UTF-8)查出來(lái)的時(shí)候遍歷ResultSet時(shí)候一定使用:rs.getBytes(columnName),不能使用re.getString(XXX)
public static String resultSetToJson(ResultSet rs) throws SQLException, JSONException, UnsupportedEncodingException {
// json數(shù)組
JSONArray array = new JSONArray();
// 獲取列數(shù)
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// 遍歷ResultSet中的每條數(shù)據(jù)
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));//列的值,有數(shù)據(jù)則轉(zhuǎn)碼
} else {
value = "";//列的值,為空,直接取出去 } jsonObj.put(columnName, value);}
array.add(jsonObj); } return array.toString();}
2、Sqlite報(bào)錯(cuò) not implemented by SQLite JDBC driver,參照下面代碼解決
/**執(zhí)行插入和更改操作,無(wú)返回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();//注意此種形式可以解決報(bào)錯(cuò)
pst.executeBatch(); }}注意此種形式可以解決報(bào)錯(cuò)
/**執(zhí)行查詢操作
public static void selectUser() throws SQLException {Connection connection = SqliteDB.getCon();
PreparedStatement pst = connection.prepareStatement("select comment from user;");
ResultSet resultSet = pst.executeQuery();//后面這個(gè)不能帶sql
while (resultSet.next()) {
String s = resultSet.getString("comment");
}}
歡迎參與《Sqlite:未實(shí)現(xiàn)驅(qū)動(dòng)和亂碼問(wèn)題解決》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/9182.html