package JavaSQL_Sample;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class enshu6 {
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
//初期化ののじゃじゅに代入
judge.judgement(enshu.initial());
}
}
class enshu{
public static int initial() throws SQLException, ClassNotFoundException, IOException {
//db接続
//DB接続
System.out.println("処理区分(1:新規2:変更3:削除9:終了)");
System.out.println("顧客マスタのデータのプログラムです、数字を入力してください");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str;
str = in.readLine();
int dealID = Integer.parseInt(str);
System.out.println("処理したいの数字は:"+dealID);
if(dealID==9) {
System.out.println("プログラム終了");
System.exit(0);
}
return dealID;
}}
class judge {
static String DBSERVER = "jdbc:hsqldb:hsql://localhost";
static String USER = "sa";
static String PASSWD = "";
static void judgement(int dealID) throws IOException, ClassNotFoundException, SQLException {
int jikou=0;
while(true)
{
if((dealID!=1)&&(dealID!=2)&&(dealID!=3)&&(dealID!=9)||(jikou==1)) {
System.out.println("処理区分(1:新規2:変更3:削除9:終了)");
System.out.println("顧客マスタのデータのプログラムです、数字を入力してください");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = in.readLine();
dealID = Integer.parseInt(str);
jikou=0;
}
if(jikou==0) {
switch(dealID)
{
case 1: {reconfirm(dealID);
jikou=1;break;}
case 2:{reconfirm(dealID); jikou=1;break;}
case 3:{reconfirm(dealID); jikou=1;break;}
case 9: {System.out.println("プログラム終了");
System.exit(0);}
}
}
}
}
static void reconfirm( int number) throws IOException, ClassNotFoundException, SQLException {
boolean judge=true;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(judge==true) {
System.out.print("確認入力を行う:1を入力すると顧客マスタを読み込み新規を行う、9ならキャンセル,処理区分に戻る");
String str = in.readLine();
int kakunin=Integer.parseInt(str);
switch(kakunin)
{
case 1:
{ if (number==1)
shori.jikou1();
if (number==2)
{shori.jikou2();}
if(number==3) {
shori.jikou3();
}
}
case 9:
{System.out.println("修理区分に戻る");
judge=false;
break;}
default:
}
}
}
}
class shori {
static String DBSERVER = "jdbc:hsqldb:hsql://localhost";
static String USER = "sa";
static String PASSWD = "";
static void jikou1() throws SQLException, ClassNotFoundException, IOException {
Connection con =
DriverManager.getConnection(DBSERVER,USER,PASSWD);
Statement stmt = con.createStatement();
Class.forName("org.hsqldb.jdbcDriver");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str=null;
System.out.print("お客様idを入力してください:");
str = in.readLine();
int temp=Integer.parseInt(str);
String query = "SELECT * FROM 顧客マスタ WHERE 顧客ID = " +temp ;
ResultSet rs = stmt.executeQuery(query);
if(rs.next()){
System.out.println("エーラ、データ登録したです");
System.out.println("顧客名:");
System.out.println(rs.getString("顧客名"));
System.out.println("連絡先:");
System.out.println(rs.getString("連絡先"));
System.out.println(""); //改行
}
else{
System.out.println("エーラ:データ未登録");
System.out.println("id to username と連絡先を入力してください");
int ID=Integer.parseInt(in.readLine());
String userName = in.readLine();
String usertel = in.readLine();//int変換
stmt = con.createStatement();
query = "INSERT INTO 顧客マスタ(顧客ID,顧客名,連絡先) " +"VALUES ('"+ID+"','"+userName+"','"+usertel+"')";
int ret = stmt.executeUpdate(query);
System.out.println(ret + "行を挿入しました");
System.out.println("追加完了 "); //改行
}
stmt.close();
con.close();
}
static void jikou2() throws SQLException, ClassNotFoundException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = null;
str = in.readLine();
int kakunin=Integer.parseInt(str);
String query = null;
ResultSet rs = null;
Connection con =
DriverManager.getConnection(DBSERVER,USER,PASSWD);
Statement stmt = con.createStatement();
Class.forName("org.hsqldb.jdbcDriver");
while(true) {
System.out.println("顧客IDを入力してください:");
str = in.readLine();
kakunin=Integer.parseInt(str);
System.out.println("入力したの顧客IDは:"+kakunin);
query = "SELECT * FROM 顧客マスタ WHERE 顧客ID = "+kakunin ;
rs = stmt.executeQuery(query);
if(rs.next()){
System.out.println("顧客名:");
System.out.println(rs.getString("顧客名"));
System.out.println("連絡先:");
System.out.println(rs.getString("連絡先"));
System.out.println(""); //改行
System.out.print("改ざんしたいのお客様めと連絡先を入力してください:");
String userName = in.readLine();
String usertel = in.readLine();//int変換
query = "UPDATE 顧客マスタ SET 顧客名='"+userName+"'WHERE 顧客ID = '"+kakunin+"' " ;
rs = stmt.executeQuery(query);
query = "UPDATE 顧客マスタ SET 連絡先='"+usertel+"'WHERE 顧客ID = '"+kakunin+"' " ;
rs = stmt.executeQuery(query);
System.out.println("改ざんしたいのお客様めと連絡先を更新か完了した");}
else{
System.out.println("エーラ:データ未登録");
//登録していない、
break;}
}
stmt.close();
con.close();
}
static void jikou3() throws SQLException, ClassNotFoundException, IOException {
ResultSet rs = null;
Connection con =
DriverManager.getConnection(DBSERVER,USER,PASSWD);
Statement stmt = con.createStatement();
Class.forName("org.hsqldb.jdbcDriver");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str;
while(true) {
System.out.println("顧客IDを入力してください:");
str = in.readLine();
int kakunin=Integer.parseInt(str);
System.out.println("入力したの顧客IDは:"+kakunin);
String query = "SELECT * FROM 顧客マスタ WHERE 顧客ID = "+kakunin ;
rs = stmt.executeQuery(query);
if(rs.next()){
System.out.println("顧客名:");
System.out.println(rs.getString("顧客名"));
System.out.println("連絡先:");
System.out.println(rs.getString("連絡先"));
System.out.println(""); //改行
query = "DELETE FROM 顧客マスタ WHERE 顧客ID = '"+kakunin+"' " ;
rs = stmt.executeQuery(query);
System.out.println("消す完了");
break;
}
else{
System.out.println("エーラ:データ未登録");
//登録していない、
break;
}
}
stmt.close();
con.close();
}
}