html
1<!DOCTYPE html> 2<html> 3<head> 4<meta charset="UTF-8"> 5<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 6<link rel="stylesheet" type="text/css" href="style.css"> 7<title>login page</title> 8</head> 9<body class="login"> 10<div class="frameborder1"> 11<h1>ログイン</h1> 12<div class="frameborder2"> 13<div class="content"> 14<form action="/webkensyu/Relay" method="post"> 15<p class="loginp"><label>ユーザー名<input type="text" name="username" class="input1" placeholder="ユーザ名" required ></label></p> 16<p class="loginp"><label>パスワード<input type="password" name ="pass" class="input1" id="password2" placeholder="password" maxlength="10" pattern="[a-zA-Z0-9]+" required></label></p> 17<div class="button"> 18<button type="submit" name="button1">ログイン</button><br> 19</div> 20</form> 21<div class="button"> 22<form action="/webkensyu/Relay" method="get"> 23<button type="submit" name="button2">ユーザー作成</button> 24</form> 25</div> 26</div> 27</div> 28</div> 29</body> 30</html>
java
1package dao; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.PreparedStatement; 6import java.sql.ResultSet; 7import java.sql.SQLException; 8import java.util.ArrayList; 9 10public class DBIO { 11 12 protected static final String DBNAME = "jdbc:mysql://localhost:3306/webkensyu?useUnicode=true&characterEncoding=utf8"; 13 protected static final String DBUSER = "user"; 14 protected static final String DBPASS = "pass"; 15 protected Connection conn; 16 public DBIO(){ 17 this.connect(); 18} 19 public void connect() { 20 try { 21 Class.forName("com.mysql.jdbc.Driver"); 22 conn = DriverManager.getConnection(DBNAME,DBUSER,DBPASS); 23 } catch(SQLException e) { 24 e.printStackTrace(); 25 }catch(ClassNotFoundException e) { 26 e.printStackTrace(); 27 } 28 } 29 30 //ユーザー一覧表示 31 public ArrayList<String> getUser(){ 32 33 ArrayList<String> result = new ArrayList<String>(); 34 35 try { 36 this.connect(); 37 38 String sql ="SELECT user_name FROM users"; 39 PreparedStatement pstmt = conn.prepareStatement(sql); 40 ResultSet rs = pstmt.executeQuery(); 41 42 while(rs.next()) { 43 result.add(rs.getString("user_name")); 44 } 45 }catch(SQLException e) { 46 e.printStackTrace(); 47 }finally { 48 this.close(); 49 } 50 return result; 51 } 52 53 //パスワード変更 54 public boolean Passchange( String name, String pass){ 55 56 try{ 57 this.connect(); 58 String sql ="UPDATE users SET password = ? WHERE user_name = ?"; 59 PreparedStatement pstmt = conn.prepareStatement(sql); 60 pstmt.setString(1, pass); 61 pstmt.setString(2, name); 62 63 int rs = pstmt.executeUpdate(); 64 if(rs == 1) { 65 return true; 66 }else { 67 return false; 68 } 69 }catch (SQLException e) { 70 e.printStackTrace(); 71 }finally { 72 this.close(); 73 } 74 return false; 75 } 76 77 //ログインで入力されたユーザー名、パスワードの確認 78 public boolean Authentication(String name, String pass) { 79 try { 80 this.connect(); 81 82 String sql ="SELECT user_name,password FROM users WHERE user_name = ? && password = ? "; 83 PreparedStatement pstmt = conn.prepareStatement(sql); 84 pstmt.setString(1, name); 85 pstmt.setString(2, pass); 86 ResultSet rs = pstmt.executeQuery(); 87 88 if(rs.next()) { 89 return true; 90 }else { 91 return false; 92 } 93 }catch (SQLException e) { 94 e.printStackTrace(); 95 }finally { 96 this.close(); 97 } 98 return false; 99 } 100 101 //ユーザーの作成 102 public int Make_user(String id, String name, String pass) { 103 try { 104 105 this.connect(); 106 String sql ="INSERT INTO users(user_id,user_name,password)VALUES(?,?,?)"; 107 PreparedStatement pstmt = conn.prepareStatement(sql); 108 pstmt.setString(1, id); 109 pstmt.setString(2, name); 110 pstmt.setString(3, pass); 111 int rs = pstmt.executeUpdate(); 112 if(rs == 1) { 113 return 0; 114 }else { 115 return -1; 116 } 117 }catch (SQLException e) { 118 119 e.printStackTrace(); 120 }finally { 121 this.close(); 122 } 123 return -1; 124 } 125 126 //現在のパスワード確認 127 public boolean pass_change( String name, String pass, String new_pass) { 128 129 boolean result = false; 130 try { 131 132 this.connect(); 133 String sql ="SELECT password FROM users WHERE password = ?"; 134 PreparedStatement pstmt = conn.prepareStatement(sql); 135 pstmt.setString(1, pass); 136 ResultSet rs = pstmt.executeQuery(); 137 138 if(rs.next()) { 139 result = true; 140 }else { 141 result = false; 142 } 143 if(result == true) { 144 145 String sql_update = "UPDATE users SET password = ? WHERE user_name = ?"; 146 147 PreparedStatement pstmt2 = conn.prepareStatement(sql_update); 148 pstmt2.setString(1, new_pass); 149 pstmt2.setString(2, name); 150 151 int rst = pstmt2.executeUpdate(); 152 if(rst == 1) { 153 result = true; 154 }else { 155 result = false; 156 } 157 } 158 }catch(SQLException e) { 159 160 e.printStackTrace(); 161 }finally { 162 this.close(); 163 } 164 return result; 165 } 166 public void close() { 167 168 if(conn != null) { 169 try { 170 conn.close(); 171 }catch(SQLException e) { 172 e.printStackTrace(); 173 } 174 } 175 } 176}
form からのデータをサーブレットで受け取りサーブレットからデータベースにアクセスしたいのですが、アクセスできなくなってしまいました。(最初はできていました)
コンソールエラー内容
Fri Sep 13 10:29:37 JST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
java.sql.SQLException: Access denied for user 'user'@'localhost' (using password: YES)
at dao.DBIO.connect(DBIO.java:22)
at dao.DBIO.<init>(DBIO.java:17)
エラーがでたので調べてみて
java
1//public class DBIO 2protected static final String DBNAME = "jdbc:mysql://localhost:3306/webkensyu?useUnicode=true&characterEncoding=utf8&useSSL=false
としました。
実行すると
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public Key Retrieval is not allowed
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
とエラーがでました。
次に
java
1protected static final String DBNAME = "jdbc:mysql://localhost:3306/webkensyu?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false";
としました。
すると
java.sql.SQLException: Access denied for user 'user'@'localhost' (using password: YES)
とエラーがでました。
解決が出来なかったのでアドバイスを頂けると助かります。
バージョン
mysql Ver 14.14 Distrib 5.6.19, for Win64 (x86_64)
補足
コマンドプロンプトの方でも
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: Žw’肳‚ꂽƒ‚ƒWƒ…[ƒ‹‚ªŒ©‚‚©‚è‚Ü‚¹‚ñB
とエラーが出てしまいます。

あなたの回答
tips
プレビュー