質問です。
javaでmysqlに接続したのですが、エラーが起きます
コードは以下の通りです。
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Testdata2 { public static void main(String[] args) { /* 事前準備(JAR配置を含む) */ try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { //JDBCドライバが見つからない場合の処理 e.printStackTrace(); } Connection con = null; try { /* データベースの接続 */ // JDBC URLを指定 con = DriverManager.getConnection("jdbc:mysql://”自分のIPアド”:3306/goods","root",""); /* SQL送信完了 */ /**************************/ PreparedStatement pstmt = con.prepareStatement("SELECT * FROM employees WHERE salary >= ?" ); pstmt.setInt(1,300000); ResultSet rs = pstmt.executeQuery(); //行が見つかればtrueが返る if(rs.next()) { System.out.println(rs.getInt("salary")); } else { System.out.println("該当データは存在しません"); } rs.close(); pstmt.close(); //SQLを実行 //メインのDB操作処理 /**************************/ } catch (SQLException e) { // 接続や、SQL処理の失敗時の処理 e.printStackTrace(); } finally { /* データベース接続の切断 */ if (con != null) { try { con.close(); } catch (SQLException e) { //接続失敗時の処理 e.printStackTrace(); } } } } }
エラー表示は以下の通りです。
java.sql.SQLException: null, message from server: "Host 'peanuts' is not allowed to connect to this MariaDB server"
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at sample.Testdata2.main(Testdata2.java:37)
サーバーのアクセス権限がないといわれるのですが、新たに設定しないと接続できないのでしょうか。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/09 13:05