Еть метод по считыванию информации с БД :
public class DataBaseMethods {
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_java_lab6","root","root");
return connection;
public static List<Product> getProductsByOriginId(int id_origin) throws SQLException {
List<Product> res = new ArrayList<Product>();
Connection conn= getConnection();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM products WHERE frombasket=" + id_origin + ";");
while(rs.next()){
Product p = new Product(rs.getString("name"),rs.getTimestamp("dateofcreation").toLocalDateTime().toLocalDate(),rs.getInt("shelftime"),rs.getDouble("price"));
res.add(p);
}
return res;
}
public static List<Basket> getAllBaskets() throws SQLException {
List<Basket> res = new ArrayList<Basket>();
Connection conn= getConnection();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM baskets;");
while(rs.next()){
Basket b= new Basket(rs.getTimestamp("time_of_pick_up").toLocalDateTime().toLocalDate(),rs.getString("color"),rs.getInt("spaciousness"),getProductsByOriginId(rs.getInt("id")));
res.add(b);
}
rs.close();
return res;
}
}
}
Если запустить это в main , то проблем не будет :
List<Basket> b = null;
try {
b = new DataBaseMethods().getAllBaskets();
for(Basket temp_p : b){
System.out.println(temp_p.getColor());
}
} catch (SQLException e) {
e.printStackTrace();
}
Но если взять этот же код в servlet ,то заходит в SQLException :
@WebServlet(name = "MyServlet")
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String operation = request.getParameter("operation");
List<Basket> b=null;
try {
b= new DataBaseMethods().getAllBaskets();
for(Basket temp_b :b){
out.print(temp_b.getColor());
}
} catch (SQLException e) {
out.println("sql error");
}
//out.println(operation);
// out.print(operation);
if(operation.equals("All Baskets")){
try {
b= new DataBaseMethods().getAllBaskets();
for(Basket temp_b :b){
out.print(temp_b.getColor());
}
} catch (SQLException e) {
out.println("sql error");
}
}
out.close();
}
}
Как развивать веб-проекты в 2026 году: технологии, контент E-E-A-T и факторы доверия
Современные инструменты для криптотрейдинга: как технологии помогают принимать решения
Апостиль в Лос-Анджелесе без лишних нервов и бумажной волокиты
Основные этапы разработки сайта для стоматологической клиники