I should make separate class with reading library of books, updating etc. Class books should have:
- parametric constructor plus override for toString()
- Method getAll static to read all data for DB
- Method getById is reading data when we give ID, also static.
but i did next:
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;
}
I really don't understand how.. Any help will be like :D