Began to deal with servlets and jsp. It's time to connect to the database, but there was a problem. Please help.
- Threw mysql-connector-java-5.1.39-bin.jar into the Tomcat / lib folder
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>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>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?
<Resource>tag with a capital. - Sergey Gornostaev