我想在 MySQL 数据库中存储一些印地语单词。为此,我编写了一个网络爬虫。我能够从 HTML 页面成功读取这些单词并将它们显示在 NetBeans 控制台中。但是当我将它们插入 MySQL 时,它们会变为 ?????????。此外,如果我在 PHPMyAdmin 本身中使用 SQL 查询插入相同的单词,它们会被正确存储。
我搜索了很多 Google 和各种论坛,并且在大多数地方都采取了适当的预防措施来处理 Unicode。如果输入 Unicode,我们是否必须在 SQL 语句 (JDBC) 中明确提及?
这是我的整个代码。
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.*;
public class TestDataParsing2 {
public int counter = 1;
private String ID = "";
private String title = "";
private String owner = "";
private String s="";
private Connection conn = null;
private String url = "jdbc:mysql://localhost:3306";
private String dbName = "/hindi-eng";
private String driver = "com.mysql.jdbc.Driver";
private String userName = "root";
private String password = "";
private String TABLE = "dict";
private void initdb(){
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
} catch (Exception e) {
e.printStackTrace();
}
}
private void closedb(){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void process(String content){
try{
BufferedReader reader = new BufferedReader(new StringReader(content));
String text = "";
boolean start1 = false;
boolean start2 = false;
while (( text = reader.readLine()) != null) {
if(text.contains("\"a")) {
System.out.println("______________________________________________________________");
String id = getID(text);
this.ID = id;
String title = getTitle(text);
this.title = title;
String owner = getOwner(text);
this.owner = owner;
start1 = true;
}
if(start1 && start2) {
String s = getS(text);
this.s = s;
counter++;
insert();
start2=false;
start1= false;
}
if(start1) {
start2= true;
}
}
}catch(Exception e){
System.out.println(e);
}
}
public void insert(){
String insertString = "INSERT INTO " + TABLE + " VALUES (" + this.counter + ",'" +
this.ID + "','" + this.title + "','" + this.owner + "','" + this.s + "')";
System.out.println(insertString);
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(insertString);
stmt.close();
} catch(Exception e) {
System.out.println(e);
}
}
public String getID(String text){
String id = "";
id = text.substring(text.indexOf("\"")+1, text.indexOf("\","));
return id;
}
public String getTitle(String text){
String title = "";
title = text.substring(text.indexOf(",\"")+2, text.indexOf("\",\"1."));
return title;
}
public String getOwner(String text){
try{
String owner = "";
owner = text.substring(text.indexOf("\",\"1.")+5, text.indexOf("\"<br>"));
int i;
for(i=0;i<owner.length();i++) {
String fifthChar = "\u00AE";
int codePoint = owner.codePointAt(i);
}
return owner;
} catch(Exception e) {
System.out.println(e);
System.out.println("eeee");
}
return owner;
}
public String getS(String text){
String s = "";
s = text.substring(0, text.indexOf("<br>"));
return s;
}
public String download(String path) {
String result = "";
try {
URL url = new URL(path);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream in = null;
in = url.openStream();
String content = pipe(in,"utf-8");
result = content;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public String pipe(InputStream in,String charset) throws IOException {
StringBuffer s = new StringBuffer();
if(charset==null||"".equals(charset)){
charset="utf-8";
}
String rLine = null;
BufferedReader bReader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
FileOutputStream("C:\\Research\\MiningSoftwareRepositories\\Traceability-Link-Recovery\\EXPERIMENTS\\BR\\"
+ bugid + ".txt");
while ( (rLine = bReader.readLine()) != null) {
String tmp_rLine = rLine;
s.append(tmp_rLine+"\n");
}
tmp_rLine = null;
}
in.close();
return s.toString();
}
public static void main(String[] args) {
TestDataParsing2 tdp = new TestDataParsing2();
tdp.initdb();
System.out.println("process started");
String urlPath = "file:///C:/Users/Abhinav/Downloads/Compressed/eng-hindi-dict-utf8/sa.htm";
String content = tdp.download(urlPath);
tdp.process(content);
tdp.closedb();
}
此外,当我将 "?useUnicode=yes&characterEncoding=UTF-8" 添加到 conn_url 时,我会收到以下错误,这些错误没有它就不会出现。
java.sql.SQLException:不支持的字符编码“UTF-8/hindi-eng”。进程开始于 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) 的 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) 的 com.mysql.jdbc.SQLError.createSQLException(SQLError.java :974) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) 在 com.mysql.jdbc.StringUtils.getBytes(StringUtils.java:574) 在 com.mysql.jdbc.StringUtils.getBytes(StringUtils. java:719) 在 com.mysql.jdbc.Buffer.writeStringNoNull(Buffer.java:704) 在 com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2573) 在 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl .java:2713) 在 com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2663) 在 com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1599) 在 com.mysql.jdbc。
INSERT INTO dict VALUES (2,' a ' , ' Art ' , ' एक',' 我买了一支笔。' ) _ __ _ __ _ INSERT INTO dict VALUES (3,'aback','Adv','पीछे/हतप्रभ','我被他的粗鲁吓了一跳。') java.lang.NullPointerException