新規作成
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user