---
# System prepended metadata

title: 0812 HW

---

# 0812 HW
[TOC]

## 在 Maven 專案中要使用 MariaDB 的步驟
:::danger
* **Prerequisites**
    * A MariaDB / MySQL server running on localhost using the default port 3306
    * Java version >= 8
    * Maven
:::

:::info
1. 創建 maven 專案( Create Maven Project )
    * 使用 Maven 創建一個簡單的 Java 專案：
```
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
```
:::
:::info
2. pom.xml 設定 MariaDB Java 驅動( Get MariaDB Java Driver )

```
<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>3.0.7</version>
</dependency>

```
:::
:::info
3. 設定 MariaDB 連線( Connection )
    * 基本的 maven 原型App.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/", "root", null)) {
            // create a Statement
            try (Statement stmt = conn.createStatement()) {
                //execute query
                try (ResultSet rs = stmt.executeQuery("SELECT 'Hello World!'")) {
                    //position result to first
                    rs.first();
                    System.out.println(rs.getString(1)); //result is "Hello World!"
                }
            }
        }
    }
}
```
:::
:::info
4. Compile project:
```
mvn install
```
:::

流程參考 [MariaDB 文件](https://mariadb.com/kb/en/java-connector-using-maven/)
POM.XML 設定 [MVN Repositoey](https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client/3.0.7)

# 請使用原生 SQL 語法回答以下問題
## 新增一個名為 training 的 DB
```sql=
CREATE DATABASE training;
```
## 新增一張名為 user 的 table，有 id、name、age 欄位
```sql=
CREATE TABLE user(
    
    id    INT             NOT NULL PRIMARY KEY AUTO_INCREMEN,
    name  VARCHAR(128)    NOT NULL,
    age   INT             NOT NULL

);
```
## 查詢全部 user
```sql=
SELECT * FROM user;
```
## 查詢 name 是 Bill，age 是 17 歲的 user
```sql=
SELECT name, age 
FROM User
WHERE name = "Bill" 
AND age = 17;
```
## 新增三個 user
### name 是 Brian，age 是 13 歲
### name 是 Brian，age 是 20 歲
### name 是 Brian，age 是 50 歲
```sql=
INSERT INTO (name, age) VALUES ('Brian', 13);
INSERT INTO (name, age) VALUES ('Brian', 20);
INSERT INTO (name, age) VALUES ('Brian', 50);
```
## 將 name 是 Brian，age 是 50 歲的 user 的 age 改完 60 歲
```sql=
UPDATE user 
SET age = 60 
WHERE name ='Brian' 
AND age = 50;
```
## 刪除 name 是 Brian，age 是 60 歲的 user
```sql=
DELETE FROM user 
WHERE name='Brain' 
AND age = 60;
```
## 計算出有幾個 name 是 Brian 的 user
```sql=
SELECT COUNT(*) FROM user
WHERE name = 'Brian' ;
```
# 將 input 元素轉成 output 元素後 return
* input : petList = [“2. dog”, “”, “3. cat”, " ", “1. rabbit”]
* output : petList = ["1.rabbits, “2.dogs”, “3.cats”]
* 使用一般方法
```java=
Class Animal{

public String sortArrayByPetList(String[] pet) {
    
    List<String> petList = new ArrayList();
    
        //移除空字串
        prtList.removeAll(Arrays.pet("", null));
        //排序第一個字母，字母為數字，所以自然可以比大小
        Collections.sort(petList)
    
        return petList.toArray(new String[petList.size()]);
    }
}

```
    
    
* 使用 Stream 方法
```java=
Class Animal{
   
    public String[] sortArrayByPetList(String[] pet) {
        
        petList = strings.stream().filter(string -> !string
        .isEmpty())// 獲取非空字串
        .sorted().collect(Collectors.petList())//排序
        
    retrun petList;
    }
}
```
