| 1 | package mypackage; |
|---|
| 2 | |
|---|
| 3 | import java.util.*; |
|---|
| 4 | import java.sql.*; |
|---|
| 5 | |
|---|
| 6 | public class Person { |
|---|
| 7 | private String firstName; |
|---|
| 8 | private String lastName; |
|---|
| 9 | private Calendar birthday=Calendar.getInstance(); |
|---|
| 10 | private String zip; |
|---|
| 11 | private String address; |
|---|
| 12 | |
|---|
| 13 | public Person() {} |
|---|
| 14 | |
|---|
| 15 | public Person(String fName,String lName) { |
|---|
| 16 | firstName=fName; |
|---|
| 17 | lastName=lName; |
|---|
| 18 | } |
|---|
| 19 | public void zipSearch() { |
|---|
| 20 | if (zip!=null) { |
|---|
| 21 | zip.Model model=new zip.Model(); |
|---|
| 22 | model.setZip(zip); |
|---|
| 23 | model.execute(); |
|---|
| 24 | |
|---|
| 25 | //検索結果の1番目を住所として設定 |
|---|
| 26 | String[] result=model.getResults().get(0); |
|---|
| 27 | this.setZip(result[0]); |
|---|
| 28 | this.setAddress(result[1].replaceAll("以下に掲載がない場合","")); |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | public void execute() { |
|---|
| 32 | Connection conn =null; |
|---|
| 33 | try { |
|---|
| 34 | javax.naming.Context ctx=new javax.naming.InitialContext(); |
|---|
| 35 | javax.sql.DataSource ds |
|---|
| 36 | =(javax.sql.DataSource)ctx.lookupLink("java:comp/env/jdbc/mydb"); |
|---|
| 37 | conn = ds.getConnection(); |
|---|
| 38 | |
|---|
| 39 | PreparedStatement stmt |
|---|
| 40 | =conn.prepareStatement("INSERT INTO people " |
|---|
| 41 | +"(firstName,lastName,birthday,zip,address) " |
|---|
| 42 | +"values (?,?,?,?,?)"); |
|---|
| 43 | stmt.setString(1, firstName); |
|---|
| 44 | stmt.setString(2, lastName); |
|---|
| 45 | stmt.setDate(3, new java.sql.Date(birthday.getTime().getTime())); |
|---|
| 46 | stmt.setString(4, zip); |
|---|
| 47 | stmt.setString(5, address); |
|---|
| 48 | |
|---|
| 49 | stmt.execute(); |
|---|
| 50 | } catch (Exception e) { |
|---|
| 51 | System.out.println("in Model.execute, "+e.getMessage()); |
|---|
| 52 | } finally { |
|---|
| 53 | if (conn!=null) { |
|---|
| 54 | try { |
|---|
| 55 | conn.close(); |
|---|
| 56 | } catch (SQLException e) {} |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | public int getAge() { |
|---|
| 61 | Calendar today=Calendar.getInstance(); |
|---|
| 62 | int year=today.get(Calendar.YEAR); |
|---|
| 63 | int month=today.get(Calendar.MONTH); |
|---|
| 64 | int date=today.get(Calendar.DAY_OF_MONTH); |
|---|
| 65 | |
|---|
| 66 | if (getMonth()<month) return year-getYear(); |
|---|
| 67 | if (getMonth()>month) return year-getYear()-1; |
|---|
| 68 | if (getDate()<=date) return year-getYear(); |
|---|
| 69 | return year-getYear()-1; |
|---|
| 70 | } |
|---|
| 71 | public void run() { |
|---|
| 72 | System.out.println("RUN!"); |
|---|
| 73 | } |
|---|
| 74 | public String getAddress() { |
|---|
| 75 | return address; |
|---|
| 76 | } |
|---|
| 77 | public void setAddress(String address) { |
|---|
| 78 | this.address = address; |
|---|
| 79 | } |
|---|
| 80 | public String getZip() { |
|---|
| 81 | return zip; |
|---|
| 82 | } |
|---|
| 83 | public void setZip(String zip) { |
|---|
| 84 | this.zip = zip; |
|---|
| 85 | } |
|---|
| 86 | public String getFirstName() { |
|---|
| 87 | return firstName; |
|---|
| 88 | } |
|---|
| 89 | public void setFirstName(String firstName) { |
|---|
| 90 | this.firstName = firstName; |
|---|
| 91 | } |
|---|
| 92 | public String getLastName() { |
|---|
| 93 | return lastName; |
|---|
| 94 | } |
|---|
| 95 | public void setLastName(String lastName) { |
|---|
| 96 | this.lastName = lastName; |
|---|
| 97 | } |
|---|
| 98 | public Calendar getBirthday() { |
|---|
| 99 | return birthday; |
|---|
| 100 | } |
|---|
| 101 | public void setBirthday(int year,int month,int date) { |
|---|
| 102 | birthday.set(year, month-1, date,0,0,0); //1月が0だから |
|---|
| 103 | } |
|---|
| 104 | public int getYear() { |
|---|
| 105 | return birthday.get(Calendar.YEAR); |
|---|
| 106 | } |
|---|
| 107 | public int getMonth() { |
|---|
| 108 | return birthday.get(Calendar.MONTH)+1; |
|---|
| 109 | } |
|---|
| 110 | public int getDate() { |
|---|
| 111 | return birthday.get(Calendar.DAY_OF_MONTH); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|