文本保存到mysql乱码_我从txt文本中读取中文文本然后存到mysql数据库中,中文变成了乱码!-问答-阿里云开发者社区-阿里云...

在尝试从GBK编码的HTML文件中读取中文内容,并将其插入到MySQL数据库中时,出现了中文乱码问题。使用Jsoup解析HTML文件,获取评论内容,然后连接到MySQL数据库进行数据存储。但在执行过程中,数据保存为乱码。可能的原因是字符编码不匹配,需要确保文件读取和数据库操作时的编码设置正确。
摘要由CSDN通过智能技术生成

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值