0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【メモ】eclipseからxamppのmariadbに接続する手順

Posted at

1.Why? java with MariaDB

前回、javaでmvcアプリを作成した。
この段階から、crudアプリを作成できるようになりたかった。
なぜjavaからmariadbに接続するかは下記の理由があった。

(1)PHPで簡単なcrudアプリで作るのに、xamppをインストールしていた。
(2)とりあえず今あるxamppのDBを使って、javaでcrudアプリを作れるか試してみたい

さらに__xamppのコンパネにMySQLと書いてあるのでmysql-connector-xxx.jarを追加していたが実行すると接続エラーが出て動かない。__

__「mariadb」のコネクタ__が必要だったことが後にわかった。

2.環境

xamppのバージョン:7.2.3
eclipseのバージョン:Version: 2019-12 (4.14.0)
mariadbのバージョン:10.1.31
java:11
OS:windows10 home 64bit

3.eclipseでの設定

1).DB接続の必要なプロジェクトを右クリックしてビルド・パスを選択

89985E47-FD1A-4FBA-92B2-F07CFE7D9453.jpeg

2).javaのビルド・パスで「ライブラリ」タブを選択し、クラスパスを
選択した状態で「外部JARの追加(x)」ボタンを押下。

65CE1C29-5623-4498-8E55-C2507691CE4E.jpeg

3).下記サイトでダウンロードしていたmariadb-java-client-xxx.jarを選択して
「開く」ボタンを押す

3E03ABCE-6BAB-41AF-A51E-ABAEDFBD5683.jpeg

[mariadbのjavaコネクタ(全バージョン)]
(https://downloads.mariadb.org/connector-java/+releases/)

4).ライブラリのクラスパスにjarファイルが反映されていることが確認して、
適用して閉じるボタンを押下する

6E7CBD24-007C-4437-B18B-2B13D3BCDD8E.jpeg

4.ソースでの実装例


 public Boolean updateData() {

//pokemondbはデータベース名
  String URL
  = "jdbc:mariadb://localhost:3306/pokemondb?useUnicode=true&characterEncoding=utf8mb4";
  String USER = "root";
  String PASS = "";

  try(Connection conn =
    DriverManager.getConnection(URL, USER, PASS)){

   conn.setAutoCommit(false);

  //DB処理
  
  } catch (Exception e) {
   e.printStackTrace();
   return false;
  }finally {
   System.out.println("処理が完了しました");
  }
 }

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?