0
0

More than 1 year has passed since last update.

Maven で MariaDB のデータを読む

Last updated at Posted at 2023-07-30

こちらのページを参考にしました。
Javaコネクター(Maven利用)

MariaDB の用意

MariaDB が動いていることを確認

$ sudo systemctl status mariadb
● mariadb.service - MariaDB 11.0.2 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; preset:>
     Active: active (running) since Sun 2023-07-30 11:20:45 JST; 19min ago

次でデータが読めることとします。
データベース city
ユーザー scott
パスワード tiger123
テーブル cities

$ mariadb -uscott -ptiger123 city
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 11.0.2-MariaDB Arch Linux

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [city]> select * from cities;
+-------+--------+------------+------------+
| id    | name   | population | date_mod   |
+-------+--------+------------+------------+
| t3321 | 岡山   |     691734 | 2001-02-15 |
| t3322 | 倉敷   |     923657 | 2001-08-01 |
| t3323 | 津山   |     279358 | 2001-07-29 |
| t3324 | 玉野   |     284615 | 2001-05-18 |
| t3325 | 笠岡   |     741256 | 2001-01-17 |
| t3326 | 井原   |     497521 | 2001-04-21 |
| t3327 | 総社   |     217348 | 2001-07-15 |
| t3328 | 高梁   |     152978 | 2001-09-11 |
| t3329 | 新見   |     823495 | 2001-10-02 |
+-------+--------+------------+------------+
9 rows in set (0.001 sec)

MariaDB [city]>

プロジェクトの作成

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=app01 \
	-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

pom.xml を次と差し替え

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>my-app</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Java のプログラム

src/main/java/com/mycompany/app/App.java
package com.mycompany.app;

import java.sql.*;

public class App {

    public static void main( String[] args ) throws SQLException {
        //create connection for a server installed in localhost, with a user "root" with no password
        try (Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost/city", "scott", "tiger123")) {
            // create a Statement
            try (Statement stmt = conn.createStatement()) {
                //execute query
                try (ResultSet rset = stmt.executeQuery("SELECT ID, NAME, POPULATION, DATE_MOD from cities order by ID")) {
                    //position result to first
while (rset.next())
		{
		String out_str = rset.getString ("id") + "\t";
		out_str += rset.getString("name") + "\t";
		out_str += rset.getString("population") + "\t";
		out_str += rset.getString("date_mod");

		System.out.println (out_str);
		}
                }
            }
        }
    }
}

コンパイル

mvn install

実行

mvn exec:java -Dexec.mainClass="com.mycompany.app.App"

実行結果

[INFO] Building my-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ my-app ---
t3321	岡山	691734	2001-02-15
t3322	倉敷	923657	2001-08-01
t3323	津山	279358	2001-07-29
t3324	玉野	284615	2001-05-18
t3325	笠岡	741256	2001-01-17
t3326	井原	497521	2001-04-21
t3327	総社	217348	2001-07-15
t3328	高梁	152978	2001-09-11
t3329	新見	823495	2001-10-02
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.410 s
[INFO] Finished at: 2023-07-30T11:48:39+09:00
[INFO] ------------------------------------------------------------------------
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