开发者社区> 问答> 正文

我从txt文本中读取中文文本然后存到mysql数据库中,中文变成了乱码!

public static void JD() throws IOException{
        File input =new File("/Users/huboqin/Downloads/网页1/京东/jd450.html");
        Document doc=Jsoup.parse(input,"GB18030");
        Elements ele=doc.getElementsByClass("comment-content");
        String user=ele.select("dd").first().text();
        try {
            Class.forName("com.mysql.jdbc.Driver"); System.out.println("Success loading Mysql Driver!");
            } catch (Exception e) {
            System.out.print("Error loading Mysql Driver!");
            e.printStackTrace(); }
        Connection conn =null;
        Statement stmt = null;
        try{
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/Test", "root", "");;
            stmt =(Statement) conn.createStatement();
        }
        catch(SQLException e1){
            e1.printStackTrace();
        }
        File file =new File("/Users/huboqin/Documents/数据/京东/450.txt");
        FileInputStream fis =null;
        try{
            fis = new FileInputStream(file);
            InputStreamReader input1 =new InputStreamReader(fis);
            BufferedReader br=new BufferedReader(input1);
            String line=null;
            String sql=null;
            try{
                while((line=br.readLine())!=null){
                    sql="insert into Details(CommentID,MarketID,ProductID,Comments,Stars)values('"+ 1 +"','"+2+"','"+1+"','"+line+"','"+5+"')";
                    stmt.executeUpdate(sql);
                    }
                }
                 
                catch(IOException e){
                    e.printStackTrace();
                }
                catch(SQLException e){
                    e.printStackTrace();
                }
                 
                }
             
            catch(FileNotFoundException e){
                e.printStackTrace();
            }
             
            }

展开
收起
爵霸 2016-06-13 11:13:07 2533 0
1 条回答
写回答
取消 提交回答
  • 修改配置文件中的字符集,或穿件数据库表的时候指定字符集,以及客户端连接的时候指定字符集...

    或者安装的时候注意一下..

    2019-07-17 19:35:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载

相关镜像