Tak sem trochu pokrocil s databazi. Posilam na ukazku kod, kde se pripojuju k databazi a getConnection() a nasledne tam mam dve metody. Jednu na precteni databaze a jeji vypis do konzole a druhou na pridani radku do databaze. To mi ovsem nejak nefunguje. Nevite proc?
public class Database{
Connection getConnection() throws Exception{
String url = "jdbc:derby://localhost:1527/Tasks";
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
return DriverManager.getConnection(url, "Majitel", "heslo");
}
void readDatabase(Connection conn) throws Exception{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(
"SELECT APP.TASKS.TASK, APP.TASKS.TOPIC, APP.TASKS.TOPIC, APP.TASKS.DONE "
+ "FROM APP.TASKS");
System.out.println("Task - Topic - Priority - Done");
while (rs.next()) {
System.out.println(""+ rs.getString(1)+" - "+rs.getString(2)+" - "+rs.getString(3)+" - "+rs.getString(4)+"");
}
// uzavření dotazu i všech výsledků a spojení
st.close();
conn.close();
}
void addRow(Connection conn) throws Exception{
Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO TASKS" + "(TASK, TOPIC, PRIORITY) VALUES ('Ahoj', 'Neco', '1')");
st.close();
conn.close();
}
public static void main(String[] args) throws Exception {
Database d = new Database();
d.addRow(d.getConnection());
d.readDatabase(d.getConnection());
}
}