【问题标题】:Add ArrayList<String> to SQLite DB将 ArrayList<String> 添加到 SQLite DB
【发布时间】:2014-02-14 12:52:31
【问题描述】:

我正在编写我的第一个程序并尝试使用 JDBC 驱动程序 sqlite-jdbc-3.7.2.jar 将带有 ArrayList(来自 CSV 数据库)的数据传输到 SQLite 数据库。 我已经阅读了tutorials für SQLite 和 java 初学者以及对 stackoverflow 问题的不同答案(Link 1,Link 2),我终于可以创建一个包含表和变量列的数据库(数据类型:TEXT)。但是当我尝试使用这种方法将 ArrayList 中的记录添加到我的测试表时:

/**
 * Add record from ArrayList<string> to table
 * @param selectTable (name of table)
 * @param line (List with record for table)
 */
public void addRecord (String selectTable, ArrayList<String> line){
    try{
        //connect to database
        connection = new DBconnect(pathDataBase);
        database = connection.getConnection();

        //create SQL Statement
        ArrayList<String> columnNames = new ArrayList<String>();
        columnNames = getColumnList(selectTable); //call method to get all columnNames from selected table

        String sSQL = "INSERT INTO " + selectTable + "("; //update statement (string sSQL)

        //disperse ArrayList<string> columnNames in parts to add to the statement(string sSQL)
        int i = 0;
        for (i=0; i<columnNames.size(); i++){
            sSQL = sSQL + columnNames.get(i);
            if (columnNames.size() + 1 > i+2){
                sSQL = sSQL + ", "; //update statement(string sSQL)
            }//end if
        }// end for
        sSQL = sSQL + ") VALUES("; //update statement(string sSQL)

        //disperse ArrayList<string> line in parts to add to the statement(string sSQL)
        i=0;
        for (i=0; i<columnNames.size(); i++){
            sSQL = sSQL + line.get(i); //add record per line in columns
            if (columnNames.size() + 1 > i+2){
                sSQL = sSQL + ", ";
            }//end if
        }//end for
        sSQL = sSQL + ");";
        System.out.println(sSQL);

        Statement statement = database.createStatement();
        System.out.println("created statement");
        statement.executeUpdate(sSQL);
        System.out.println("executed Update");

        statement.close();
        database.close();
        //catch exception
    } catch ( Exception e ) {
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        System.exit(0);
    }//end try/catch
}//end method

我得到以下异常描述:

Opened database successfully
INSERT INTO Testtable(Test1, Test2, Test3) VALUES(Hallo1, Hallo2, Hallo3); //(ArrayList<string> columnNames) (ArrayList<string> line)
created statement
java.lang.NullPointerException: null

我认为我的字符串“sSQL”中的某些内容一定是错误的。

有人知道我必须改变什么才能让它工作吗? 我不能在“文本”列中写入数据类型“字符串”吗?

我希望我可以理解地描述我的问题并提出正确的问题。

提前非常感谢。 :)

@all:哦,伙计,我因自己的愚蠢而跌跌撞撞。我打开了一个到数据库的连接并调用了一个方法,该方法也打开了一个连接并关闭了它。当然现在我不能执行语句,因为我的数据库已关闭。谢谢你的时间!

【问题讨论】:

    标签: java sqlite arraylist


    【解决方案1】:

    像这样使用 for 循环...如果你传递像 Test0 Test1 Test2 Test3 这样的输入会产生像这样的输出

     Test0,Test1,Test2,Test3
    

    所以

    for(int i=0;i<columnNames.size();i++)
        {
            str=str+columnNames.get(i);
            if(!(i+1==columnNames.size()))
            {
                str=str+",";
            }
    
        }
    

    第二个for循环

     for (i=0; i<line.size(); i++){
                    sSQL = sSQL + line.get(i); //add record per line in columns
                    if(!(i+1==line.size()))
                    {
                        str=str+",";
                    }
                }
    

    并且您在构建时在查询中插入“?”(placeHolders)。如果是这样,您应该使用 PreparaedStatement ps 然后使用 ps.setInt() 之类的方法来填充正确的值,那么您应该执行更新().. . 如果你想使用prepareStatement你可以检查http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/

    【讨论】:

    • 感谢您的回答。我改变了循环并再次尝试。不幸的是,我得到了同样的例外。当我将 Statement 更改为 PreparedStatement 时,该方法不会打印我的 Syso“created Statement”并且仍然得到“null”。
    • 你做一件事......把 e.printStackTrace() 在 System.exit(0) 之前的 catch 块中;并运行并发布异常跟踪 .. 以便我们可以继续
    • 天哪,我被自己的愚蠢绊倒了。我打开了一个到数据库的连接并调用了一个方法,该方法也打开了一个连接并关闭了它。当然现在我不能执行语句,因为我的数据库已关闭。感谢 Naren 的宝贵时间!!!
    • 我想到了关于数据库关闭的同样的事情......它现在的任何工作方式
    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多