java对sqlite进行插入操作的乱码问题

weixin_38058428 2013-06-28 03:35:34
用java连接sqlite数据库之后,将sql语句转码utf-8之后使用statement.executeUpdate()插入中文是乱码,转码后读取出来也是乱码。但是如果从命令行插入中文条目,在程序中进行转码后可正常显示中文。求教怎么才能在java程序中正常地插入中文到sqlite中....... 代码如下: package manager; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SqliteManager { /** * @param args */ public static void main(String[] args) { String sql = "pragma foreign_keys=on"; String description = null; String source = "insert into sct values(100212,1001,8,'一般')"; try { description = new String(source.getBytes(), "utf8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } SqliteManager manager = new SqliteManager(); manager.init(); Connection con = manager.getConnection(); manager.update(sql, con); manager.update("delete from sct where sid=100212 and cid=1001", con);// 删除记录 manager.update(description, con);// 添加记录 ResultSet set = manager.query("select * from sct", con);// 查询记录 try { while (set.next()) { String sid = new String(set.getBytes("sid"), "GBK"); String cid = new String(set.getBytes("cid"), "GBK"); String assess = new String(set.getBytes("assess"), "GBK"); String desp = new String(set.getBytes("description"), "GBK"); System.out.println("sid: " + sid + "\tcid: " + cid + "\tassess: " + assess + "\tdesp: " + desp); } } catch (Exception e) { e.printStackTrace(); } } public void init() { try { Class.forName("org.sqlite.JDBC"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public Connection getConnection() { Connection con = null; try { con = DriverManager.getConnection("jdbc:sqlite:" + "/e:/sqlite3/assesssp1.db"); } catch (SQLException e) { e.printStackTrace(); } return con; } public void close(Connection con) { try { if (con != null && !con.isClosed()) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } public ResultSet query(String sql, Connection connection) { Statement statement; ResultSet result = null; try { statement = connection.createStatement(); statement.setQueryTimeout(100); result = statement.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return result; } public int update(String sql, Connection connection) { Statement statement; int result = 0; try { statement = connection.createStatement(); statement.setQueryTimeout(100); result = statement.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return result; } }
...全文
18 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复

474

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧