KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > example > layering > persistence > ThingerPersistence


1 package net.innig.macker.example.layering.persistence;
2
3 import java.sql.*;
4 import java.util.*;
5
6 public class ThingerPersistence
7     {
8     public ThingerPersistence()
9         throws PersistenceException
10         {
11         Properties connProps = new Properties();
12         try
13             { connProps.load(getClass().getClassLoader().getResourceAsStream("db.properties")); }
14         catch(Exception JavaDoc e)
15             { throw new PersistenceException("Unable to load db.properties", e); }
16         
17         try
18             { Class.forName(connProps.getProperty("db.driver")); }
19         catch(ClassNotFoundException JavaDoc cnfe)
20             { throw new PersistenceException("Unable to load DB driver", cnfe); }
21         
22         try
23             {
24             conn = DriverManager.getConnection(
25                 connProps.getProperty("db.url"),
26                 connProps.getProperty("db.user"),
27                 connProps.getProperty("db.pass"));
28             conn.setAutoCommit(false);
29             }
30         catch(SQLException sqle)
31             { throw new PersistenceException("Unable to connect to database", sqle); }
32             
33         try
34             {
35             selStmt = conn.prepareStatement("select name from thinger");
36             insStmt = conn.prepareStatement("insert into thinger (name) values (?)");
37             delStmt = conn.prepareStatement("delete from thinger where name = ?");
38             }
39         catch(SQLException sqle)
40             { throw new PersistenceException("Unable to prepare statements", sqle); }
41         }
42     
43     public Set selectAll()
44         throws PersistenceException
45         {
46         Set thingers = new HashSet();
47         try
48             {
49             ResultSet rs = selStmt.executeQuery();
50             while(rs.next())
51                 thingers.add(rs.getString(1));
52             rs.close();
53             }
54         catch(SQLException sqle)
55             { throw new PersistenceException("Unable to select thingers", sqle); }
56         return thingers;
57         }
58     
59     public void insert(String JavaDoc name)
60         throws PersistenceException
61         {
62         boolean done = false;
63         try
64             {
65             insStmt.clearParameters();
66             insStmt.setString(1, name);
67             insStmt.execute();
68             conn.commit();
69             done = true;
70             }
71         catch(SQLException sqle)
72             { throw new PersistenceException("Unable to insert thinger", sqle); }
73         finally
74             { quietRollback(done); }
75         }
76     
77     public void delete(String JavaDoc name)
78         throws PersistenceException
79         {
80         boolean done = false;
81         try
82             {
83             if(delStmt.executeUpdate(name) == 0)
84                 throw new PersistenceException("No such thinger: \"" + name + '"');
85             conn.commit();
86             done = true;
87             }
88         catch(SQLException sqle)
89             { throw new PersistenceException("Unable to delete thinger", sqle); }
90         finally
91             { quietRollback(done); }
92         }
93     
94     private void quietRollback(boolean doit)
95         {
96         if(doit)
97             try { conn.rollback(); }
98             catch(Exception JavaDoc e) { }
99         }
100     
101     private Connection conn;
102     private PreparedStatement selStmt, insStmt, delStmt;
103     }
104
Popular Tags