Guest User

Untitled

a guest
Apr 15th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. private static String dbURL="jdbc:mysql://172.16.4.148:3306/nicobas_db";
  2.     private static String username="nicobas";
  3.     private static String password="mysql";
  4. public static ArrayList<Product> getProducts()
  5.     {
  6.         products=new ArrayList <Product>();
  7.         try
  8.         {
  9.                     Connection connection=DriverManager.getConnection(dbURL,username,password);
  10.                     Statement statement=connection.createStatement();
  11.                     ResultSet rs=statement.executeQuery("select * from productList");
  12.             /*ResultSet rs=statement.executeQuery("select * from productList where Code='"+productCode+"'");*/
  13.                     while(rs.next())
  14.                 {
  15.                     Product p = new Product();
  16.                     p.setCode(rs.getString(1));
  17.                     p.setDescription(rs.getString(2));
  18.                     p.setPrice(rs.getDouble(3));
  19.                     products.add(p);
  20.                 }
  21.         rs.close();
  22.         statement.close();
  23.         connection.close();
  24.         }
  25.         catch(Exception e)
  26.         {
  27.             e.getMessage();
  28.         }
  29.  
  30.         return products;
  31.     }
  32.  
  33. public static void insert(Product product)
  34.     {
  35.         try{
  36.             Connection connection=DriverManager.getConnection(dbURL,username,password);
  37.             String psql = "insert into productList(Code,Description,Price)"
  38.             + " values (?,?,?)";
  39.             String Code=product.getCode();
  40.             String Description=product.getDescription();
  41.             double Price=product.getPrice();
  42.             PreparedStatement ps=connection.prepareStatement(psql);
  43.             ps.setString(1,Code);
  44.             ps.setString(2,Description);
  45.             ps.setDouble(3,Price);
  46.             ps.executeUpdate();
  47.             connection.close();
  48.             ps.close();
  49.            }
  50.         catch(Exception e)
  51.         {
  52.             System.out.println(e.getMessage());
  53.         }
  54.     }
  55.  
  56.     public static void update(Product product)
  57.     {
  58.         try{
  59.         String Code=product.getCode();
  60.         Connection connection=DriverManager.getConnection(dbURL,username,password);
  61.         Statement statement=connection.createStatement();
  62.         String sql=("update productList set Description =' "+product.getDescription()+"',Price = "+product.getPrice()+  "where Code='"+Code
  63.                 +"'");
  64.         statement.executeUpdate(sql);
  65.         statement.close();
  66.         connection.close();
  67.  
  68.  
  69.         }
  70.         catch(Exception e)
  71.         {
  72.             System.out.println(e.getMessage());
  73.         }
  74.        
  75.        
  76.        
  77.        
  78.         for (int i = 0; i < products.size(); i++)
  79.         {
  80.             Product p = products.get(i);
  81.             if (product.getCode() != null &&
  82.                 product.getCode().equalsIgnoreCase(p.getCode()))
  83.             {
  84.                 products.set(i, product);
  85.             }
  86.         }
  87.     }
  88.    
  89.     public static void delete(Product product)
  90.     {
  91.         try{
  92.         Connection connection=DriverManager.getConnection(dbURL,username,password);
  93.         Statement statement=connection.createStatement();
  94.         String sql="delete from productList where Code='"+product.getCode()
  95.                 +"'";
  96.         statement.executeUpdate(sql);
  97.         statement.close();
  98.         connection.close();
  99.  
  100.  
  101.         }
  102.         catch(Exception e)
  103.         {
  104.             System.out.println(e.getMessage());
  105.         }
  106.  
  107.     }
  108. }
Add Comment
Please, Sign In to add comment