有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将ArrayList<String>添加到SQLite数据库

我正在编写我的第一个程序,并尝试使用JDBC驱动程序SQLite-JDBC-3.7.2将ArrayList(从CSV数据库)中的数据传输到SQLite数据库。罐子 我已经阅读了tutorialsfür SQLite和java初学者以及对stackoverflow(Link 1Link 2)问题的不同回答,我终于可以创建一个带有表和变量列的数据库(数据类型:TEXT)。但当我尝试使用此方法将ArrayList中的记录添加到testtable时:

/**
 * 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:天哪,我被自己的愚蠢绊倒了。我打开了一个到数据库的连接并调用了一个方法,该方法同时打开并关闭了一个连接。当然,现在我无法执行语句,因为我的数据库已关闭。谢谢纳伦给你的时间


共 (1) 个答案

  1. # 1 楼答案

    用于像这样的循环。。。如果像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 (i=0; i<line.size(); i++){
                    sSQL = sSQL + line.get(i); //add record per line in columns
                    if(!(i+1==line.size()))
                    {
                        str=str+",";
                    }
                }
    

    而你插入了“?”生成时查询中的(占位符)。。如果是这样的话,您应该使用PrepareAdedStatement ps,然后使用类似于ps.setInt()的方法来填充其中的正确值,然后您应该执行更新()。。。如果您想使用prepareStatement,您可以选中http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/