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.

  • In which line of your code is the name extracted from the query result? - Sergey
  • @Sergey In theory, he should take this name 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
  • Is he supposed to take by force of thought? Is this your code or someone else's? Can you imagine what he does in each line of the listing? - Sergey
  • @Sergey Hmm, I wrote this code while looking at the documentation) but I don’t really understand: D: D I just learn to connect the database - E1mir

2 answers 2

This is not needed in javaee:

 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); 

Do something like this:

 public class GrdServlet extends ... Resource("jdbc/grdDS") // сервер внедряет private DataSource grdDS; // базу данных из своей конфигурации ... public void doGet(...) { Connection grdCON = grdDS.getConnection(); ... } ... } 

1) The jdbc driver should be placed in the server's shared library folder.
2) On the server the application must be created database configuration. In this configuration, the driver, database server, database name, login are specified (everything that was previously attempted in the program is put into the server configuration).
3) The configuration is assigned the name JNDI java:/jdbc/grdDS (must match the Resource("jdbc/grdDS") ).

The library folder, configuration, and resource naming are all specific to each server.

Often this can be done through the server's web-face.

This is the simplest option (with its flaws, which in most cases is not the case itself).

  • Vooo worked))) Thank you :))) Thanks for the reply) - E1mir
  if(grdRS.next()){ msgDB = grdRS.toString(); String username = rs.getString("name"); } 
  • Nope, I did it, it did not help ... All the time he writes on the h1 tag NULL - E1mir
  • @KryTer_NexT how many lines in the database? Throw a screen request, please. - Senior Pomidor
  • There are only 4 lines in the database, if you enter SELECT * FROM users; there will be 4 people, with ID = 4,5,6,7 ... And if SELECT name FROM users; then just 4 names) This is a test database, I'm new, for the first time I connect the database ... - E1mir
  • @KryTer_NexT and when you debug, it shows NULL every time? - Senior Pomidor
  • @senor Now generally writes that JDBC not found ... How to fix? I did not connect it like? xDD Iliiii ....... - E1mir pm