0

I created a Dynamic Web Project in Eclipse, which is supposed to interact with a database. I added the database in the project, I added all the necessary texts in the appropriate files to connect the database with the servlets, but for some reason the servlets fail to communicate with the database. For example, there is an option for displaying the contents of a table. When I select that option, instead of displaying the table's contents, the code inside the catch section of the try/catch runs. Also, there are 2 options involving entering data into tables. When I enter data in the html pages and press submit, nothing happens. It should display to me that the data entry was successful.

What am I missing? What I'm doing wrong?

Here is the project: http://www.mediafire.com/file/pwu46ohgd3qs0pr/PhoneBillsApp2.zip/file

I'm using Eclipse 2020-03 (4.15.0) and Tomcat 8

Any help will be appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Antonis
  • 7
  • 1
  • 8

2 Answers2

0

You need to check the log files of the tomcat server for any exceptions. The logs are usually located under $CATALINA_HOME/logs

Verify that:

  • You have the datasource defined in the Tomcat's context.xml: $CATALINA_HOME/conf/context.xml Should contain something like:
<Resource name="jdbc/LiveDataSource"
 auth="Container"
 type="javax.sql.DataSource"
 maxTotal="100"
 maxIdle="30"
 maxWaitMillis="10000"
 username="xxx"
 password="yyy"
 driverClassName="com.mysql.jdbc.Driver"
 url="jdbc:mysql://localhost:3306/mydb"/>
  • If you run the application inside Eclipse, go to "Java EE" perspective, in "Project explorer" view, under "Servers/Tomcat xxxx" resource you should have context.xml file. Check if it also contains the "jdbc/LiveDataSource"
  • You should have records parent tables before inserting a raw with foreign keys (like programme)
  • You have a SQL error at DBServlet line 169: seller should be a escaped with single quotes, e.g. insertStmt2 += "'seller'" + ","; not `insertStmt2 += "seller" + ",";

In order to avoid SQL injection, I would suggest to use PreparedStatement instead of constructing the SQL commands inside the servlet. Consider using some ORM like Hibernate, EclipseLink, etc.

UPDATE 1 Location of CATALINA_HOME and CATALINA_BASE can be found from the launch configuration

launch_configuration_arguments

Redirecting a log file to a custom location: Open launch configuration / Apache Tomcat / Tomcat 8 (or 9) / Commons tab / Output file enter image description here

UPDATE 2 Check that the $CATALINA_BASE/conf/context.xml (in my case C:\projects\workspaces\delme\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\context.xml) contains the jdbc/LiveDataSource.

Also when you cath exception in servlet code log the stack trace otherwise it won't be visible in the log files e.g.:

        try {
           // ...some code here...
        } catch(Exception e) {
            out.println("Problem with the database connection");
            e.printStackTrace(); // or better use loggers
            // private final Logger logger = Logger.getLogger(getClass().getName()); 
            // logger.log(Level.SEVERE, "Problem with the database connection", e);
        }
k1r0
  • 542
  • 1
  • 5
  • 12
0

I fixed it. I needed to set the server's timezone to UTC, and replace "LiveDataSource" with the name of the DB file. The datasource now looks like this:

<Resource name="jdbc/phoneBills"
 auth="Container"
 type="javax.sql.DataSource"
 maxTotal="100"
 maxIdle="30"
 maxWaitMillis="10000"
 username="root"
 password="12345678"
 driverClassName="com.mysql.cj.jdbc.Driver"
 url="jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC"/>
Antonis
  • 7
  • 1
  • 8