当前位置: 代码迷 >> J2EE >> 为什么只能插入数据库1行,然后出错?该如何解决
  详细解决方案

为什么只能插入数据库1行,然后出错?该如何解决

热度:54   发布时间:2016-04-22 01:17:29.0
为什么只能插入数据库1行,然后出错?

Java code
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;            }    }    }private ArrayList<String> parse(String str){    ArrayList<String> arry=new ArrayList();    StandardTagger tagger = new StandardTagger("-Oyomi");    StandardLattice lattice = tagger.createLattice();        lattice.setSentence(str);    tagger.parse(lattice);     Node node=lattice.bosNode();    while (node != null) {        String surface = node.surface();         arry.add(surface);        node = node.next();    }    lattice.destroy();    tagger.destroy();       return arry;}}
  相关解决方案