Began to deal with servlets and jsp. It's time to connect to the database, but there was a problem. Please help.

  1. Threw mysql-connector-java-5.1.39-bin.jar into the Tomcat / lib folder
  2. In the project, created a webapp / META-INF directory, in it created context.xml , which contains:

    <?xml version='1.0' encoding='utf-8'?> <Context> <Resource name="jdbc/library" auth="Container" description="DB Connection" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/library" maxActive="10" maxIdle="3" maxWait="100" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" type="javax.sql.DataSource"></Resource> </Context> 
  3. Added the following to web.xml:

     <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/library</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 
  4. To check the connection using this code:

      public void check() { try { InitialContext initialContext = new InitialContext(); DataSource dataSource = (DataSource)initialContext.lookup("java:comp/env/jdbc/library"); Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM book"); System.out.println(resultSet.next()); } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } 

Bottom line: I get java.sql.SQLException: Cannot create driver JDBC driver 'for connect URL' null '

Tell me what am I doing wrong?

  • This may be because Tomcat configuration files are formatted as schemaless XML; elements and attributes are case-sensitive ? Try to write the <Resource> tag with a capital. - Sergey Gornostaev
  • @Mihusle Read a book called Java JDBC Tutorial . There everything is laid out and understandable. - Vladimir Glinskikh

0