JDBC 1:




_____________________________________________________________________
public class ItemType{
    
    Long id;
    String name;
    Double deposit;
    Double costPerDay;
    
    ItemType(){ }
    ItemType(Long id, String name, Double deposit, Double costPerDay){        
this.id = id;
this.name = name;
this.deposit = deposit;
this.costPerDay = costPerDay;    
    }
    
    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }
    
    
    public Double getDeposit() {
    return deposit;
    }

    public void setDeposit(Double deposit) {
    this.deposit = deposit;
    }
    
    public Long getId() {
    return id;
    }

    public void setId(Long id) {
    this.id = id;
    }
    
    public Double getCostPerDay() {
    return costPerDay;
    }

    public void setCostPerDay(Double costPerDay) {
    this.costPerDay = costPerDay;
    }
    
}
_________________________________________________________________

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class DBConnection {
public static Connection getConnection() throws SQLException {        
        ResourceBundle rb = ResourceBundle.getBundle("oracle");
        String url = rb.getString("db.url");
        String username = rb.getString("db.username");
        String password = rb.getString("db.password");
        
        Connection conn = DriverManager.getConnection(url,username,password);
        return conn;
    }
}
_____________________________________________________________________
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class ItemTypeDAO {
public List<ItemType> getAllItemTypes() throws SQLException {
List<ItemType> itemTypeList = new ArrayList<>();
        Connection conn = DBConnection.getConnection();
        Statement stmt = conn.createStatement();
        String query = "select * from item_type";
        ResultSet rs = stmt.executeQuery(query);
        
        while(rs.next()){
            long id = rs.getLong(1);
            String name = rs.getString(2);
            double deposit = rs.getDouble(3);
            double costPerDay = rs.getDouble(4);
            
            ItemType it = new ItemType(id,name,deposit,costPerDay);
            itemTypeList.add(it);
            it=null;
        }
        return itemTypeList;
            
}
}
_______________________________________________________________________
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws SQLException {
    ItemTypeDAO dao = new ItemTypeDAO();
        
        List<ItemType> itemList = new ArrayList<>();
        itemList=dao.getAllItemTypes();
        
        System.out.format("%-5s %-15s %-10s %s\n","ID","Name","Deposit","Cost per day");
        for(ItemType it:itemList){
            
             System.out.format("%-5s %-15s %-10s %s\n",it.getId(),it.getName(),it.getDeposit(),it.getCostPerDay());
        }
}
}



Comments

Popular posts from this blog