init
This commit is contained in:
71
src/com/rhizome/example/dao/EmployeeDAO.java
Normal file
71
src/com/rhizome/example/dao/EmployeeDAO.java
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* EmployeeDAO.java
|
||||
* 作成 IT-College 2020
|
||||
*------------------------------------------------------------
|
||||
* Copyright(c) Rhizome Inc. All Rights Reserved.
|
||||
*/
|
||||
package com.rhizome.example.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
public class EmployeeDAO {
|
||||
private static final String TBL_NAME = "employee";
|
||||
private static final String ID_EMPLOYEE = "id_employee";
|
||||
private static final String NM_EMPLOYEE = "nm_employee";
|
||||
private static final String KN_EMPLOYEE = "kn_employee";
|
||||
private static final String MAIL_ADDRESS = "mail_address";
|
||||
private static final String PASSWORD = "password";
|
||||
private static final String ID_DEPARTMENT = "id_department";
|
||||
|
||||
/** DBコネクション */
|
||||
public Connection con;
|
||||
/** DBステートメント */
|
||||
PreparedStatement stmt;
|
||||
/** 検索結果 */
|
||||
ResultSet rs;
|
||||
|
||||
/**
|
||||
* インスタンス生成時DBコネクション
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
public EmployeeDAO(Connection con) {
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルの情報を全件取得する
|
||||
*
|
||||
* @return 検索結果
|
||||
* @throws SQLException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public List<String> selectAll() throws SQLException, ClassNotFoundException {
|
||||
List<String> rtnList = new ArrayList<>();
|
||||
StringBuilder sql = new StringBuilder();
|
||||
sql.append(" SELECT ");
|
||||
sql.append(" " + "*");
|
||||
sql.append(" FROM ");
|
||||
sql.append(" " + TBL_NAME);
|
||||
try {
|
||||
|
||||
this.stmt = con.prepareStatement(sql.toString());
|
||||
rs = stmt.executeQuery();
|
||||
while (rs.next()) {
|
||||
rtnList.add(rs.getString(NM_EMPLOYEE));
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeStatement(this.stmt);
|
||||
}
|
||||
return rtnList;
|
||||
}
|
||||
|
||||
}
|
||||
149
src/com/rhizome/example/main/Main.java
Normal file
149
src/com/rhizome/example/main/Main.java
Normal file
@@ -0,0 +1,149 @@
|
||||
package com.rhizome.example.main;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.rhizome.example.dao.EmployeeDAO;
|
||||
import com.rhizome.example.util.DbUtil;
|
||||
|
||||
/**
|
||||
* @author IT-College
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Main {
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
/**
|
||||
* テーブル切り替え用変数 0:Employeeテーブル 1:Departmentテーブル
|
||||
*/
|
||||
static int tblFlg = 0;
|
||||
|
||||
/**
|
||||
* 処理開始位置
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
System.out.println("システムを開始します");
|
||||
while (true) {
|
||||
try {
|
||||
new Main().start();
|
||||
} catch (Exception e) {
|
||||
System.out.println("予期せぬエラーが発生しました");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 処理選択を行う
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
public void start() throws ClassNotFoundException, SQLException {
|
||||
System.out.println();
|
||||
System.out.println("現在のメンテナンス対象テーブル:" + targetTableName(tblFlg));
|
||||
System.out.println("1:検索 2:登録 3:更新 4:削除 9:テーブル切替 0:終了");
|
||||
String ope = sc.nextLine();
|
||||
switch (ope) {
|
||||
case "1":
|
||||
empSelect();
|
||||
break;
|
||||
case "2":
|
||||
break;
|
||||
case "3":
|
||||
break;
|
||||
case "4":
|
||||
break;
|
||||
case "9":
|
||||
return;
|
||||
case "0":
|
||||
System.out.println("システムを終了します");
|
||||
System.exit(0);
|
||||
default:
|
||||
System.out.println("指定の番号を入力してください");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 社員テーブルから情報を検索する
|
||||
*
|
||||
* @throws ClassNotFoundException
|
||||
* @throws SQLException
|
||||
*/
|
||||
public static void empSelect() throws ClassNotFoundException, SQLException {
|
||||
List<String> empList = new ArrayList<>();
|
||||
Connection con = DbUtil.getConnection();
|
||||
EmployeeDAO empDao = new EmployeeDAO(con);
|
||||
System.out.println("1:全検索 2:条件検索 それ以外:戻る");
|
||||
String select = sc.nextLine();
|
||||
try {
|
||||
switch (select) {
|
||||
case "1":
|
||||
empList = empDao.selectAll();
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
for (String emp : empList) {
|
||||
System.out.println(emp);
|
||||
}
|
||||
} finally {
|
||||
DbUtil.closeConnection(empDao.con);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* フラグを判定し、テーブル名を取得する
|
||||
*
|
||||
* @param tblFlg
|
||||
* テーブル名判定用フラグ
|
||||
* @return テーブル名
|
||||
*/
|
||||
public static String targetTableName(int tblFlg) {
|
||||
String rtnTblName = "";
|
||||
if (tblFlg == 0) {
|
||||
rtnTblName = "社員テーブル";
|
||||
} else {
|
||||
rtnTblName = "部署テーブル";
|
||||
}
|
||||
return rtnTblName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 入力値を判定し、返却する
|
||||
*
|
||||
* @param titleName
|
||||
* 入力項目名
|
||||
* @param flg
|
||||
* 数値チェック可否フラグ
|
||||
* @return
|
||||
*/
|
||||
public static String inputCheck(String titleName, boolean flg) {
|
||||
String str = null;
|
||||
while (true) {
|
||||
System.out.println(titleName + "を入力してください");
|
||||
str = sc.nextLine();
|
||||
if (str.trim().length() == 0) {
|
||||
System.out.println(titleName + "は入力必須です。");
|
||||
continue;
|
||||
}
|
||||
if (flg) {
|
||||
if (str.trim().length() > 0) {
|
||||
try {
|
||||
Integer.parseInt(str);
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println(titleName + "は数値入力です");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
68
src/com/rhizome/example/util/DbUtil.java
Normal file
68
src/com/rhizome/example/util/DbUtil.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.rhizome.example.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
public class DbUtil {
|
||||
// 定数定義(共通)
|
||||
private static final String DB_HOST = "127.0.0.1";
|
||||
private static final String DB_NAME = "rezodb";
|
||||
private static final String DB_USER = "rezouser";
|
||||
private static final String DB_PASS = "Rezo_0000";
|
||||
|
||||
// 定数定義(PostgreSQL用)
|
||||
// private static final String DBMS = "postgresql";
|
||||
// private static final String DB_DRIVER = "org." + DBMS + ".Driver";
|
||||
// private static final String DB_PORT = "5432";
|
||||
// private static final String DB_URL = "jdbc:" + DBMS + "://" + DB_HOST +
|
||||
// ":" + DB_PORT + "/" + DB_NAME;
|
||||
|
||||
// 定数定義(MySQL用)
|
||||
private static final String DBMS = "mysql";
|
||||
private static final String DB_DRIVER = "com." + DBMS + ".cj.jdbc.Driver";
|
||||
private static final String DB_PORT = "3306";
|
||||
private static final String DB_URL = "jdbc:" + DBMS + "://" + DB_HOST + ":" + DB_PORT + "/" + DB_NAME;
|
||||
// + "?serverTimezone=JST"; //JDBCドライバのバージョン8.0.22以前を利用する場合は必要
|
||||
|
||||
// インスタンス化の禁止
|
||||
private DbUtil() {
|
||||
}
|
||||
|
||||
// Connectionを単一のインスタンスとする
|
||||
private static Connection con;
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
* 戻り値 :Connection
|
||||
* メソッド名 :getConnection
|
||||
* 引数 :なし
|
||||
* 例外 :ClassNotFoundException, SQLException
|
||||
* 処理 :JDBCドライバを読み込み後、DB接続する。
|
||||
* 生成されたConnectionを返却する
|
||||
* ※Connection生成には上記の定数を活用してください
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
* 戻り値 :なし
|
||||
* メソッド名 :closeStatement
|
||||
* 引数 :DBステートメント(PreparedStatement型)
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のDBステートメントがnullでない場合、そのDBステートメントをクローズする
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* TODO 以下の仕様でメソッドを作成してください。
|
||||
*
|
||||
* 戻り値 :なし
|
||||
* メソッド名 :closeConnection()
|
||||
* 引数 :切断するコネクション(Connection型)
|
||||
* 例外 :SQLException
|
||||
* 処理 :引数のコネクションがnullでない場合、そのコネクションを切断する
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user