标题:PreparedStatement操作sqllite数据库批量提交报java.sql.SQLException: NYI ...
只看楼主
satanjerk
Rank: 1
等 级:新手上路
帖 子:10
专家分:4
注 册:2009-5-30
结帖率:50%
已结贴  问题点数:20 回复次数:2 
PreparedStatement操作sqllite数据库批量提交报java.sql.SQLException: NYI错误
代码如下:
程序代码:
public static void main(String[] args) throws SQLException {
        Connection con = null;
  
        TimeSpan ts = new TimeSpan();
        ts.SetStartTime();
        try
        {
      
            Class.forName("org.sqlite.JDBC");
            con = DriverManager.getConnection("jdbc:sqlite:"+filename);
            con.setAutoCommit(false);
            //Statement stm = con.createStatement();
      
      
      
            PreparedStatement prep = con.prepareStatement("insert into book (Name,author,Price) values(?,?,?);");
      
            for(int i=1;i<10001;i++)
            {
                prep.setString(1, "BookName"+i);
                prep.setString(2, "Writer"+i);
                prep.setInt(3, i);
                prep.addBatch();
            }
            //prep.executeUpdate();
            //con.setAutoCommit(false);
            prep.executeBatch();
            ();
            con.setAutoCommit(true);
            /*
            prep.setString(1, "水浒传");
            prep.setString(2, "施耐庵");
            prep.setInt(3, 89);
  
      
            */
            ts.SetEndTime();
      
            ts.GetSpanTime();
        /*    con.setAutoCommit(true);
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("select * from book");
            while(rs.next())
            {
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
            }
      
      
        */
      
        }
        catch(Exception e)
        {
            try
            {
                con.rollback();
            }
            catch (SQLException ex)
            {
          
                ex.printStackTrace();
            }
            System.out.println(e.getMessage());
            System.out.println(e.toString());
        }
        finally
        {
            try
            {
                con.close();
            }
            catch (SQLException e)
            {
          
                e.printStackTrace();
            }
        }
    }
单次提交executeUpdate()没有任何问题,批量提交executeBatch()总是抛异常
搜索更多相关主题的帖子: sqlite 数据库 
2013-05-23 13:37
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
得分:14 
while(rs.next())
            {
                System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));
            }
     改成这样:
 while(rs.next())
            {
                System.out.println(rs.getInt(1)+" "+rs.getInt(2)+" "+rs.getInt(3)+" "+rs.getInt(4));
            }
     试试!!

旨在提高编程水平,学有所用,学有所成,学有所为。
2013-05-23 22:15
satanjerk
Rank: 1
等 级:新手上路
帖 子:10
专家分:4
注 册:2009-5-30
得分:0 
回复 2楼 Kingbox_tang
输出没有任何问题,关键是批量插入的时候老是报异常NYI
2013-05-24 18:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-407823-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018647 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved