/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package asukohapakett; import java.sql.*; import java.util.*; /** * * @author turing */ public class AndmeHaldus { Connection yhendus; Connection looYhendus() throws SQLException{ if(yhendus==null){ yhendus=DriverManager.getConnection( "jdbc:derby://localhost:1527/sample", "app", "app"); } return yhendus; } public AsukohaAndmed[] kysiAndmed() throws SQLException{ List loetelu=new ArrayList(); Statement st=looYhendus().createStatement(); ResultSet rs=st.executeQuery("SELECT id, x, y, eesnimi FROM asukohad"); while(rs.next()){ loetelu.add(new AsukohaAndmed(rs.getInt("id"), rs.getInt("x"), rs.getInt("y"), rs.getString("eesnimi"))); } return loetelu.toArray(new AsukohaAndmed[0]); } public void lisaAndmed(int x, int y, String eesnimi) throws SQLException{ PreparedStatement st=looYhendus().prepareStatement( "INSERT INTO asukohad (x, y, eesnimi) VALUES (?, ?, ?)"); st.setInt(1, x); st.setInt(2, y); st.setString(3, eesnimi); st.executeUpdate(); } }