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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 | package cartexample;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
public class ProductDAO {
private Connection con = null;
PreparedStatement stmt1 = null;
public List<Product> getAllProducts() throws SQLException {
try {
String sqlquery= "SELECT * FROM product_cart_table;";
ResultSet rs;
stmt1 = con.prepareStatement(sqlquery);
rs = stmt1.executeQuery();
List<Product> productList = new ArrayList<Product>();
//Product product = new Product();
while(rs.next()) {
productList.add( new Product(rs.getString("pcode"),
rs.getString("pname"), rs.getDouble("pprice"), rs.getString("pimage"), 1) );
}
return productList;
} catch (SQLException e) {
throw new SQLException("An error occured while getting products from database: " + e.getMessage());
}
}
/**
* Default Constructor.
*/
public ProductDAO() {
}
public void open() throws SQLException {
try {
// dynamically load the driver's class file into memory
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
throw new SQLException("MySQL Driver error: " + e.getMessage());
}
try {
// establish a connection with the database and creates a Connection
// object (con)
con = DriverManager.getConnection("jdbc:mysql://195.251.249.131:3306/eloi_stuff", "eloi_stuff", "*************");
} catch (Exception e) {
con = null;
// throw SQLException if a database access error occurs
throw new SQLException("Could not establish connection with the Database Server: " + e.getMessage());
}
} // End of open
public void close() throws SQLException {
try {
// if connection is open
if (con != null)
con.close(); // close the connection to the database to end database session
if (stmt1 != null)
stmt1.close();
} catch (Exception e3) {
throw new SQLException("Could not close connection with the Database Server: " + e3.getMessage());
}
}// end of close
}
|