使用JAVA Web爬虫在MYSQL中存储印地语单词

时间:2012-07-21 08:36:06

标签: mysql unicode jdbc hindi

我想在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();
}

当我向conn_url添加“?useUnicode = yes&amp; characterEncoding = UTF-8”时,我会收到以下错误,而这些错误在没有它的情况下就会出现。

  

java.sql.SQLException:不支持的字符编码'UTF-8 / hindi-eng'。   过程开始了   在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)   在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)   在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.ConnectionImpl.loadServerVariables(ConnectionImpl.java:3928)   在com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3473)   在com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)   在com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)   在com.mysql.jdbc.ConnectionImpl。(ConnectionImpl.java:813)   在com.mysql.jdbc.JDBC4Connection。(JDBC4Connection.java:47)   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   在java.lang.reflect.Constructor.newInstance(Constructor.java:525)   在com.mysql.jdbc.Util.handleNewInstance(Util.java:411)   在com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)   在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)   在java.sql.DriverManager.getConnection(DriverManager.java:579)   在java.sql.DriverManager.getConnection(DriverManager.java:221)   在TestDataParsing2.initdb(TestDataParsing2.java:29)   在TestDataParsing2.main(TestDataParsing2.java:239)

     
     

INSERT INTO dict VALUES(2,'a','Art','एक','我买了一支笔。')          显示java.lang.NullPointerException          的 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _          INSERT INTO dict VALUES(3,'aback','Adv','पीछे/हतप्रभ','我对他的粗鲁感到有些吃惊。')          显示java.lang.NullPointerException

1 个答案:

答案 0 :(得分:2)

您未指定数据库连接编码,因此将使用服务器默认编码。看起来服务器未配置为使用UTF-8。

您可以将server encoding设置为UTF-8,也可以在连接上设置characterEncoding属性:

conn = DriverManager.getConnection(url+dbName+"?characterEncoding=UTF-8",userName,password);

请记住,连接URL的语法是“jdbc:mysql:// host:port / database?opt​​ion1 = value1&amp; option2 = value2&amp; ...”。