将值插入数据库

时间:2014-07-23 16:13:13

标签: java mysql database sqlite record

GOT IT WORKING。感谢大家的帮助

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

每次运行程序时,它都会不断添加“John”(只有在数据库为空时才应添加John)。

我的代码:

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;




    }

}

非常感谢提前。

2 个答案:

答案 0 :(得分:0)

看起来你没有给人们一个价值:你创建了一个ArrayList实例,但就是这样。在连接到数据库后尝试进行查询:SELECT * FROM PeoplesInfo然后检查此查询的结果以查看您的表是否为空。

希望有所帮助

答案 1 :(得分:0)

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

此代码应在检查人员数组是否为空之前发生。

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());       

}

此外,您似乎尝试了几种不同的方式,因此只需要一次插入命令两次。

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

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;
}