課題でJavaのプログラミングがでて困っております。
アクセスクラスとコントロールクラスの二つではデータのselectなどうまくいったのですが今課題のgeter seterを利用しなきゃいけないところで生きず待ってしまっております。
下記に私が挫折したコードがあります。
Javaに詳しい方助けてください。よろしくお願いします。
Java
1//アクセスクラス 2import java.sql.*; 3import java.util.ArrayList; 4import java.util.List; 5 6public class DataBase { 7 8 private Connection con = createConnection(); 9 10 private String Host = "jdbc:mysql://localhost:3306/shoseki"; 11 private String User = "root"; 12 private String Pass = ""; 13 14 private Connection createConnection() { 15 Connection con = null; 16 try { 17 Class.forName("com.mysql.jdbc.Driver"); 18 con = DriverManager.getConnection(Host,User,Pass); 19 }catch(ClassNotFoundException e) { 20 e.printStackTrace(); 21 }catch(SQLException e) { 22 e.printStackTrace(); 23 } 24 return con; 25 } 26 27 private void close(Connection con){ 28 try { 29 con.close(); 30 }catch(SQLException e) { 31 e.printStackTrace(); 32 } 33 } 34 35 public void Serch(String key, String value) { 36 String sql = "Select * From shoseki"; 37 ResultSet rs = null; 38 if(key != "null") { 39 sql = sql.concat(" where " + key + " LIKE " + "'%" + value + "%'"); 40 } 41 rs = SqlExec(sql); 42 SetResult(rs); 43 } 44 45 private ResultSet SqlExec(String sql) { 46 Statement stmt = null; 47 ResultSet rs = null; 48 try { 49 stmt = con.createStatement(); 50 rs = stmt.executeQuery(sql); 51 }catch(SQLException e) { 52 e.printStackTrace(); 53 } 54 return rs; 55 } 56 57 private void SetResult(ResultSet rs) { 58 List List = new ArrayList(); 59 while(rs.next()) { 60 Shoseki shoseki = new Shoseki(); 61 shoseki.setIsbn(rs.getString("isbn")); 62 shoseki.setTitle(rs.getString("title")); 63 shoseki.setAuthor(rs.getString("author")); 64 List.add(shoseki); 65 } 66 } 67}
Java
1// コントロールクラス 2import java.sql.*; 3import java.util.ArrayList; 4import java.util.List; 5 6class ShosekiManagementSystem { 7 8 private Shoseki shoseki; 9 public static void main(String[] args){ 10 11 String inputString; 12 int n; 13 14 ShosekiManagementSystem sms = new ShosekiManagementSystem(); 15 16 while(true) { 17 // Menu Display 18 System.out.println("********************"); 19 System.out.println("**書籍データベース**"); 20 System.out.println("****操作メニュー****"); 21 System.out.println("********************"); 22 System.out.println("1.登録"); 23 System.out.println("2.検索"); 24 System.out.println("3.更新"); 25 System.out.println("4.削除"); 26 System.out.println("9.終了"); 27 System.out.println(""); 28 System.out.println("> "); 29 30 inputString = new java.util.Scanner(System.in).nextLine(); 31 n = Integer.parseInt(inputString); 32 // End Determination 33 if(n == 9 ) break; 34 35 // Menu Number Check 36 switch(n) { 37 case 1: 38 sms.Insert(); 39 break; 40 case 2: 41 sms.Serch(); 42 break; 43 case 3: 44 sms.Update(); 45 break; 46 case 4: 47 sms.Delete(); 48 break; 49 default: 50 } 51 } 52 } 53 54 // Create 55 private void Insert() { 56 57 } 58 59 // Update 60 private void Update() { 61 62 } 63 64 // Delete 65 private void Delete() { 66 67 } 68 69 // Serch 70 private void Serch() { 71 DataBase DB = new DataBase(); 72 ResultSet rs; 73 String inputString; 74 int n; 75 76 while(true) { 77 // Menu Display 78 System.out.println("1.全件検索"); 79 System.out.println("2.ISBNから検索"); 80 System.out.println("3.書籍名から検索"); 81 System.out.println("4.著者から検索"); 82 System.out.println("9.戻る"); 83 System.out.println(""); 84 System.out.println("> "); 85 86 inputString = new java.util.Scanner(System.in).nextLine(); 87 n = Integer.parseInt(inputString); 88 89 // End Determination 90 if(n == 9) break; 91 92 // Menu Number Check 93 switch(n) { 94 case 1: 95 DB.Serch("null","null"); 96 ShowList(); 97 break; 98 case 2: 99 System.out.println(""); 100 System.out.println("ISBN番号による検索"); 101 System.out.println(""); 102 System.out.println("ISBN番号 ↓ "); 103 inputString = new java.util.Scanner(System.in).nextLine(); 104 DB.Serch("isbn",inputString); 105 ShowList(); 106 break; 107 case 3: 108 System.out.println(""); 109 System.out.println("タイトルによる検索"); 110 System.out.println(""); 111 System.out.println("タイトル ↓ "); 112 inputString = new java.util.Scanner(System.in).nextLine(); 113 DB.Serch("title",inputString); 114 ShowList(); 115 break; 116 case 4: 117 System.out.println(""); 118 System.out.println("著者による検索"); 119 System.out.println(""); 120 System.out.println("著者 ↓ "); 121 inputString = new java.util.Scanner(System.in).nextLine(); 122 DB.Serch("author",inputString); 123 ShowList(); 124 break; 125 default: 126 } 127 } 128 } 129 130 private void ShowList(){ 131 Shoseki List; 132 for(inr i = 0; i < List.size(); i++) { 133 List = (Shoseki) shoseki.get(i); 134 System.out.println("ISBN :\t" + List.getIsbn); 135 System.out.println("タイトル :\t" + List.getTitle); 136 System.out.println("著者 :\t" + List.getAuthor); 137 } 138 } 139}
Java
1// ゲッター 2public class Shoseki { 3 4 private String isbn; 5 private String title; 6 private String author; 7 8 public String getIsbn() { 9 return this.isbn; 10 } 11 12 public void setIsbn(String isbn) { 13 this.isbn = isbn; 14 } 15 16 public String getTitle() { 17 return this.title; 18 } 19 20 public void setTitle(String title) { 21 this.title = title; 22 } 23 24 public String getAuthor() { 25 return this.author; 26 } 27 28 public void setAuthor(String author) { 29 this.author = author; 30 } 31}
回答2件
あなたの回答
tips
プレビュー