-1

我应该单独上课,阅读图书馆,更新等。课本应该有:

  1. 参数构造函数加上 toString() 的覆盖
  2. 方法 getAll static 读取 DB 的所有数据
  3. 当我们给出 ID 时,getById 方法正在读取数据,也是静态的。

但我接下来做了:

主.java

    try {      
        ArrayList<String[]> allBooks = Books.getAll("SELECT * FROM books");
        for (String[] izd : allBooks) {
            System.out.println("Books id: " + izd[0] + " Date of publishing: " + izd[1] + " Id of books: " + izd[2]+...);
        }
    } catch (Exception e) {
        System.out.println(e);
    }

书籍.java

    static ArrayList<String[]> getAll(String stt) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException
    {
    connect();
    Statement stmt = conn.createStatement();
    stmt.executeQuery(stt);

    ResultSet rs = stmt.getResultSet();
    int columnsNum = rs.getMetaData().getColumnCount();
    ArrayList<String[]> result = new ArrayList<String[]>();

    while(rs.next())
    {
        String[] rowArr = new String[columnsNum];
            for(int i=0;i<columnsNum;i++)
                    rowArr[i]=rs.getString(i+1).toString();
            result.add(rowArr);
    }
    close();
    return result;
}

我真的不明白怎么......任何帮助都会像:D

4

1 回答 1

0

您在这里并没有真正提供有关您的目标的太多信息,所以我只能猜测。我会期待这样的事情:

public class Book {
  private String title;
  //...

  //constructor
  public Book(String theTitle) {
    this.title = theTitle;
  }

  public static ArrayList<Book> getAll(String stt) { /* ... */ }

  public static Book getById(int id) { /* ... */ }
}

在你的getAll函数中,你可能想要这样的东西:

while(rs.next())
{
    Book aBook = new Book(rs.getString("title"));
    result.add(aBook);
}
于 2014-02-24T11:38:35.653 回答