-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProductDb.java
More file actions
60 lines (51 loc) · 1.96 KB
/
ProductDb.java
File metadata and controls
60 lines (51 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package productpackage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
class ProductDb {
Statement st;
void addProduct(String product_name,String product_image, float product_price, String product_description) {
connectToDb();
String sql="INSERT INTO product_table(Product_Name,Product_Image,Product_Price,Product_Description) VALUES('"+product_name+"','"+product_image+"',"+product_price+",'"+product_description+"')";
try {
st.executeUpdate(sql);
} catch (SQLException ex) {
Logger.getLogger(ProductDb.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void connectToDb() {
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/productdb";
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(url,"root","");
st=con.createStatement();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(ProductDb.class.getName()).log(Level.SEVERE, null, ex);
}
}
ArrayList viewItem() {
connectToDb();
String query="SELECT * FROM product_table";
List items=new ArrayList();
ResultSet rs;
try {
rs = st.executeQuery(query);
while(rs.next())
{
items.add(rs.getString("Product_Name"));
items.add(rs.getString("Product_Description"));
items.add(rs.getString("Product_Price"));
}
} catch (SQLException ex) {
Logger.getLogger(ProductDb.class.getName()).log(Level.SEVERE, null, ex);
}
return (ArrayList) items;
}
}