Java 在数据库中插入值

Java 在数据库中插入值,java,mysql,database,sqlite,record,Java,Mysql,Database,Sqlite,Record,让它工作起来。谢谢大家的帮助 我是编程新手,我正在检查我的数据库是否为空。如果它是空的,它将插入一条记录。如果没有,它应该正常运行。我还在检查我的图像是否存在文件路径 每次我运行程序时,它都会不断添加John,只有当数据库为空时,才应该添加John resultSet = statement .executeQuery("SELECT name, bio, image FROM PeoplesInfo"); while (resultSet.next()) { Person

让它工作起来。谢谢大家的帮助

我是编程新手,我正在检查我的数据库是否为空。如果它是空的,它将插入一条记录。如果没有,它应该正常运行。我还在检查我的图像是否存在文件路径

每次我运行程序时,它都会不断添加John,只有当数据库为空时,才应该添加John

resultSet = statement
    .executeQuery("SELECT name, bio, image FROM PeoplesInfo");

while (resultSet.next()) {

    Person p = new Person(resultSet.getString("name"),resultSet.getString("bio"),resultSet.getString("image"));
    people.add(p);

    System.out.println(p.toString());
    System.out.println("Retrieved element is = " + p.getName());       

}
我的代码:

public class Database {

    private ArrayList<Person> people;


    public Database(){
        people = new ArrayList<Person>();


    }


    public void run(){

        Connection connection = null;
        ResultSet resultSet = null;
        Statement statement = null;


        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager
                    .getConnection("jdbc:sqlite:C:\\Users\\James\\Documents\\GoogleApp\\Peopleinfo.sqlite");


            statement = connection.createStatement();

            if(people.isEmpty()){

                File f = new File ("C:\\Users\\James\\Documents\\GoogleApp\\images\\upload.png");

                if (f.exists()){

                        String sql = "INSERT INTO PeoplesInfo (Name,bio,Image) " +
                                "VALUES ('John','California','C:\\Users\\James\\Documents\\GoogleApp\\images\\upload.png');"; 
                        statement.executeUpdate(sql);

                        System.out.println("Image exists");

                    }
                    else {
                        System.out.println("Image doesn't exist");}

            }


            else if (!people.isEmpty()) {

                String sql = "INSERT INTO PeoplesInfo (Name,bio,Image) " +
                        "VALUES ('Stan','California','C:\\Users\\James\\Documents\\GoogleApp\\images\\upload.png');"; 
                statement.executeUpdate(sql);

                System.out.println("Record added");


            }



            resultSet = statement
                    .executeQuery("SELECT name, bio, image FROM PeoplesInfo");




        while (resultSet.next()) {

                Person p = new Person(resultSet.getString("name"),resultSet.getString("bio"),resultSet.getString("image"));
                people.add(p);


                System.out.println(p.toString());
                System.out.println("Retrieved element is = " + p.getName());       



            }


                        resultSet = statement
                    .executeQuery("SELECT name, bio, image FROM PeoplesInfo");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {

                resultSet.close();
                statement.close();
                connection.close();

            } catch (Exception e) {
                e.printStackTrace();

            }

        }
    }


    public ArrayList<Person> getPeople(){
        return people;




    }

}

非常感谢。

您似乎没有给人们带来价值:您创建了ArrayList的一个实例,但仅此而已。连接到数据库后,尝试进行查询:从PeoplesInfo中选择*,然后检查此查询的结果以查看表是否为空


希望对我有帮助。对我来说,这似乎是一个简单的操作顺序问题

在检查人员数组是否为空之前,应先执行此代码

resultSet = statement
    .executeQuery("SELECT name, bio, image FROM PeoplesInfo");

while (resultSet.next()) {

    Person p = new Person(resultSet.getString("name"),resultSet.getString("bio"),resultSet.getString("image"));
    people.add(p);

    System.out.println(p.toString());
    System.out.println("Retrieved element is = " + p.getName());       

}
另外,看起来您尝试了两种不同的方法,因此当您只需要一次insert命令时,就有了两次insert命令

然后重新排列,应该是这样的

private ArrayList<Person> people;
public Database(){
    people = new ArrayList<Person>();
}

public void run(){

    Connection connection = null;
    ResultSet resultSet = null;
    Statement statement = null;


    try {
        Class.forName("org.sqlite.JDBC");
        connection = DriverManager
            .getConnection("jdbc:sqlite:C:\\Users\\James\\Documents\\GoogleApp\\Peopleinfo.sqlite");


        statement = connection.createStatement();
        resultSet = statement
            .executeQuery("SELECT name, bio, image FROM PeoplesInfo");

        while (resultSet.next()) {

            Person p = new Person(resultSet.getString("name"),resultSet.getString("bio"),resultSet.getString("image"));
            people.add(p);

            System.out.println(p.toString());
            System.out.println("Retrieved element is = " + p.getName());       

        }

        if(people.isEmpty()){
            String sql = "INSERT INTO PeoplesInfo (Name,bio,Image) " +
                "VALUES ('John','California','C:\\Users\\James\\Documents\\GoogleApp\\images\\upload.png');"; 
            statement.executeUpdate(sql);
            System.out.println("Record added");

            File f = new File ("C:\\Users\\James\\Documents\\GoogleApp\\images\\upload.png");

            if (f.exists()){
                System.out.println("Image exists");
            }
            else {
                System.out.println("Image doesn't exist");
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            resultSet.close();
            statement.close();
            connection.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


public ArrayList<Person> getPeople(){
    return people;
}

看起来您正在检查ArrayList是否为空,而不是数据库。people.isEmtpy可能总是正确的,因为您没有向ArrayListSwap中添加任何内容。根据人员是否为空,您可以将数据填充到人员所在的位,以及插入的位。此外,出于各种原因,将数据库副本的长寿命objectgraph保留在内存中会给您带来很多麻烦。介意再解释一下交换数据的相关内容吗?对不起,我只是有点困惑。对不起。交换你执行事情的顺序。现在,在将任何数据填充到persons数组之前插入。只需移动将数据从db放入方法顶部的arraylist的代码块,而不是底部的代码块。如果我不清楚,很抱歉。我正在尝试检查表而不是数组的结果。这是我目前尝试的,它给了我一个索引越界错误。rSt=语句。executeQuerySELECT*来自PeoplesInfo;ifrSt==null{String sql=插入PeoplesInfo名称、bio、图像+值'John'、'California'、'C:\\Users\\James\\Documents\\GoogleApp\\images\\upload.png';添加了statement.executeUpdatesql;System.out.printlnRecord;