前提・実現したいこと
・JavaプログラムからのMySQLサーバー接続
・コンパイル時のエラーは起きないが、mysqlのドライバを定義(?)した時に
ClassNotFoundExceptionが出てしまう。
発生している問題・エラーメッセージ
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at com.gmail.rotoyutoriapp.newSQL.<init>(newSQL.java:22) at com.gmail.rotoyutoriapp.Player.SQLOrder.newPlayerData(SQLOrder.java:13) at com.gmail.rotoyutoriapp.Player.NewPlayer.newPlayerID(NewPlayer.java:12) at com.gmail.rotoyutoriapp.onEnableProcessing.onEnable(onEnableProcessing.java:18) at com.gmail.rotoyutoriapp.Main.main(Main.java:11) java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
該当のソースコード
newSQL
1import java.sql.Connection; 2import java.sql.DriverManager; 3import java.sql.SQLException; 4import java.sql.Statement; 5import java.lang.Exception; 6 7public class newSQL { 8 9 public Connection objCon = null; 10 public Statement statement = null; 11 public boolean isConnect = false; 12 13 public newSQL() { 14 String dbName = ServerProperty.dbName;//DB名 15 String user = ServerProperty.userName;//ユーザ名 16 String pass = ServerProperty.pass;//パスワード 17 18 try { 19 //JDBCドライバを 20 Class.forName("com.mysql.jdbc.Driver"); 21 //接続開始 localhost:61229 は、各PCの接続ポート、IPを指定する 22 objCon = DriverManager.getConnection( 23 "jdbc:mysql://" + ServerProperty.ip + ":3306/" //接続開始 24 + dbName + "?useSSL=false", 25 user, pass); 26 statement = objCon.createStatement(); 27 isConnect = true; 28 System.out.println("DBへの接続成功"); 29 } catch(Exception objEx) { 30 //err 31 getLogger.info("ドライバロード失敗"); 32 objEx.printStackTrace(); 33 System.err.println(objEx.getClass().getName() + ":" + objEx.getMessage()); 34 isConnect = false; 35 } 36 } 37 38 public void close() { 39 try { 40 this.objCon.close(); 41 System.out.println("DBへの接続を切断"); 42 } catch (SQLException e) { 43 e.printStackTrace(); 44 } 45 }
SQLOrder
1import com.gmail.rotoyutoriapp.newSQL; 2import com.gmail.rotoyutoriapp.getLogger; 3import com.gmail.rotoyutoriapp.Exception; 4 5import java.sql.SQLException; 6 7public class SQLOrder { 8 public static boolean newPlayerData(String uuid) { 9 try { 10 getLogger.info("プレイヤーデータをSQLに登録します。"); 11 newSQL sql = new newSQL(); 12 String command = "INSERT INTO `players` (`UUID`, `playerName`) VALUES ('" + uuid + "', NULL');"; 13 //String command = "INSERT INTO `reports` (`id`, `time`, `username`, `uuid`, `naiyou`) VALUES (NULL, CURRENT_TIMESTAMP, '" + playername + "', '" + uuid + "', '" + naiyou + "');"; 14 int result = sql.statement.executeUpdate(command); 15 sql.close(); 16 return true; 17 } catch (SQLException e) { 18 Exception.SQLException("SQLOrder(Player)","newPlayerData"); 19 return false; 20 } 21 } 22}
NewPlayer
1import java.util.UUID; 2import com.gmail.rotoyutoriapp.*; 3 4public class NewPlayer { 5 public static void newPlayerID() { 6 getLogger.info("プレイヤーIDを生成します..."); 7 String uuid = UUID.randomUUID().toString(); 8 getLogger.info("プレイヤーID生成完了"); 9 getLogger.info("ID:" + uuid); 10 SQLOrder.newPlayerData(uuid); 11 } 12}
onEnableProcessing
1import com.gmail.rotoyutoriapp.Player.NewPlayer; 2 3import java.net.InetAddress; 4import java.net.UnknownHostException; 5 6public class onEnableProcessing { 7 static void onEnable() { 8 try { 9 InetAddress localAddress = InetAddress.getLocalHost(); //IPを取得 10 getLogger.info("現在のローカルIP:" + localAddress); //取得したIPを表示 11 Maintenance.onCheck(); //メンテナンス状態を確認 12 CreateFiles.serverProperty(); //サーバープロパティファイルの作成 13 CheckFiles.NotificationDirectoryCheck(); //お知らせディレクトリの存在を確認 14 getLogger.info("サーバーが起動しました。"); 15 ServerProperty.getProperty(); //サーバープロパティを取得 16 NewPlayer.newPlayerID(); 17 Main.onInputCommands(); //コマンド入力待機モード 18 } catch (UnknownHostException e) { 19 getLogger.info("IP取得エラー"); 20 getLogger.info("UnknownHostException"); 21 getLogger.info("クラス:onEnableProcessing"); 22 } 23 } 24}
pom
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.gmail.rotoyutoriapp</groupId> 8 <artifactId>GameServer</artifactId> 9 <version>1.0</version> 10 11 <build> 12 <plugins> 13 <plugin> 14 <groupId>org.apache.maven.plugins</groupId> 15 <artifactId>maven-jar-plugin</artifactId> 16 <version>3.0.2</version> 17 <configuration> 18 <archive> 19 <manifest> 20 <mainClass>com.gmail.rotoyutoriapp.Main</mainClass> 21 <addClasspath>true</addClasspath> 22 <addExtensions>false</addExtensions> 23 </manifest> 24 </archive> 25 </configuration> 26 </plugin> 27 </plugins> 28 </build> 29 30 31 <dependencies> 32 <dependency> 33 <groupId>mysql</groupId> 34 <artifactId>mysql-connector-java</artifactId> 35 <version>8.0.12</version> 36 </dependency> 37 </dependencies> 38 39 <properties> 40 <maven.compiler.source>1.8</maven.compiler.source> 41 <maven.compiler.target>1.8</maven.compiler.target> 42 </properties> 43 44</project>
Main
1 2import java.util.Scanner; 3 4public class Main { 5 6 public static void main(String[] args) { 7 getLogger.info("ゲームサーバー起動処理開始..."); 8 onEnableProcessing.onEnable(); 9 10 } 11 12 static void onInputCommands() { 13 System.out.print(">"); 14 Scanner scanner = new Scanner(System.in); 15 String input = scanner.nextLine(); 16 SaveLogs.saveLog(">" + input); 17 commands(input); 18 } 19 20 static void commands(String command) { 21 if (command.equalsIgnoreCase("stop")) { //サーバーの停止 22 commands.stop(); 23 } else if (command.equalsIgnoreCase("setMaintenance")) { //メンテナンスモードを設定 24 commands.SetMaintenance(); 25 } else if (command.equalsIgnoreCase("checkMaintenance")) { //メンテナンスモードを確認 26 commands.maintenanceCheck(); 27 } else if (command.contains("brc ")) { //運営からのお知らせコマンド(brc があれば) 28 String contains = command.replace("brc ", ""); //brc を空白に置き換え 29 SaveNotification.onSave(contains); 30 } else if (command.equalsIgnoreCase("help")) { 31 commands.help(); 32 } else { 33 getLogger.info("コマンドが間違っています。"); 34 getLogger.info("helpでヒントを表示します。"); 35 } 36 onInputCommands(); 37 } 38}
試したこと
・pom.xmlのdependencyのartifactIDをmysqlにしてみたりmysql-connector-javaにしてみたが変わらず
・ドライバのURLをcom.microsoft.sqlserver.jdbc.SQLServerDriverやorg.h2.Driverに変えてみた
・forNameをしなくても良いという情報があったため、forNameを削除した
・Class.forName("com.mysql.jdbc.Driver").newInstance();に変えてみたりしたが変わらず
補足情報(FW/ツールのバージョンなど)
開発ソフト - Intellij IDEA
Mavenプロジェクト
開発PC
OS: Windows10 home