本のライブラリを読んだり、更新したりして、別のクラスを作成する必要があります。クラスの本には次のものが必要です。
- パラメトリック コンストラクタと toString() のオーバーライド
- DB のすべてのデータを読み取るメソッド getAll static
- メソッド getById は、ID を与えるときにデータを読み取りますが、これも静的です。
しかし、私は次のことをしました:
Main.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);
}
Books.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