Witam!
Tworzę aplikację z bazą danych derby. Mam w GUI kilka tabel, których model wygląda np. tak:
public class TabelaModel extends AbstractTableModel{
public static final int a = 0;
private static final String b = "";
private static final int c = 2;
private static final int d = 3;
private static final float e = 4;
private static final float f = 4;
@Override
public int getRowCount() {
return wiersze.length;
}
@Override
public int getColumnCount() {
return wiersze[0].length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return wiersze[rowIndex][columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex){
return columnIndex == 1 || columnIndex == 2;
}
@Override
public String getColumnName(int column){
return nazwyKolumn[column];
}
@Override
public void setValueAt(Object ilosc, int rowIndex, int columnIndex){
wiersze[rowIndex][columnIndex] = ilosc;
}
private String[] nazwyKolumn = {"CosA", "CosB", "CosC", "CosD", "CosE", "CosF"};
private Object[][] wiersze = {{1, "", "", "", "", ""},
{2, "", "", "", "", ""},
{3, "", "", "", "", ""},
{4, "", "", "", "'", ""},
{5, "", "", "", "", ""},
{6, "", "", "", "", ""},
{7, "", "", "", "", ""},
{8, "", "", "", "", ""},
{9, "", "", "", "", ""},
{10, "", "", "", "", ""},
{11, "", "", "", "", ""},
{12, "", "", "", "", ""},
{13, "", "", "", "", ""},
{14, "", "", "", "", ""},
{15, "", "", "", "", ""},
{16, "", "", "", "", ""},
};
}
Łączę się z bazą i pobieram rekordy w ten sposób:
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
con = DriverManager.getConnection("jdbc:derby://localhost:1527/baza", "admin", "admin");
ps = con.createStatement();
rs = ps.executeQuery("select cena from Ceny where nazwa = 'przedmiotA'");
while(rs.next()){
System.out.println(rs.getFloat("cena"));
}
}catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}catch(SQLException e){
System.out.println(e.getMessage());
}
Ale mam dwa problemy:
Po pierwsze jak najlepiej w tym modelu uzupełnić tabelę danymi z bazy czy tak jak to robię niżej?
Czy można jakoś inaczej pobierać te rekordy, czy zawsze musi być ten while:
while(rs.next()){
System.out.println(rs.getFloat("cena"));
}
Wiem on jest bo zapytanie może zwrócić kilka rekordów ale gdy ja wiem, że moje dane są unikatowe w bazie i będzie zwracany tylko jeden?