Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static String dbURL="jdbc:mysql://172.16.4.148:3306/nicobas_db";
- private static String username="nicobas";
- private static String password="mysql";
- public static ArrayList<Product> getProducts()
- {
- products=new ArrayList <Product>();
- try
- {
- Connection connection=DriverManager.getConnection(dbURL,username,password);
- Statement statement=connection.createStatement();
- ResultSet rs=statement.executeQuery("select * from productList");
- /*ResultSet rs=statement.executeQuery("select * from productList where Code='"+productCode+"'");*/
- while(rs.next())
- {
- Product p = new Product();
- p.setCode(rs.getString(1));
- p.setDescription(rs.getString(2));
- p.setPrice(rs.getDouble(3));
- products.add(p);
- }
- rs.close();
- statement.close();
- connection.close();
- }
- catch(Exception e)
- {
- e.getMessage();
- }
- return products;
- }
- public static void insert(Product product)
- {
- try{
- Connection connection=DriverManager.getConnection(dbURL,username,password);
- String psql = "insert into productList(Code,Description,Price)"
- + " values (?,?,?)";
- String Code=product.getCode();
- String Description=product.getDescription();
- double Price=product.getPrice();
- PreparedStatement ps=connection.prepareStatement(psql);
- ps.setString(1,Code);
- ps.setString(2,Description);
- ps.setDouble(3,Price);
- ps.executeUpdate();
- connection.close();
- ps.close();
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- }
- public static void update(Product product)
- {
- try{
- String Code=product.getCode();
- Connection connection=DriverManager.getConnection(dbURL,username,password);
- Statement statement=connection.createStatement();
- String sql=("update productList set Description =' "+product.getDescription()+"',Price = "+product.getPrice()+ "where Code='"+Code
- +"'");
- statement.executeUpdate(sql);
- statement.close();
- connection.close();
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- for (int i = 0; i < products.size(); i++)
- {
- Product p = products.get(i);
- if (product.getCode() != null &&
- product.getCode().equalsIgnoreCase(p.getCode()))
- {
- products.set(i, product);
- }
- }
- }
- public static void delete(Product product)
- {
- try{
- Connection connection=DriverManager.getConnection(dbURL,username,password);
- Statement statement=connection.createStatement();
- String sql="delete from productList where Code='"+product.getCode()
- +"'";
- statement.executeUpdate(sql);
- statement.close();
- connection.close();
- }
- catch(Exception e)
- {
- System.out.println(e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment