Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Profession;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class operation {
- private ArrayList <product> products = new ArrayList<product>();
- public ArrayList<product> getProducts() {
- return products;
- }
- public void setProducts(ArrayList<product> products) {
- this.products = products;
- }
- public void add (product p){
- try {
- //1
- Class.forName("com.mysql.jdbc.Driver");
- //2
- java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/productmanagement","root","");
- //3
- PreparedStatement pr = cn.prepareStatement("INSERT INTO product VALUES (NULL,?,?,?,?)");
- pr.setString(1, p.getName());
- pr.setString(2, p.getDescr());
- pr.setInt(3, p.getPrice());
- pr.setInt(4, p.getState());
- //4
- pr.execute();
- } catch (Exception ex) {
- //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
- ex.printStackTrace();
- }
- //products.add(p);
- }
- public void remove (Long id){
- try {
- //1
- Class.forName("com.mysql.jdbc.Driver");
- //2
- java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/productmanagement","root","");
- //3
- PreparedStatement pr = cn.prepareStatement("DELETE FROM product WHERE id =?");
- pr.setLong(1, id);
- //4
- pr.execute();
- } catch (Exception ex) {
- //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
- ex.printStackTrace();
- }
- /*for (product p : products ){
- if (p.getId()==id){
- products.remove(p);
- break;
- }
- }*/
- }
- public ArrayList getAll(){
- ArrayList listproduct = new ArrayList<product>();
- try {
- //1
- Class.forName("com.mysql.jdbc.Driver");
- //2
- java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/productmanagement","root","");
- //3
- PreparedStatement pr = cn.prepareStatement("SELECT * FROM product ORDER BY id");
- //4
- ResultSet rs = pr.executeQuery();
- //5
- while (rs.next()){
- product p = new product();
- p.setId (rs.getLong("id"));
- p.setName (rs.getString("name"));
- p.setDescr (rs.getString("descr"));
- p.setPrice (rs.getInt("price"));
- p.setState (rs.getInt("state"));
- listproduct.add(p);
- }
- } catch (Exception ex) {
- //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
- ex.printStackTrace();
- }
- return listproduct;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement