KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestApplet


1
2 /**
3  * RmiJdbc client/server JDBC Driver
4  * (C) GIE Dyade (Groupe BULL / INRIA Research Center) 1997
5  *
6  * @version 1.0
7  * @author Pierre-Yves Gibello (Pierre-Yves.Gibello@inrialpes.fr)
8  */

9
10 import java.sql.*;
11 import java.applet.*;
12 import java.awt.*;
13
14 /**
15  * This is a sample program for RmiJdbc client/server jdbc Driver
16  * RmiJdbc relies on Java RMI for jdbc objects distribution
17  */

18 public class TestApplet extends Applet {
19
20   String JavaDoc driver_ = "org.objectweb.rmijdbc.Driver";
21   String JavaDoc url_ = "localhost";
22   String JavaDoc srvUrl_ = "jdbc:idb=sample.prp";
23   String JavaDoc user_ = "";
24   String JavaDoc passwd_ = "";
25   String JavaDoc sql_ = "select * from import1";
26   int maxResults_ = 16;
27
28   Panel connectPanel_;
29   TextField driverText_;
30   TextField urlText_;
31   TextField userText_;
32   TextField passwdText_;
33   Button connectButton_;
34   Button disconnectButton_;
35
36   Panel sqlPanel_;
37   TextArea sqlArea_;
38   TextField maxText_;
39   Button sqlButton_;
40
41   Panel resultPanel_;
42   List sqlResults_;
43
44   Connection connection_ = null;
45
46   void build() {
47
48     String JavaDoc srv = getCodeBase().getHost();
49     if(srv == null || srv.length() < 1) {
50       try {
51         srv = java.net.InetAddress.getLocalHost().getHostName();
52       } catch(Exception JavaDoc e) {
53         e.printStackTrace();
54         srv = new String JavaDoc("localhost");
55       }
56     }
57
58     url_ = new String JavaDoc("jdbc:rmi://" + srv + "/" + srvUrl_);
59
60     setLayout(new BorderLayout());
61
62     connectPanel_ = new Panel();
63     connectPanel_.setLayout(new GridLayout(5,2));
64
65     connectPanel_.add(new Label("Driver"));
66     driverText_ = new TextField(driver_);
67     connectPanel_.add(driverText_);
68
69     connectPanel_.add(new Label("URL"));
70     urlText_ = new TextField(url_);
71     connectPanel_.add(urlText_);
72
73     connectPanel_.add(new Label("User"));
74     userText_ = new TextField(user_);
75     connectPanel_.add(userText_);
76
77     connectPanel_.add(new Label("Password"));
78     passwdText_ = new TextField(passwd_);
79     connectPanel_.add(passwdText_);
80
81     connectButton_ = new Button("Connect");
82     connectPanel_.add(connectButton_);
83
84     disconnectButton_ = new Button("Disconnect");
85     disconnectButton_.disable();
86     connectPanel_.add(disconnectButton_);
87
88     add(connectPanel_, "North");
89
90     // Query Panel
91
sqlPanel_ = new Panel();
92     sqlPanel_.setLayout(new BorderLayout());
93     
94     Panel reqPanel = new Panel();
95     reqPanel.setLayout(new GridLayout(1,2));
96
97     reqPanel.add(new Label("SQL"));
98     sqlArea_ = new TextArea(sql_, 4, 20);
99     reqPanel.add(sqlArea_);
100
101     sqlPanel_.add(reqPanel, "North");
102
103     Panel execPanel = new Panel();
104     execPanel.setLayout(new GridLayout(1,3));
105     execPanel.add(new Label("Max Results"));
106     maxText_ = new TextField(Integer.toString(maxResults_), 3);
107     execPanel.add(maxText_);
108
109     sqlButton_ = new Button("Execute");
110     sqlButton_.disable();
111     execPanel.add(sqlButton_);
112
113     sqlPanel_.add(execPanel, "Center");
114
115     add(sqlPanel_, "Center");
116
117     resultPanel_ = new Panel();
118     resultPanel_.setLayout(new BorderLayout());
119     sqlResults_ = new List(16);
120     resultPanel_.add(sqlResults_, "Center");
121
122     add(resultPanel_, "South");
123   }
124
125   public boolean action(Event evt, Object JavaDoc o) {
126     try {
127       if(evt.target == connectButton_) {
128         driver_ = driverText_.getText();
129         url_ = urlText_.getText();
130         user_ = userText_.getText();
131         passwd_ = passwdText_.getText();
132         connectDB();
133         sqlButton_.enable();
134         connectButton_.disable();
135         disconnectButton_.enable();
136       } if(evt.target == disconnectButton_) {
137         disconnectDB();
138         sqlResults_.clear();
139         sqlButton_.disable();
140         disconnectButton_.disable();
141         connectButton_.enable();
142
143       } else if(evt.target == sqlButton_) {
144         sql_ = sqlArea_.getText();
145         maxResults_ = Integer.parseInt(maxText_.getText());
146         execSQL();
147       }
148     } catch(Exception JavaDoc e) {
149       e.printStackTrace();
150     }
151     return false;
152   }
153
154   void connectDB() throws Exception JavaDoc {
155     Class.forName(driver_).newInstance();
156     connection_ = DriverManager.getConnection(url_, user_, passwd_);
157   }
158
159   void disconnectDB() throws Exception JavaDoc {
160     if(connection_ != null) connection_.close();
161     connection_ = null;
162   }
163
164   void execSQL() throws Exception JavaDoc {
165     
166     Statement st = connection_.createStatement();
167     ResultSet rs = st.executeQuery(sql_);
168
169     sqlResults_.clear();
170     ResultSetMetaData md = rs.getMetaData();
171     StringBuffer JavaDoc result;
172     for(int row = 0; row < maxResults_ && rs.next(); row++) {
173       result = new StringBuffer JavaDoc();
174       for(int col=1; col<= md.getColumnCount(); col++) {
175         result.append(rs.getString(col) + ", ");
176       }
177       sqlResults_.add(result.toString());
178     }
179
180     rs.close();
181     st.close();
182   }
183
184   public void init() {
185
186     try {
187
188       // Register RmiJdbc Driver in jdbc DriverManager
189
// The call to newInstance() is necessary on some platforms
190
// (with some java VM implementations)
191
Class.forName("org.objectweb.rmijdbc.Driver").newInstance();
192
193       build();
194       validate();
195
196     } catch(Exception JavaDoc e) {
197       e.printStackTrace();
198     }
199
200 /*******************
201
202
203       // Test with InstantDB free java database engine
204       // See http://www.instantdb.co.uk for info & download
205       String url = "jdbc:idb=sample.prp";
206
207       // RMI host will point to applet server
208       String rmiHost = new String("//" + getCodeBase().getHost());
209
210       // RmiJdbc URL is of the form:
211       // jdbc:rmi://<rmiHostName[:port]>/<jdbc-url>
212
213       Connection c = DriverManager.getConnection("jdbc:rmi:"
214        + rmiHost + "/" + url);
215
216       Statement st = c.createStatement();
217       ResultSet rs = st.executeQuery("select * from import1");
218
219       ResultSetMetaData md = rs.getMetaData();
220       while(rs.next()) {
221         System.out.print("\nTUPLE: | ");
222         for(int i=1; i<= md.getColumnCount(); i++) {
223           System.out.print(rs.getString(i) + " | ");
224         }
225       }
226
227       rs.close();
228
229 ****************/

230   }
231 };
232
233
Popular Tags