0

I've been trying to configure Tomcat to access MySQL database in IntelliJ but without much success.

I have placed mysql-connector-java-5.1.20. jar into Tomcat8.5/lib/

This is my context.xml file that is placed in WEB-INF/lib/

<Context>
   <WatchedResources>WEB-INF/web.xml</WatchedResources>

   <Resourse name="jdbc/web_student_tracker"
      auth="Container" type="javax.sql.DataSource"
      maxActive="20" maxIdle="5" maxWait="10000"
      username="webstudent" password="webstudent"
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/web_student_tracker"/>
</Context>

Web.xml file is place in WEB-INF/

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

  <resource-ref>
    <description>MySQL Data Source Example</description>
    <res-ref-name>jdbc/web_student_tracker</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

This is what my servlet looks like

 @WebServlet(name = "TestServlet", urlPatterns = {"/testServlet","/a"})
 public class TestServlet extends HttpServlet {

 //define datasource/connection pool for resource injection
 @Resource(name="jdbc/web_student_tracker")
 private DataSource dataSource;

 protected void doPost(HttpServletRequest request, HttpServletResponse 
  response) throws ServletException, IOException {

 }

  protected void doGet(HttpServletRequest request, HttpServletResponse 
  response) throws ServletException, IOException {
    //set the printwriter
    PrintWriter out = response.getWriter();
    response.setContentType("text/plain");

    //get a connection to the database
    Connection myConn =null;
    Statement myStmt = null;
    ResultSet myRs = null;

    try {
        // create a sql statements
        myConn = dataSource.getConnection(); // fails here

        // execute sql query
        String sql = "select * from student";

        myStmt = myConn.createStatement();

        //process the result set
        myRs = myStmt.executeQuery(sql);

        while(myRs.next())  {
            String email = myRs.getString("email");
            out.println(email);
        }
    }
    catch ( Exception exc)  {
        exc.printStackTrace();
    }
  }
 }

When I try to get a connection from a datasource, I get the following exception:

java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 
'null'
...
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
...

What am I doing wrong or missing ? Thank you for your time and help!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
uros
  • 9
  • 2

0 Answers0