Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package parcaToplama;
- import java.net.URL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- import hardwares.CPU;
- import hardwares.GPU;
- import hardwares.HDD;
- import hardwares.MOBO;
- import hardwares.PSU;
- import hardwares.RAM;
- import parcaEkleCikar.MainFrame;
- public class Sqltest {
- public static ArrayList<CPU> cpu = new ArrayList<>();
- public static ArrayList<GPU> gpu = new ArrayList<>();
- public static ArrayList<MOBO> mobo = new ArrayList<>();
- public static ArrayList<HDD> hdd = new ArrayList<>();
- public static ArrayList<PSU> psu = new ArrayList<>();
- public static ArrayList<RAM> ram = new ArrayList<>();
- int i = 1;
- private String whatAreWeDeleting="";
- /*public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- new Sqltest().getConnection();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- System.out.println("error");
- e.printStackTrace();
- }
- }*/
- public Connection getConnection() throws Exception {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/computer_hw";
- String username = "root";
- String password = "";
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url, username, password);
- // System.out.println("Connection successfull");
- Statement statement = conn.createStatement();
- // int result=statement.executeUpdate("ALTER TABLE `test_table` ADD
- // `bu_adi` INT(150) NOT NULL AFTER `hey`");
- // int result = statement.executeUpdate("ALTER TABLE `test_table` DROP
- // `bu_adi`");
- // queryler
- // queryler
- String query = "SELECT * FROM `cpu`";
- ResultSet result = statement.executeQuery(query);
- while (result.next()) {
- cpu.add(new CPU(result.getInt("id"), result.getString("brand"), result.getString("model"),
- result.getInt("price"), result.getFloat("power"), result.getInt("core"), result.getFloat("ghz"),
- result.getString("socket"), result.getString("ddr"), new URL(result.getString("URL"))));
- }
- String query2 = "SELECT * FROM `ram`";
- ResultSet result2 = statement.executeQuery(query2);
- while (result2.next()) {
- ram.add(new RAM(result2.getInt("id"), result2.getString("brand"), result2.getString("model"),
- result2.getInt("price"), result2.getFloat("power"), result2.getString("memory_type"),
- result2.getInt("clock_speed"), result2.getInt("memory_capacity"),
- new URL(result2.getString("URL"))));
- }
- String query3 = "SELECT * FROM `gpu`";
- ResultSet result3 = statement.executeQuery(query3);
- while (result3.next()) {
- gpu.add(new GPU(result3.getInt("id"), result3.getString("brand"), result3.getString("model"),
- result3.getInt("price"), result3.getFloat("power"), result3.getInt("memory_capacity"),
- new URL(result3.getString("URL"))));
- }
- String query4 = "SELECT * FROM `hdd`";
- ResultSet result4 = statement.executeQuery(query4);
- while (result4.next()) // hdd
- {
- hdd.add(new HDD(result4.getInt("id"), result4.getString("brand"), result4.getString("model"),
- result4.getInt("price"), result4.getFloat("power"), result4.getInt("memory"),
- result4.getString("type"), new URL(result4.getString("URL"))));
- }
- String query5 = "SELECT * FROM `mobo`";
- ResultSet result5 = statement.executeQuery(query5);
- while (result5.next()) // mobo
- {
- mobo.add(new MOBO(result5.getInt("id"), result5.getString("brand"), result5.getString("model"),
- result5.getInt("price"), 0, result5.getInt("max_ram"), result5.getString("ddr"),
- result5.getString("cpu_socket"), new URL(result5.getString("URL"))));
- }
- String query6 = "SELECT * FROM `psu`";
- ResultSet result6 = statement.executeQuery(query6);
- while (result6.next()) // psu
- {
- psu.add(new PSU(result6.getInt("id"), result6.getString("brand"), result6.getString("model"),
- result6.getInt("price"), 0, result6.getInt("power_output"), new URL(result6.getString("URL"))));
- }
- conn.close();
- /*
- * for (CPU a : cpu) { System.out.println(a.getshortDescription()); }
- * for (GPU a : gpu) { System.out.println(a.getshortDescription()); }
- * for (MOBO a : mobo) { System.out.println(a.getshortDescription()); }
- * for (RAM a : ram) { System.out.println(a.getshortDescription()); }
- * for (PSU a : psu) { System.out.println(a.getshortDescription()); }
- * for (HDD a : hdd) { System.out.println(a.getshortDescription()); }
- *
- * i = 6;
- */
- return conn;
- }
- public void sayHello(){
- System.out.println("hellooooO!!");
- }
- public Connection addDeleteOperations(Hardware hardware, boolean trueIfAdding, int i) {
- try {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/computer_hw";
- String username = "root";
- String password = "";
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url, username, password);
- Statement statement = conn.createStatement();
- String query="";
- if(trueIfAdding){
- switch(i){
- case 1: //mobo
- query=
- "INSERT INTO `computer_hw`.`mobo` "
- + "(`id`, `brand`, `model`, `price`, `max_ram`, `ddr`, `cpu_socket`, `URL`)"
- + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
- "', '" + ((MOBO)hardware).maxRAM + "', '" + ((MOBO)hardware).DDR +"', '"+ ((MOBO)hardware).CPUSocket +
- "', '"+ hardware.url.toString() +"')";
- break;
- case 2: //cpu
- //System.out.println("hey");
- query=
- "INSERT INTO `computer_hw`.`cpu` "
- + "(`id`, `brand`, `model`, `price`, `power`, `core`, `socket`, `ghz`, `ddr`, `URL`)"
- + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
- "', '" + ((CPU)hardware).power + "', '" + ((CPU)hardware).coreCount +"', '"+ ((CPU)hardware).socket +
- "', '"+ ((CPU)hardware).ghz +"', '" + ((CPU)hardware).DDR +"', '" + ((CPU)hardware).url.toString() +"')";
- //System.out.println("query");
- break;
- case 3:
- query=
- "INSERT INTO `computer_hw`.`ram` "
- + "(`memory_type`, `clock_speed`, `memory_capacity`, `power`, `price`, `brand`, `model`, `id`, `URL`)"
- + " VALUES ('" + ((RAM)hardware).DDR + "', '" + ((RAM)hardware).mhz + "', '"+ ((RAM)hardware).memory +
- "', '" + (hardware).power + "', '" + hardware.price +"', '"+ hardware.brand +
- "', '"+ hardware.model +"', '" + 0 +"', '" + (hardware).url.toString() +"')";
- break;
- case 4:
- query=
- "INSERT INTO `computer_hw`.`gpu` "
- + "(`id`, `brand`, `model`, `price`, `power`, `memory_capacity`, `URL`)"
- + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
- "', '" + (hardware).power + "', '" + ((GPU)hardware).memory +"', '"+ (hardware).url.toString() +"')";
- break;
- case 5:
- System.out.println("heyy");
- query=
- "INSERT INTO `computer_hw`.`hdd` "
- + "(`id`, `brand`, `model`, `price`, `power`, `type`, `memory`, `URL`)"
- + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price + "', '" + (hardware).power
- + "', '" + ((HDD)hardware).type + "', '" + ((HDD)hardware).memory +"', '"+ (hardware).url.toString() +"')";
- //System.out.println(query);
- break;
- case 6:
- query=
- "INSERT INTO `computer_hw`.`psu` "
- + "(`id`, `brand`, `model`, `price`, `power_output`, `URL`)"
- + " VALUES (NULL, '" + hardware.brand + "', '" + hardware.model + "', '"+ hardware.price +
- "', '" + ((PSU)hardware).power_output +"', '"+ (hardware).url.toString() +"')";
- //System.out.println(query);
- break;
- }
- System.out.println("hey2");
- } else {
- switch(i){
- case 1:
- whatAreWeDeleting = "mobo";
- break;
- case 2:
- whatAreWeDeleting = "cpu";
- break;
- case 3:
- whatAreWeDeleting = "ram";
- break;
- case 4:
- whatAreWeDeleting = "gpu";
- break;
- case 5:
- whatAreWeDeleting = "hdd";
- break;
- case 6:
- whatAreWeDeleting = "psu";
- break;
- }
- query = "DELETE FROM `computer_hw`.`" + whatAreWeDeleting + "`" +
- "WHERE `" + whatAreWeDeleting + "`.`model`" + " = '" + hardware.model + "'";
- }
- System.out.println(query);
- statement.executeUpdate(query);
- conn.close();
- /*Thread t = new Thread(new Runnable(){
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- Thread.sleep(500);
- MainFrame.updateDB();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- t.start();*/
- } catch (Exception e) {
- System.out.println(e.toString());
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement