eclipseを使用し、javaの学習を進めています。
下記のサイトを参考にeclipseでプロジェクトを作成して動作確認をしようとしています。
[http://stroll.hatenablog.com/entry/2015/11/22/154644]
リンク内容
eclipse、tomcat、MySQLはインストール済みで、
下記バージョン使用で、
jdbcドライバ:mysql-connector-java-8.0.12.jar
Mysql:Ver 8.0.12 for osx10.13 on x86_64 (Homebrew)
プロジェクトを作り、上記を参考にし、実行すると下記のような結果になります。
デバッグすると、
conn = DriverManager.getConnection("jdbc:mysql://localhost/SAMPLEDB", "root", "");
の箇所から、e.printStackTrace();に移動します。
コンソールログは途中で止まっているように思えます(途中は文字数超過のため省略)。
10 23, 2018 10:49:05 午後 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:DBSampleServlet' did not find a matching property. 10 23, 2018 10:49:05 午後 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TodoServlet' did not find a matching property. 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Server version: Apache Tomcat/8.0.53 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Server built: Jun 29 2018 14:42:45 UTC 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Server number: 8.0.53.0 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: OS Name: Mac OS X 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: OS Version: 10.13.6 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Architecture: x86_64 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: JVM Version: 1.8.0_181-b13 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: JVM Vendor: Oracle Corporation 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: CATALINA_BASE: /Users/sawada/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: CATALINA_HOME: /Applications/apache-tomcat-8.0.53 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Command line argument: -Dcatalina.base=/Users/sawada/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Command line argument: -Dcatalina.home=/Applications/apache-tomcat-8.0.53 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Command line argument: -Dwtp.deploy=/Users/sawada/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Command line argument: -Djava.endorsed.dirs=/Applications/apache-tomcat-8.0.53/endorsed 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.VersionLoggerListener log 情報: Command line argument: -Dfile.encoding=UTF-8 10 23, 2018 10:49:05 午後 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 略 10 23, 2018 10:49:05 午後 org.apache.coyote.AbstractProtocol init 情報: Initializing ProtocolHandler ["http-nio-8080"] 10 23, 2018 10:49:05 午後 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 情報: Using a shared selector for servlet write/read 10 23, 2018 10:49:05 午後 org.apache.coyote.AbstractProtocol init 情報: Initializing ProtocolHandler ["ajp-nio-8009"] 10 23, 2018 10:49:05 午後 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 情報: Using a shared selector for servlet write/read 10 23, 2018 10:49:05 午後 org.apache.catalina.startup.Catalina load 情報: Initialization processed in 827 ms 10 23, 2018 10:49:05 午後 org.apache.catalina.core.StandardService startInternal 情報: サービス Catalina を起動します 10 23, 2018 10:49:05 午後 org.apache.catalina.core.StandardEngine startInternal 情報: Starting Servlet Engine: Apache Tomcat/8.0.53 10 23, 2018 10:49:05 午後 org.apache.jasper.servlet.TldScanner scanJars 情報: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 10 23, 2018 10:49:06 午後 org.apache.jasper.servlet.TldScanner scanJars 略 情報: Starting ProtocolHandler ["http-nio-8080"] 10 23, 2018 10:49:06 午後 org.apache.coyote.AbstractProtocol start 情報: Starting ProtocolHandler ["ajp-nio-8009"] 10 23, 2018 10:49:06 午後 org.apache.catalina.startup.Catalina start 情報: Server startup in 1223 ms com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919) at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3996) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2142) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:781) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) at 略
一応ソースは下記になります(参考にしたサイトとほとんど同じです)
文字数制限のため一部省きます。上記参考サイトをご覧ください。
java
1package chapter6; 2 3略 4 5/** 6 * Servlet implementation class SelectSampleServle 7 */ 8@WebServlet("/SelectSampleServlet") 9public class SelectSampleServlet extends HttpServlet { 10 private static final long serialVersionUID = 1L; 11 12 /** 13 * @see HttpServlet#HttpServlet() 14 */ 15 public SelectSampleServlet() { 16 super(); 17 // TODO Auto-generated constructor stub 18 } 19 20 /** 21 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 22 * response) 23 */ 24 @Override 25 protected void doGet(HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException { 27 // TODO Auto-generated method stub 28 java.sql.Connection conn = null; 29 Statement stmt = null; 30 ResultSet rs = null; 31 ArrayList<String> list = new ArrayList<String>(); 32 try { 33 Class.forName("com.mysql.jdbc.Driver"); 34 conn = DriverManager.getConnection("jdbc:mysql://localhost/SAMPLEDB", "root", ""); 35 36 stmt = conn.createStatement(); 37 rs = stmt.executeQuery("Select * from student"); 38 39 while (rs.next()) { 40 String s = "<td>" + rs.getInt(1) + "</td>" + "<td>" + rs.getString(2) + "</td>" + "<td>" + rs.getInt(3) 41 + "</td>"; 42 list.add(s); 43 } 44 } catch (Exception e) { 45 // TODO: handle exception 46 e.printStackTrace(); 47 } finally { 48 if (rs != null) { 49 try { 50 rs.close(); 51 } catch (SQLException sqlEx) { 52 53 } 54 } 55 if (conn != null) { 56 try { 57 conn.close(); 58 } catch (SQLException sqlEx) { 59 60 } 61 } 62 } 63 略 64}
こちらはデータが表示されます。
package chapter6; import java.sql.*; public class MySqlTest { public static void main(String[] args) { Connection con = null; try { // JDBCドライバのロード - JDBC4.0(JDK1.6)以降は不要 Class.forName("com.mysql.jdbc.Driver").newInstance(); // MySQLに接続 con = DriverManager.getConnection("jdbc:mysql://localhost/SAMPLEDB", "root", ""); System.out.println("MySQLに接続できました。"); Statement stm = con.createStatement(); String sql = "Select * from student"; ResultSet rs = stm.executeQuery(sql); while(rs.next()){ int id = rs.getInt("STUDENTNO"); String name = rs.getString("STUDENTNAME"); System.out.println("取得結果 -> " + id + ":" + name); } } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { System.out.println("JDBCドライバのロードに失敗しました。"); } catch (SQLException e) { System.out.println("MySQLに接続できませんでした。"); } finally { 略 } } }
tomcatを通してdbに繋がらないと思われませすが、
tomcatは起動しているように見えますがdbに繋がらないのをどうやって解決したらいいかわかりません。
ご教授いただけると助かります。
宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー