|
Revision 26, 1.3 KB
(checked in by yabuki, 3 years ago)
|
|
|
-
Property svn:mime-type set to
text/plain
|
| Line | |
|---|
| 1 | package zip; |
|---|
| 2 | |
|---|
| 3 | import java.util.*; |
|---|
| 4 | import java.sql.*; |
|---|
| 5 | |
|---|
| 6 | public class Model { |
|---|
| 7 | private String zip; |
|---|
| 8 | private List<String[]> results; |
|---|
| 9 | |
|---|
| 10 | public List<String[]> getResults() { |
|---|
| 11 | return results; |
|---|
| 12 | } |
|---|
| 13 | public void setZip(String zip) { |
|---|
| 14 | this.zip = zip.replace("-",""); |
|---|
| 15 | } |
|---|
| 16 | public void execute() { |
|---|
| 17 | Connection conn=null; |
|---|
| 18 | try { |
|---|
| 19 | javax.naming.Context ctx=new javax.naming.InitialContext(); |
|---|
| 20 | javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookupLink("java:comp/env/jdbc/mydb"); |
|---|
| 21 | conn= ds.getConnection(); |
|---|
| 22 | |
|---|
| 23 | PreparedStatement stmt=conn.prepareStatement("SELECT * FROM zips WHERE zip LIKE ?"); |
|---|
| 24 | stmt.setString(1,zip+"%"); |
|---|
| 25 | stmt.setMaxRows(100); |
|---|
| 26 | ResultSet rs=stmt.executeQuery(); |
|---|
| 27 | |
|---|
| 28 | results=new LinkedList<String[]>(); |
|---|
| 29 | while (rs.next()) { |
|---|
| 30 | String result[]={rs.getString("zip"), |
|---|
| 31 | rs.getString("addr1") |
|---|
| 32 | +rs.getString("addr2") |
|---|
| 33 | +rs.getString("addr3") |
|---|
| 34 | +rs.getString("addr4") |
|---|
| 35 | +rs.getString("establishment")}; |
|---|
| 36 | results.add(result); |
|---|
| 37 | } |
|---|
| 38 | } catch (Exception e) { |
|---|
| 39 | System.out.println("in Model.execute, "+e.getMessage()); |
|---|
| 40 | } finally { |
|---|
| 41 | if (conn!=null) { |
|---|
| 42 | try { |
|---|
| 43 | conn.close(); |
|---|
| 44 | } catch (SQLException e) {} |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | } |
|---|