KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > InitServlet


1 package example;
2
3 import java.io.PrintWriter JavaDoc;
4
5 import java.sql.Connection JavaDoc;
6 import java.sql.Statement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9
10 import javax.sql.DataSource JavaDoc;
11
12 import javax.naming.InitialContext JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.NamingException JavaDoc;
15
16 import javax.servlet.ServletException JavaDoc;
17
18 import javax.servlet.http.HttpServlet JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22
23 /**
24  * The InitServlet initializes the database.
25  *
26  */

27 public class InitServlet extends HttpServlet JavaDoc {
28   /**
29    * The DataSource for the table.
30    */

31   private DataSource JavaDoc _ds = null;
32
33   /**
34    * Sets the data source.
35    */

36   public void setDataSource(DataSource JavaDoc ds)
37   {
38     _ds = ds;
39   }
40
41   /**
42    * Initializes the reference to the CourseBean home interface.
43    */

44   public void init()
45     throws ServletException JavaDoc
46   {
47     try {
48       if (_ds == null)
49     assemble();
50
51       Connection JavaDoc conn = _ds.getConnection();
52
53       try {
54     Statement JavaDoc stmt = conn.createStatement();
55
56     try {
57       ResultSet JavaDoc rs = stmt.executeQuery("SELECT id FROM jdbc_basic_brooms");
58
59       if (rs.next()) {
60         rs.close();
61         stmt.close();
62         return; // already initialized
63
}
64     } catch (SQLException JavaDoc e) {
65     }
66
67     stmt.executeUpdate("CREATE TABLE jdbc_basic_brooms (" +
68                        " id INTEGER PRIMARY KEY auto_increment," +
69                        " name VARCHAR(128)," +
70                        " cost INTEGER" +
71                        ")");
72     stmt.executeUpdate("INSERT INTO jdbc_basic_brooms (name, cost) " +
73                "VALUES ('firebolt', 4000)");
74     stmt.executeUpdate("INSERT INTO jdbc_basic_brooms (name, cost) " +
75                "VALUES ('nimbus 2001', 500)");
76     stmt.executeUpdate("INSERT INTO jdbc_basic_brooms (name, cost) " +
77                "VALUES ('nimbus 2000', 300)");
78     stmt.executeUpdate("INSERT INTO jdbc_basic_brooms (name, cost) " +
79                "VALUES ('cleansweep 7', 150)");
80     stmt.executeUpdate("INSERT INTO jdbc_basic_brooms (name, cost) " +
81                "VALUES ('cleansweep 5', 100)");
82     stmt.executeUpdate("INSERT INTO jdbc_basic_brooms (name, cost) " +
83                "VALUES ('shooting star', 50)");
84
85     stmt.close();
86       } finally {
87     conn.close();
88       }
89     } catch (SQLException JavaDoc e) {
90       throw new ServletException JavaDoc(e);
91     }
92   }
93
94   /**
95    * Assembles the DataSource dependency when the container does
96    * not support servlet dependency injection.
97    */

98   private void assemble()
99     throws ServletException JavaDoc
100   {
101     try {
102       String JavaDoc dataSourceName = getInitParameter("data-source");
103
104       if (dataSourceName == null)
105     dataSourceName = "jdbc/basic";
106
107       Context JavaDoc ic = new InitialContext JavaDoc();
108     
109       _ds = (DataSource JavaDoc) ic.lookup("java:comp/env/" + dataSourceName);
110
111       if (_ds == null)
112     throw new ServletException JavaDoc(dataSourceName + " is an unknown data-source.");
113     } catch (NamingException JavaDoc e) {
114       throw new ServletException JavaDoc(e);
115     }
116   }
117
118   /**
119    *
120    */

121   public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
122     throws java.io.IOException JavaDoc, ServletException JavaDoc
123   {
124     throw new UnsupportedOperationException JavaDoc();
125   }
126 }
127
Popular Tags