Auf Thema antworten

Ok, super .. wenigstens etwas mal richtig :D


öffnen tue ich die Datenbank hier:

[CODE=java]    public EditData() {

        initComponents();

        ConnectDb();

        branchRepos = new BranchRepository(conn);

    }

    Connection conn = null;

    BranchRepository branchRepos = null;

  

    private Connection ConnectDb() {

        try {

            conn = DriverManager.getConnection("jdbc:sqlite:" + System.getProperty("user.home") + "/Desktop/Standorte_Adressen.db");

            return conn;

        } catch (SQLException ex) {

            JOptionPane.showMessageDialog(null, ex);

            return null;

        }

    }[/CODE]

Konstruktor Aufruf im BranchModel:

[CODE=java]    public BranchModel(BranchRepository branches, Branch.Location location) {

        this.branches = branches;

        this.location = location;

        try {

            data = branches.findAll(location);

        } catch (SQLException e) {

            data = new ArrayList<>();

            e.printStackTrace();

        }

    }[/CODE]

Konstruktor Aufruf in BranchRepository:

[CODE=java]    public BranchRepository(Connection conn) {

        this.conn = conn;

    }[/CODE]

findAll Methode in BranchRepository:

[CODE=java]    public List<Branch> findAll(Location where) throws SQLException {

        String sql = LOCATION.get(where);

        try(PreparedStatement pst = conn.prepareStatement(sql)) {

            try(ResultSet rs = pst.executeQuery()){

                List<Branch> result = new ArrayList<>();

                while(rs.next()) {

                    result.add(readBranch(rs));

                }

                return result;           

            }

        }

    }[/CODE]

readBranchMethode in BranchRepository:

[CODE=java]    private Branch readBranch(ResultSet rs) throws SQLException {

        return new Branch(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getInt(6), rs.getLong(7), rs.getString(8), rs.getInt(9), rs.getString(10), rs.getString(11));

    }[/CODE]

Sieht doch richtig aus oder? :O



Oben