public class Task { private int id; private String category; private Date dueDate; // getters and setters here } List<Task> tasks = new ArrayList<Task>(); Connection conn = null; PreparedStatement stmt = null; String sql = "SELECT id, category, duedate " + "FROM tasks " + "WHERE category = ?"; try { conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.prepareStatement(sql); stmt.setString(1, "foo"); ResultSet rs = stmt.executeQuery(); while(rs.next()) { Task task = new Task(); task.setId(rs.getLong("id")); task.setCategory(rs.getString("category")); task.setDueDate(rs.getDate("duedate")); } rs.close(); } catch(SQLException se) { //Handle errors for JDBC. I'll just throw a RuntimeException. throw new RuntimeExcetion("error when executing query", se); } finally { try{ if(stmt != null) { stmt.close(); } } catch(SQLException se) { se.printStackTrace(); } try { if (conn != null) { conn.close(); } } catch(SQLException se) { se.printStackTrace(); } }
class CustomerList { public static Customer getCustomerById(Integer customer_id) { for(Customer customer : Customer.allCustomers) { if(customer.id == customer_id) { return customer; } } return null; } public static ArrayList<Customer> getCustomersById(Integer customer_id) { ArrayList<Customer> outList = new ArrayList<Customer>(); for(Customer customer : Customer.allCustomers) { if(customer.id == customer_id) { outList.add(customer); } } return outList; } }
see sql2o per fare fetch direttamente in una classe
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.ResultSet; import java.sql.Statement; Class.forName("com.ibm.as400.access.AS400JDBCDriver"); String uri = "jdbc:as400://localhost/"; Connection conn = DriverManager.getConnection(uri, USR, PASSWRD); public static Order getOrder(Connection conn, int filter) { Statement stmt = null; ResultSet rs = null; Order order = null; String sql = "select field1, field2 " + " from LIB" + SCHEMA_SEPARATOR + "FILENAME a " + "where field3<>'A'"; try { stmt = conn.createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { int field_a = rs.getInt("field_a"); long field_b = rs.getLong("field_b"); String field_c = rs.getString("field_c"); order = new Order(nuord, datai, valut); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } } catch (SQLException e) { e.printStackTrace(); } } return order; }
import java.sql.*; public class TestSqliteApp { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); Statement stat = conn.createStatement(); stat.executeUpdate("create table people (name, occupation)"); stat.executeUpdate("insert into people (name, occupation) VALUES ('Gandhi', 'politics')"); stat.executeUpdate("insert into people (name, occupation) VALUES ('Turing', 'computers')"); ResultSet rs = stat.executeQuery("select * from people"); while (rs.next()) { System.out.println("name = " + rs.getString("name")); System.out.println("occupation = " + rs.getString("occupation")); } rs.close(); conn.close(); } }