I want to connect to the database in java EE, take from there at least one name from the database, but this does not work ... Here is the code in the servlet:
Connection grdCON = null; Statement grdST = null; ResultSet grdRS=null; String grdDB = "jdbc:mysql://localhost:3306/graduate-work-db"; String user = "root"; String password = "paswrd"; String msgDB=null; try{ Class.forName("com.mysql.jdbc.Driver"); grdCON = DriverManager.getConnection(grdDB, user, password); grdST = grdCON.createStatement(); grdRS = grdST.executeQuery("SELECT name FROM users;"); if (grdRS.next()) { msgDB = grdRS.getString(1); request.setAttribute("message",msgDB); } catch (SQLException | ClassNotFoundException excp){ msgDB = excp.getMessage(); } finally { try { if (grdRS!=null){ grdRS.close(); } if (grdST !=null){ grdST.close(); } if (grdCON!=null){ grdCON.close(); } }catch (SQLException excp){ msgDB = excp.getMessage(); } } request.getRequestDispatcher("/index.jsp").forward(request, response);
After this whole thing, it seems he should connect to the database to take from there name
with ID=5
but he does not do this ... What am I doing wrong? Where is the mistake? Here is the JSP
where this name should appear ...
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Test</title> </head> <body> Test <br> <br> <h1><%=request.getAttribute("message")%></h1> </body> </html>
The h1
tag is written NULL ... And in theory, it should have been my name from the database.
name
extracted from the query result? - Sergeyname
from the database and show it on the h1 tag, but it does not do it ... I just learn it, so I ran into this problem - E1mir