問題点
JNDIを使ってMysqlに接続したいのですが、'No suitable driver' になってしまいます。
色々調べて試していますが、上手くいきません。
複数定義ファイルを用いるので、設定項目を何か勘違いしているか、
設定ファイルの配置場所が間違っているのでしょうか。
参考になるサイトが古いバージョンが多い事もあり、どなたかご教示いただけると幸いです。
ヒントや、参考になるサイト情報でも構いませんので、どうぞよろしくお願いいたします。
環境
Eclipse
Tomcat 9
Maven 3.5.4
Mysql 8.0.16
ここにより詳細な情報を記載してください。
発生している問題・エラーメッセージ
Caused by: java.sql.SQLException: No suitable driver at java.sql/java.sql.DriverManager.getDriver(DriverManager.java:298) at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:495) ... 32 more
該当のソースコード
pom.xml
<properties> <java.version>11</java.version> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> : : <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina</artifactId> <version>9.0.22</version> </dependency> : <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> : : <dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0.0</version> </dependency> : <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> <!-- Runs the JUnit unit tests. Creates test reports. --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <!-- run Maven-webapp in Tomcat --> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.7.7</version> <configuration> <container> <containerId>tomcat9x</containerId> <type>embedded</type> </container> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <!-- <artifactId>tomcat8-maven-plugin</artifactId> <version>3.0-r1756463</version> --> <configuration> <path>/upload_image_db4</path> <!-- <server>tomcat-localhost</server> --> <server>TomcatServer</server> <url>http://localhost:8080/manager/text</url> <username>tomcat</username> <password>password</password> <contextFile>${basedir}/src/main/webapp/META-INF/context.xml</contextFile> <useTestClasspath>false</useTestClasspath> <systemProperties> <systemProperty> <name>maven.tomcat.port</name> <value>8090</value> </systemProperty> </systemProperties> </configuration> <executions> <execution> <id>tomcat7-run</id> <goals> <goal>run-war-only</goal> </goals> <phase>package</phase> </execution> <execution> <id>tomcat-shutdown</id> <goals> <goal>shutdown</goal> </goals> <phase>deploy</phase> <configuration> <fork>true</fork> <port>8090</port> </configuration> </execution> </executions> : : </plugin> </plugins> </pluginManagement> <finalName>upload_image_db4</finalName> </build> </project>
Maven プロジェクトのディレクトリ構成
Context.xml
<Context> <Resource name="jdbc/Practice_MVC" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="root" password="seri331" driverClassName="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/Practice_MVC"/> <!-- url="jdbc:mysql://mysqlserver:3306/Practice_MVC"/> --> </Context>
web.xml
<web-app> <display-name>Archetype Created Web Application</display-name> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/upload_image_db4</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
GetDataSourceLogic.java
public DataSource getDataSource() throws NamingException { Context initContext = new InitialContext(); Context context = (Context)initContext.lookup("java:/comp/env/"); DataSource source = (DataSource)context.lookup("jdbc/upload_image_db4");//java:/comp/env/jdbc/upload_image_db4 return source; }
###参考にさせていただいたサイト
https://qiita.com/zaki-lknr/items/8137ac40ebd8f5bdb3c5
https://codehaus-cargo.github.io/cargo/Home.html
https://stackoverflow.com/questions/45771942/sqlexception-no-suitable-driver-found-for-jdbcmysql-between-two-maven-projects
他諸々。
###追記
xebme様のおかげで、大変参考になるF&Qに辿り着いたので、載せておきます。
https://stackoverflow.com/questions/3485177/how-to-configure-tomcat-to-connect-with-mysql
回答2件
あなたの回答
tips
プレビュー