用Lucene连接Mysql数据库代码

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;

import java.sql.*;

public class ReadMysql {

 public static Connection getConnection() throws SQLException ,
  java.lang.ClassNotFoundException{
  String url = “jdbc:mysql://localhost:3306/studentinfo”;
  Class.forName(“com.mysql.jdbc.Driver”);
  String userName = “root”;
  String password = “123456″;
  Connection con = DriverManager.getConnection(url,userName,password);
  return con;
 }

 public static void main(String[] args) {
  try{
   Connection con = getConnection();
   Statement sql = con.createStatement();
   sql.execute(“drop table if exists student”);
   sql.execute(“create table student(id int not null auto_increment,name varchar(250) not null default ‘name’,math int not null default 60,primary key(id))ENGINE=Myisam CHARSET=GBK;”);
   sql.execute(“insert student values(1,’english good moring teacher english,tea,invoid,are,99,100 sun jackson’,’100′)”);
   sql.execute(“insert student values(2,’chinesepyhiscial,wuli,dede’,50)”);
   sql.execute(“insert student values(3,’what is are test desto,target blank’,’65′)”);
   String query = “select * from student”;
   ResultSet result = sql.executeQuery(query);
   System.out.println(“Student表数据如下:”);
   System.out.println(“———————————”);
   System.out.println(“学号”+” “+”姓名”+” “+”数学成绩”);
   System.out.println(“———————————”);

   String number;
   String name;
   String math;
//   File indexDir=new File(“d://luceneIndex”);  
   //Analyzer luceneAnalyzer =new StandardAnalyzer();

//   IndexWriter writer = new IndexWrite(“d://indexnew//”,new StandardAnalyzer(), true);

   File indexDir=new File(“d://indexnew”);
   Analyzer luceneAnalyzer =new StandardAnalyzer();

  try{
   IndexWriter indexWriter =new IndexWriter(indexDir,luceneAnalyzer,true,IndexWriter.MaxFieldLength.UNLIMITED);/*实例一个索引创建器*/

   while(result.next()){

/*========================================*/
    // 增加document到索引去

      // document对象,相当于数据库中一条记录

      Document document = new Document();

/*
public Field(String name,
             String value,
             Field.Store store,
             Field.Index index)Create a field by specifying its name, value and how it will be saved in the index. Term vectors will not be stored in the index.

Parameters:
name – The name of the field
value – The string to process
store – Whether value should be stored in the index
index – Whether the field should be indexed, and if so, if it should be tokenized before indexing

Throws:
NullPointerException – if name or value is null
IllegalArgumentException – if the field is neither stored nor indexed

*/

      document.add(new Field(“id”, result.getString(“id”),  Field.Store.YES, Field.Index.ANALYZED));
      document.add(new Field(“name”,result.getString(“name”), Field.Store.YES, Field.Index.ANALYZED));
      document.add(new Field(“math”,result.getString(“math”), Field.Store.YES, Field.Index.NO));

      indexWriter.addDocument(document);/*加入索引器*/
/*========================================*/
   }
      indexWriter.optimize(); /*优化*/
      indexWriter.close(); /*关闭*/
  } catch (IOException e) {
   e.printStackTrace();
   System.out.println(e.getMessage());
  }
   sql.close();
   con.close();
  }catch(java.lang.ClassNotFoundException e){
   System.err.println(“ClassNotFoundException:” + e.getMessage());
  }catch(SQLException ex){
   System.err.println(“SQLException:” + ex.getMessage());
  }
 }
}

 

 

转自

 

http://blog.haohtml.com/index.php/archives/2105

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值