· 7 years ago · Feb 20, 2019, 11:06 PM
1public String createTable(ArrayList<Company> companyList) throws SQLException
2 {
3 Connection cononnection = con.getConnection();
4
5 ArrayList<Laptop> laptopFindlist=new ArrayList<Laptop>();
6 laptopFindlist.addAll(companyList.get(0).getLaptop());
7
8 ArrayList<Mobile> mobileFindlist=new ArrayList<Mobile>();
9 mobileFindlist.addAll(companyList.get(0).getMobile());
10
11 String createMobilequery = "CREATE TABLE IF NOT EXISTS mobile(mobile_id int PRIMARY KEY,mobile_model varchar(30),mobile_rating int,mobile_price float);";
12 String createLaptopquery = "CREATE TABLE IF NOT EXISTS laptop(laptop_id int PRIMARY KEY,laptop_model varchar(30),laptop_rating int ,laptop_price float);";
13 String createCompanyquery = "CREATE TABLE IF NOT EXISTS company(company_id int,laptop_id int, FOREIGN KEY(laptop_id) REFERENCES laptop(laptop_id),mobile_id int,FOREIGN KEY(mobile_id) REFERENCES Mobile(mobile_id),company_name varchar(30));";
14
15 Statement st = cononnection.createStatement();
16 int check1 = st.executeUpdate(createMobilequery);
17 int check2 = st.executeUpdate(createLaptopquery);
18 int check3 = st.executeUpdate(createCompanyquery);
19
20 //INSERTING DATA INTO TABLE
21 String insertIntomobileQuery = "insert into mobile values('"+mobileFindlist.get(0).getMobileId()+"','"+mobileFindlist.get(0).getMobileModel()+"','"+mobileFindlist.get(0).getMobileRating()+"','"+mobileFindlist.get(0).getMobilePrice()+"');";
22 System.out.println(insertIntomobileQuery);
23 String insertIntolaptopQuery = "insert into laptop values('"+laptopFindlist.get(0).getLaptopId()+"','"+laptopFindlist.get(0).getLaptopModel()+"','"+laptopFindlist.get(0).getLaptopRating()+"','"+laptopFindlist.get(0).getLaptopPrice()+"');";
24 System.out.println(insertIntolaptopQuery);
25 String insertCompanyquery = "insert into company values('"+companyList.get(0).getCompanyId()+"','"+laptopFindlist.get(0).getLaptopId()+"','"+mobileFindlist.get(0).getMobileId()+"','"+companyList.get(0).getCompanyName()+"');";
26 System.out.println(insertCompanyquery);
27
28 try {
29 int check11 = st.executeUpdate(insertIntomobileQuery);
30 int check22 = st.executeUpdate(insertIntolaptopQuery);
31 int check33 = st.executeUpdate(insertCompanyquery);
32 return "INSERTED";
33 }
34 catch(Exception ex)
35 {
36 System.out.println(ex);
37 }
38 cononnection.close();
39 return "ERROR INSERTING VALUES";
40 }