前提・実現したいこと
閲覧いただき、ありがとうございます。
Eclipse,Springを使ってJAVAの勉強をしている(超)初心者です。
元々、受講していた通信教育の課題がDBを使用しておらず、
内部にテーブルを保持している構造だったので、それを
MySQL,JDBCで外部化しようとしています。
一覧表示、更新、参照機能だけのシンプルな画面です。
参照の際に入力したidをキーにしてMySQLにアクセスしにいこうと
しているのですが、「SQL構文エラー」で落ちてしまいます。
パラメタをうまくSQLに渡すためにはどのようにすべきなのでしょうか。
どなたかご教授いただけないでしょうか。
発生している問題・エラーメッセージ
HTTPステータス 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from unit02.addressbook where id = ?]; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1
該当のソースコード
java
1(AddressBookController.java) 2package jp.practice.address; 3 4import java.util.List; 5import java.util.Map; 6 7import org.springframework.stereotype.Controller; 8import org.springframework.ui.Model; 9import org.springframework.web.bind.annotation.RequestMapping; 10 11@Controller 12@RequestMapping(value = "/book") 13public class AddressBookController { 14 private static final String INIT = "init"; 15 private static final String REFER = "refer"; 16 private static final String UPDATE = "update"; 17 private static final String LIST = "list"; 18 private static final String INIMSG = "従業員番号を入力してください"; 19 private static final String REFMSG = "情報が取得できました"; 20 private static final String UPDMSG = "情報を更新してください"; 21 private static final String ERRMSG = "該当するデータはありません"; 22 23 private RecordManager recordManager; 24 public AddressBookController(RecordManager recordManager) { 25 this.recordManager = recordManager; 26 } 27 28 @RequestMapping(value = "/start") 29 public String init(AddressBookForm form, Model model) { 30 model.addAttribute("message", INIMSG); 31 return INIT; 32 } 33 34 @RequestMapping(params = "reference") 35 public String refer(AddressBookForm form, Model model) { 36 List<Map<String, Object>> list1 = recordManager.selectEmployee(form.getId()); 37 if (list1 != null) { 38 form.setName(list1.getName()); 39 form.setPhone(list1.getPhone()); 40 form.setAddress(list1.getAddress()); 41 model.addAttribute("message", REFMSG); 42 return REFER; 43 } else { 44 model.addAttribute("message", ERRMSG); 45 return INIT; 46 } 47 } 48 49 ----------割愛-------------- 50 51 @RequestMapping(params = "toInit") 52 public String toInit(AddressBookForm form, Model model) { 53 model.addAttribute("message", INIMSG); 54 return INIT; 55 } 56}
java
1(AddressBookForm.java)) 2package jp.practice.address; 3 4public class AddressBookForm { 5 6 private String id; 7 private String name; 8 private String phone; 9 private String address; 10 11 public AddressBookForm() { 12 } 13 /** 14 * @return id 15 */ 16 public String getId() { 17 return id; 18 } 19 /** 20 * @param id セットする id 21 */ 22 public void setId(String id) { 23 this.id = id; 24 } 25 /** 26 * @return name 27 */ 28 public String getName() { 29 return name; 30 } 31 /** 32 * @param name セットする name 33 */ 34 public void setName(String name) { 35 this.name = name; 36 } 37 /** 38 * @return phone 39 */ 40 public String getPhone() { 41 return phone; 42 } 43 /** 44 * @param phone セットする phone 45 */ 46 public void setPhone(String phone) { 47 this.phone = phone; 48 } 49 /** 50 * @return address 51 */ 52 public String getAddress() { 53 return address; 54 } 55 /** 56 * @param address セットする address 57 */ 58 public void setAddress(String address) { 59 this.address = address; 60 } 61}
java
1(Employee.java) 2package jp.practice.address; 3 4public class Employee { 5 6 private String id; 7 private String name; 8 private String phone; 9 private String address; 10 11 /** 12 * @param id 13 * @param name 14 * @param phone 15 * @param address 16 */ 17 public Employee(String id, String name, String phone, String address) { 18 this.id = id; 19 this.name = name; 20 this.phone = phone; 21 this.address = address; 22 } 23 /** 24 * @return id 25 */ 26 public String getId() { 27 return id; 28 } 29 /** 30 * @param id セットする id 31 */ 32 public void setId(String id) { 33 this.id = id; 34 } 35 /** 36 * @return name 37 */ 38 public String getName() { 39 return name; 40 } 41 /** 42 * @param name セットする name 43 */ 44 public void setName(String name) { 45 this.name = name; 46 } 47 /** 48 * @return phone 49 */ 50 public String getPhone() { 51 return phone; 52 } 53 /** 54 * @param phone セットする phone 55 */ 56 public void setPhone(String phone) { 57 this.phone = phone; 58 } 59 /** 60 * @return address 61 */ 62 public String getAddress() { 63 return address; 64 } 65 /** 66 * @param address セットする address 67 */ 68 public void setAddress(String address) { 69 this.address = address; 70 } 71 /* (非 Javadoc) 72 * @see java.lang.Object#hashCode() 73 */ 74 @Override 75 public int hashCode() { 76 final int prime = 31; 77 int result = 1; 78 result = prime * result + ((id == null) ? 0 : id.hashCode()); 79 return result; 80 } 81 /* (非 Javadoc) 82 * @see java.lang.Object#equals(java.lang.Object) 83 */ 84 @Override 85 public boolean equals(Object obj) { 86 if (this == obj) { 87 return true; 88 } 89 if (obj == null) { 90 return false; 91 } 92 if (!(obj instanceof Employee)) { 93 return false; 94 } 95 Employee other = (Employee) obj; 96 if (id == null) { 97 if (other.id != null) { 98 return false; 99 } 100 } else if (!id.equals(other.id)) { 101 return false; 102 } 103 return true; 104 } 105}
java
1(RecordManager.java) 2package jp.practice.address; 3 4import java.util.ArrayList; 5import java.util.List; 6import java.util.Map; 7 8import org.springframework.jdbc.core.JdbcTemplate; 9import org.springframework.stereotype.Component; 10@Component 11public class RecordManager { 12 private JdbcTemplate jdbcTemplate; 13 14// /** 初期値データ */ 15 private static List<Employee> list = new ArrayList<>(); 16 static { 17 } 18 public RecordManager(JdbcTemplate jdbcTemplate) { 19 this.jdbcTemplate = jdbcTemplate; 20 } 21 /** 22 * 指定したID のレコードを取得 23 * 24 * @param id 25 * 従業員番号 26 * @return Employee 27 */ 28 29/**start ****/ 30// public static Employee selectEmployee(String id) { 31 public List<Map<String, Object>> selectEmployee(String id) { 32 List<Map<String, Object>> list1 = jdbcTemplate.queryForList("select * from unit02.addressbook where id = ?"); 33 return list1; 34// int index = list.indexOf(new Employee(id, "", "", "")); 35// if (index < 0) { 36// return null; 37// } 38// return list.get(index); 39/**e n d*/ 40 } 41 42 /** 43 * 指定した従業員情報を反映 44 * 45 * @param emp 46 * 従業員情報 47 */ 48 public static void updateEmployee(Employee emp) { 49 int index = list.indexOf(emp); 50 list.set(index, emp); 51 } 52 public List<Map<String, Object>> getEmployeeList() { 53 List<Map<String, Object>> list1 = jdbcTemplate.queryForList("select * from unit02.addressbook order by id"); 54 return list1; 55 } 56}
試したこと
①「?」を直書きで「'200101'」などの実際に存在しているものにしたら
正常にデータが取得できました。
②「?」を「id?」「?id」などにしてみましたがダメでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/31 02:10 編集
2019/05/31 03:42
2019/05/31 04:18
2019/05/31 04:20
2019/05/31 04:33