2014-07-23 219 views
0

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; 




    } 

} 

提前非常感謝。

+0

看起來你正在檢查你的ArrayList是否爲空而不是數據庫。 'people.isEmtpy()'可能總是如此,因爲您沒有向ArrayList添加任何內容。 –

+1

用數據交換填充人的位,以及插入位,具體取決於人員是否爲空。 –

+1

此外,由於各種原因,保留一個長壽命的複製對象圖,內存中的數據庫將給你帶來很多麻煩。 –

回答

0

看起來好像你沒有給人們一個價值:你創建了一個ArrayList的實例,但僅此而已。嘗試在連接到數據庫後進行查詢:SELECT * FROM PeoplesInfo,然後檢查此查詢的結果以查看您的表是否爲空。

希望它可以幫助

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

我試圖從表中檢查結果而不是數組。這是目前我嘗試過的,它給了我一個索引超出界限的錯誤。 rSt =語句 \t \t .executeQuery(「SELECT * FROM PeoplesInfo」); \t \t 如果(RST == NULL){ \t字符串SQL = 「INSERT INTO PeoplesInfo(姓名,生物,圖像)」 + \t「VALUES( '約翰', '加利福尼亞','C:\\用戶\ \詹姆斯\\文檔\\ GoogleApp \\圖像\\ upload.png');「; \t statement.executeUpdate(sql); \t System.out.println(「Record added」); – user3837019