日期:2014-05-19 浏览次数:20719 次
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Iterator; import net.moraleboost.mecab.Node; import net.moraleboost.mecab.impl.StandardLattice; import net.moraleboost.mecab.impl.StandardTagger; public class DataAccess { //private ArrayList<String> array=null; //private ArrayList<String> arry=null; public static void main(String[] args) { DataAccess dataAccess =new DataAccess(); try { dataAccess.selectPostgre(); } catch (Exception e) { e.printStackTrace(); } } public void selectPostgre() throws Exception{ String user = "qito"; String pass = "111"; String servername = "127.0.0.1"; String dbname = "book"; Connection conn = null; Statement stmt = null; ResultSet rset = null; String isbn=null; String title=null; String str=null; int i; ResultSet idnum; int listid; ArrayList<String> array=new ArrayList(); try { Class.forName ("org.postgresql.Driver"); /* Connection作成 */ conn = DriverManager.getConnection ("jdbc:postgresql://" + servername + ":5432/" + dbname,user,pass); /* Statement作成 */ stmt = conn.createStatement(); /* Resultset作成 */ rset = stmt.executeQuery("select * from book"); //while(rset.next()){ //System.out.println(rset.getString("isbn")); //} //System.out.println("!!!!!!!!!!!!!!!!"); String insertstr ; while (rset.next()) { //System.out.println(rset.getString("title")); title=rset.getString("title"); //System.out.println(title); isbn=rset.getString("isbn"); //System.out.println(isbn); array=new ArrayList<String>(parse(rset.getString("title"))); for(i=1;i<array.size()+1;i++){ Iterator<String> it = array.iterator(); while(it.hasNext()) { str=new String(it.next()); if(str.equals("")||str.equals("(")||str.equals(")")||str.equals("=")||str.equals("-")||str.equals("は")||str.equals("が")||str.equals("に")||str.equals("の")||str.equals("/")||str.equals("、")||str.equals("?")||str.equals("."))continue; str=str.replaceAll("\r\n",""); System.out.print(str); stmt.executeQuery("insert into morpheme_list (morpheme_name) values('"+str+"')");//这里出错 但是数据可以插到数据库里 } } } } catch (ClassNotFoundException e) { throw e; } catch (SQLException e) { throw e; } catch ( Exception e){ throw e; } finally{ if(conn != null){ conn.close(); conn = null; } if(stmt != null){ stmt.close(); stmt = null; } if(rset != null){ rset.close(); rset = null; } }