1 2 9 10 import java.sql.*; 11 import java.applet.*; 12 import java.awt.*; 13 14 18 public class TestApplet extends Applet { 19 20 String driver_ = "org.objectweb.rmijdbc.Driver"; 21 String url_ = "localhost"; 22 String srvUrl_ = "jdbc:idb=sample.prp"; 23 String user_ = ""; 24 String passwd_ = ""; 25 String 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 srv = getCodeBase().getHost(); 49 if(srv == null || srv.length() < 1) { 50 try { 51 srv = java.net.InetAddress.getLocalHost().getHostName(); 52 } catch(Exception e) { 53 e.printStackTrace(); 54 srv = new String ("localhost"); 55 } 56 } 57 58 url_ = new String ("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 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 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 e) { 149 e.printStackTrace(); 150 } 151 return false; 152 } 153 154 void connectDB() throws Exception { 155 Class.forName(driver_).newInstance(); 156 connection_ = DriverManager.getConnection(url_, user_, passwd_); 157 } 158 159 void disconnectDB() throws Exception { 160 if(connection_ != null) connection_.close(); 161 connection_ = null; 162 } 163 164 void execSQL() throws Exception { 165 166 Statement st = connection_.createStatement(); 167 ResultSet rs = st.executeQuery(sql_); 168 169 sqlResults_.clear(); 170 ResultSetMetaData md = rs.getMetaData(); 171 StringBuffer result; 172 for(int row = 0; row < maxResults_ && rs.next(); row++) { 173 result = new StringBuffer (); 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 Class.forName("org.objectweb.rmijdbc.Driver").newInstance(); 192 193 build(); 194 validate(); 195 196 } catch(Exception e) { 197 e.printStackTrace(); 198 } 199 200 230 } 231 }; 232 233 | Popular Tags |