【问题标题】:error: unreported exception IOException; must be caught or declared to be thrown jdbcDir.create(); LUCENE INDEXING ^ [closed]错误:未报告的异常IOException;必须被捕获或声明被抛出 jdbcDir.create(); LUCENE 索引 ^ [关闭]
【发布时间】:2013-06-25 07:19:55
【问题描述】:

好吧,我正在尝试使用 Lucene 为我的数据库建立索引,这是代码.. 我只收到 1 个错误,无法弄清楚它是什么.. 这是我收到的错误“kel.java: 45:错误:未报告的异常IOException;必须被捕获或声明为抛出 jdbcDir.create();"

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

import org.apache.lucene.store.jdbc.JdbcDirectory;

import org.apache.lucene.store.jdbc.dialect.MySQLDialect;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.io.IOException;

public class kel
  {

    public static void main(String[] args)
    {
  //code snippet to create index  
   MysqlDataSource dataSource = new MysqlDataSource();

   dataSource.setUser("root");

   dataSource.setPassword("n");

   dataSource.setDatabaseName("lol");

    dataSource.setEmulateLocators(true); //This is important because we are dealing with a blob type data field  

   JdbcDirectory jdbcDir = new JdbcDirectory(dataSource, new MySQLDialect(), "ii");


   jdbcDir.create();


   }

}

【问题讨论】:

    标签: java mysql jdbc lucene


    【解决方案1】:

    IOExceptionchecked exception。这意味着它必须被捕获,使用 try-catch 异常或使用 throws 语句抛出。

    【讨论】:

      【解决方案2】:

      您必须将jdbcDir.create() 行包含在try-catch 块内,

      try {
         jdbcDir.create();
      }
      catch(IOException ioe){
        ioe.printStackTrace();
        logger.error(ioe.getMessage(),ioe); // if logger is implemented
      }
      

      或者让你的main(String[] args)IOException。(不建议)

      public static void main(String[] args) throws IOException
      

      查看JdbcDirectory#create()方法的文档:

      投掷:

      IO异常

      由于该方法抛出 checked exception ,因此您需要通过将潜在的异常抛出行包含在 try-catchinform 代码中来处理它通过在方法声明中添加 throws 来调用包含潜在异常抛​​出行的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多