JDBC 5: IMPORTANT
Inside the parent table/class (exhibition), define a constructor which contains one argument as a list of all the records in the child table (stall) related to each exhibition.
See the code for clarification.
public class Stall {
private Long id;
private String name;
private String detail;
private String owner;
private Exhibition exhibition;
public Stall(){}
public Stall(Long id,String name,String detail,String owner,Exhibition exhibition){
this.id=id;
this.name=name;
this.detail=detail;
this.owner=owner;
this.exhibition=exhibition;
}
public Long getId(){
return this.id;
}
public String getName(){
return this.name;
}
public String getDetail(){
return this.detail;
}
public String getOwner(){
return this.owner;
}
public Exhibition getExhibition(){
return this.exhibition;
}
public void setId(Long id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setDetail(String detail){
this.detail=detail;
}
public void setOwner(String owner){
this.owner=owner;
}
public void setExhibition(Exhibition exhibition){
this.exhibition=exhibition;
}
}
_________________________________________________________________________
import java.util.*;
public class Exhibition {
private Long id;
private String name;
private List<Stall> stallList;
public Exhibition(){}
public Exhibition(Long id,String name){
this.id=id;
this.name=name;
}
public Exhibition(Long id,String name,List<Stall> stallList){
this.id=id;
this.name=name;
this.stallList=stallList;
}
public Long getId(){
return this.id;
}
public String getName(){
return this.name;
}
public List<Stall> getStallList(){
return this.stallList;
}
public void setId(Long id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setStallList(List<Stall> stallList){
this.stallList=stallList;
}
}
_______________________________________________________________________________
import java.sql.*;
import java.util.*;
public class ExhibitionDAO {
public Exhibition getExhibition(String name) throws ClassNotFoundException, SQLException{
Exhibition exhibition = null;
List<Stall> stallList = new ArrayList<Stall>();
// Connection conn = DBConnetion.getConnection();
PreparedStatement st = DBConnection.getConnection().prepareStatement("Select * from exhibition where name=?");
st.setString(1,name);
ResultSet rs = st.executeQuery();
while(rs.next()){
Long id = rs.getLong(1);
String exName = rs.getString(2);
PreparedStatement st2 = DBConnection.getConnection().prepareStatement("Select * from stall where exhibition_id=?");
st2.setLong(1,id);
ResultSet rs2 = st2.executeQuery();
while(rs2.next()){
stallList.add(new Stall(rs2.getLong(1),rs2.getString(2),rs2.getString(3),rs2.getString(4),new Exhibition(id,exName)));
exhibition = new Exhibition(id,exName,stallList);
}
}
return exhibition;
}
}
_________________________________________________________________________________
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class DBConnection {
public static Connection getConnection() throws ClassNotFoundException, 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.util.Scanner;
public class Main {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
ExhibitionDAO dao = new ExhibitionDAO();
Exhibition e = null;
System.out.println("Enter the exhibition name:");
String ename = sc.nextLine();
e = dao.getExhibition(ename);
while(e==null){
System.out.println("Enter the correct exhibition name:");
ename=sc.nextLine();
e=dao.getExhibition(ename);
}
System.out.format("%-20s%-20s%-15s\n","Stall Name","Detail","Owner name");
for(Stall s:e.getStallList()){
System.out.format("%-20s%-20s%-15s\n",s.getName(),s.getDetail(),s.getOwner());
}
}
}
Comments
Post a Comment