2013-06-25 60 views
-2

嗯,我想索引我的数据库使用Lucene,这里的代码..我只是得到1错误,无法弄清楚它是什么..这里是我得到的错误“凯尔。 java:45:错误:未报告的异常IOException;必须被捕获或声明为抛出 jdbcDir.create();“错误:未报告的异常IOException;必须被捕获或声明为抛出jdbcDir.create(); Lucene索引^

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


    } 

} 

回答

0

你必须封闭线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()方法:

Throws:

IOException

由于该方法将引发checked异常,因此你需要处理,在代码被包围try-catch内的潜在异常抛​​出线通知它调用你的方法,其中包含了潜在的异常抛出行代码通过在您的方法声明中添加一个throws。

相关问题