Javaからデータベースにアクセスして、SELECTやINSERTを行う練習
JavaEEを使ってデータベースにアクセス後、取得したデータをブラウザ上に表示したり、データベースに書き込んだり
Windows10上にTomcatとMySQLをダウンロードしてきました。
MySQLについてはインストール後、デモデータ(world database)をインサートしています。
IntellJ Ultimateを使用
・データソースの設定方法
Javaプロジェクト上でweb.xmlとかcontext.xmlとか...persistence.xmlとかとか、ネットで調べるといろんな方法や記述方法が出てきて結局どれをどうやって設定すればいいかわかりません。
Java上の設定だけじゃダメなんでしょうか...
以下のぺージが正常に動作すれば接続ができたと判断しようと思っています。
java
package com.example.jpa_demo; import java.io.*;import java.sql.Connection;import java.sql.SQLException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.servlet.http.*;import javax.servlet.annotation.*;import javax.sql.DataSource; @WebServlet(name = "helloServlet", value = "/hello-servlet")public class HelloServlet extends HttpServlet { private String message; public void init() { message = "Hello World!"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { try { Context context = (Context) new InitialContext().lookup("java:/comp/env"); DataSource ds = (DataSource)context.lookup("jdbc/world"); Connection con = ds.getConnection(); } catch (NamingException | SQLException e) { throw new RuntimeException(e); } response.setContentType("text/html"); // Hello PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h1>" + message + "</h1>"); out.println("</body></html>"); } public void destroy() { }}
web.xml
xml
<?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_4_0.xsd" version="4.0"> <resource-ref> <res-ref-name>jdbc/world</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref></web-app>
comtext.xml
xml
<?xml version="1.0" encoding="UTF-8" ?><Context> <Resource name="jdbc/world" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="root" password="password" driverClassName="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/world" /></Context>
persistence.xml
xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" version="2.2"> <persistence-unit name="default"> <jta-data-source>jdbc/world</jta-data-source> </persistence-unit></persistence>
pom.xml
xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>jpa_demo</artifactId> <version>1.0-SNAPSHOT</version> <name>jpa_demo</name> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <junit.version>5.8.2</junit.version> </properties> <dependencies> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.2</version> </plugin> </plugins> </build></project>
No suitable driverって書いてあるんですがMavenにもMysql入れてるしlibのところにもあるんですけどね...よくわかりません...
Java
HTTPステータス 500 – Internal Server Errorタイプ 例外報告 メッセージ java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null' 説明 サーバーは予期しない条件に遭遇しました。それはリクエストの実行を妨げます。 例外 java.lang.RuntimeException: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null' com.example.jpa_demo.HelloServlet.doGet(HelloServlet.java:28) javax.servlet.http.HttpServlet.service(HttpServlet.java:655) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)根本原因 java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null' org.apache.tomcat.dbcp.dbcp2.DriverFactory.createDriver(DriverFactory.java:74) org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:475) org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:541) org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:747) com.example.jpa_demo.HelloServlet.doGet(HelloServlet.java:25) javax.servlet.http.HttpServlet.service(HttpServlet.java:655) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)根本原因 java.sql.SQLException: No suitable driver java.sql/java.sql.DriverManager.getDriver(DriverManager.java:298) org.apache.tomcat.dbcp.dbcp2.DriverFactory.createDriver(DriverFactory.java:59) org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:475) org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:541) org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:747) com.example.jpa_demo.HelloServlet.doGet(HelloServlet.java:25) javax.servlet.http.HttpServlet.service(HttpServlet.java:655) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)注意 原因のすべてのスタックトレースは、サーバのログに記録されています Apache Tomcat/8.5.81


0 コメント