INTRODUCTION TO JDBC

 DB CONNECTION:

import java.util.ResourceBundle;
import java.sql.*;
public class DBConnection {
	public static Connection getConnection() throws Exception {        
		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;
    }
}


ITEMTYPE:

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 = 



ITEMTYPEDAO

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
public class ItemTypeDAO {
public List<ItemType> getAllItemTypes() throws Exception{
List<ItemType> itemTypeList = new ArrayList<>();

Connection conn = DBConnection.getConnection();        //load,connection,statement,result,close connection
Statement stmt = conn.createStatement(); //single q,queo
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);
}
return itemTypeList;
}
}



MAIN

import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
ItemTypeDAO obj = new ItemTypeDAO();
List<ItemType> lst = new ArrayList<>();
lst = obj.getAllItemTypes();
System.out.format("%-5s %-15s %-10s %s\n","ID","Name","Deposit","Cost per day");
for (ItemType itemType : lst) {
System.out.format("%-5s %-15s %-10s %s\n",itemType.getId(),itemType.getName(),itemType.getDeposit(),itemType.getCostPerDay());
}
}
}

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.