Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app.dao;
- import app.model.Pilot;
- import java.sql.SQLException;
- import java.util.Vector;
- public class PilotDataAccess extends DataAccess {
- private Pilot pilot; /* ID = BREVET... PREGUIÇA DE MUDAR :) */
- public PilotDataAccess() {}
- public PilotDataAccess( Pilot pilot ){
- this.pilot = pilot;
- }
- public boolean insert() {
- boolean success = false;
- if( connect() ){
- try {
- state = con.prepareStatement("insert into pilot values(?,?,?,?,?)");
- state.setInt(1, pilot.getId());
- state.setString(2, pilot.getFname());
- state.setString(3, pilot.getLname());
- state.setDate(4, convertToSQL(pilot.getEntry()));
- state.setInt(5, pilot.getCategory().getId());
- state.executeQuery();
- success = true;
- } catch (SQLException e) { }
- finally {
- disconnect();
- }
- }
- return success;
- }
- public boolean delete(){
- boolean success = false;
- if( connect() ){
- try {
- state = con.prepareStatement("delete from pilot where brevet = ?");
- state.setInt(1, pilot.getId());
- state.executeQuery();
- success = true;
- } catch (SQLException e) { }
- finally {
- disconnect();
- }
- }
- return success;
- }
- public boolean update(int temp) {
- boolean success = false;
- if( connect() ){
- try {
- state = con.prepareStatement("update pilot set brevet=?, fname=?, lname=?, entry=?, pilotcategory=? where brevet = ?");
- int id = pilot.getId();
- state.setInt(1, id);
- state.setString(2, pilot.getFname());
- state.setString(3, pilot.getLname());
- state.setDate(4, convertToSQL(pilot.getEntry()));
- state.setInt(5, pilot.getCategory().getId());
- state.setInt(6, temp);
- state.executeQuery();
- success = true;
- } catch( SQLException e ){ }
- finally {
- disconnect();
- }
- }
- return success;
- }
- public Vector<Vector> select() {
- Vector<Vector> set = null;
- if( connect() ){
- set = new Vector();
- try {
- state = con.prepareStatement("select * from pilot");
- result = state.executeQuery();
- Vector<String>line = null;
- while( result.next() ){
- line = new Vector();
- line.add(Integer.toString(result.getInt("brevet")));
- line.add(result.getString("fname"));
- line.add(result.getString("lname"));
- line.add(result.getDate("entry").toString());
- line.add(Integer.toString(result.getInt("pilotcategory")));
- set.add(line);
- }
- } catch (SQLException e) { }
- finally {
- disconnect();
- }
- }
- return set;
- }
- public Vector<Vector> selectAllPilotNames() {
- Vector<Vector> set = null;
- if( connect() ){
- set = new Vector();
- try {
- state = con.prepareStatement("select * from pilot");
- result = state.executeQuery();
- Vector<String>line = null;
- while( result.next() ){
- line = new Vector();
- line.add(result.getString("fname"));
- set.add(line);
- }
- } catch (SQLException e) { }
- finally {
- disconnect();
- }
- }
- return set;
- }
- public String selectByID( int id ) {
- String name = "";
- if( connect() ){
- try {
- state = con.prepareStatement("select fname from pilot where brevet = ?");
- state.setInt(1, id);
- result = state.executeQuery();
- while( result.next() )
- name = result.getString("fname");
- } catch (SQLException e) { }
- finally {
- disconnect();
- }
- }
- return name;
- }
- public int selectByName( String name ) {
- int id = 0;
- if( connect() ){
- try {
- state = con.prepareStatement("select brevet from pilot where fname = ?");
- state.setString(1, name);
- result = state.executeQuery();
- while( result.next() )
- id = result.getInt("brevet");
- } catch (SQLException e) { }
- finally {
- disconnect();
- }
- }
- return id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement