📄 21.txt
字号:
80 l_cnt = 0;
81 m_textarea2.append("\n");
82 }
83 }
84 }
85 l_fis.close(); //close the fileinputstream
86 return new String(l_strbuf); // Return the file text
87 } catch (IOException ex) {
88 return null;
89 }
90 }
91
92 /**
93 * Pad the input integer with zeros (for 3chars) and print
94 * in GUI along with the demarcator
95 **/
96 public void padAndAppend(int p_str,String p_demarcator) {
97 String l_str = new Integer(p_str).toString();
98 if (l_str.length() == 1)
99 m_textarea1.append("00"+l_str+p_demarcator);
100 else if (l_str.length() == 2)
101 m_textarea1.append("0"+l_str+p_demarcator);
102 else
103 m_textarea1.append(l_str+p_demarcator);
104 }
105
106 /**
107 * Sets up the listeners that respond to user actions,
108 * including button press
109 **/
110
111 public void setupListeners(){
112 m_ascii.addActionListener(
113 new java.awt.event.ActionListener(){
114 public void actionPerformed(ActionEvent e){
115 try{
116 m_label1.setForeground(Color.black);
117 m_label1.setText("ASCII Data Stream");
118 putStatus("Reading Ascii data stream...");
119 Thread.sleep(1000);
120 m_textarea1.setText("");
121 m_parent.dispatchEvent("ASCII");
122 }catch(InterruptedException ex){
123 putStatus(ex.toString());
124 }
125 }
126 });
127
128 m_unicode.addActionListener(
129 new java.awt.event.ActionListener() {
130 public void actionPerformed(ActionEvent e) {
131 try{
132 m_label1.setForeground(Color.black);
133 m_label1.setText("UNICODE Data Stream");
134 putStatus("Reading Unicode data stream...");
135 Thread.sleep(1000);
136 m_textarea1.setText("");
137 m_parent.dispatchEvent("UNICODE");
138 }catch(InterruptedException ex){
139 putStatus(ex.toString());
140 }
141 }
142 });
143 m_binary.addActionListener(
144 new java.awt.event.ActionListener() {
145 public void actionPerformed(ActionEvent e) {
146 try{
147 m_label1.setForeground(Color.black);
148 m_label1.setText("BINARY Data Stream");
149 putStatus("Reading Binary data stream...");
150 Thread.sleep(1000);
151 m_textarea1.setText("");
152 m_parent.dispatchEvent("BINARY");
153 }catch(InterruptedException ex){
154 //Trap Interrupted error of thread
155 putStatus(ex.toString());
156 }
157 }
158 });
159
160 m_exit.addActionListener(
161 new java.awt.event.ActionListener() {
162 public void actionPerformed(ActionEvent e){
163 if(e.getActionCommand() =="EXIT"){
164 m_parent.dispatchEvent("EXIT");
165 }
166 }
167 });
168 }
169
170 /**
171 * Status field message display handlers
172 **/
173
174 public void appendStatus(String p_status){
175 String l_status=m_statusbar.getText();
176 m_statusbar.setText(l_status + ":" + p_status);
177 }
178 public void putStatus(String p_message){
179 m_statusbar.setText(p_message);
180 }
181 public void clearStatus(){
182 m_statusbar.setText(" ");
183 }
184
185 /**
186 * Initialize all the GUI components and the layout
187 **/
188
189 public void jbInit() throws Exception {
190 m_gridBagLayout1 = new GridBagLayout();
191 m_gridBagLayout2 = new GridBagLayout();
192 m_gridBagLayout3 = new GridBagLayout();
193 m_panel1 = new JPanel();
194 m_panel2 = new JPanel();
195 m_panel3 = new JPanel();
196 m_radioPanel = new JPanel();
197 m_label1 = new JLabel();
198 m_label2 = new JLabel();
199 m_label3 = new JLabel();
200 m_radio=new ImageIcon("radio.gif");
201 m_radioPressed=new ImageIcon("radioPressed.gif");
202 m_radioSelected=new ImageIcon("radioSelected.gif");
203 m_ascii=new JRadioButton("Ascii",m_radio);
204 m_ascii.setPressedIcon(m_radioPressed);
205 m_ascii.setRolloverIcon(m_radioPressed);
206 m_ascii.setSelectedIcon(m_radioSelected);
207 m_textarea1 = new TextArea();
208 m_textarea2 = new TextArea();
209 m_statusbar.setFont(new Font("Monospaced", 0, 16));
210 m_group.add(m_ascii);
211 m_binary=new JRadioButton("Binary",m_radio);
212 m_binary.setPressedIcon(m_radioPressed);
213 m_binary.setRolloverIcon(m_radioPressed);
214 m_binary.setSelectedIcon(m_radioSelected);
215 m_group.add(m_binary);
216 m_unicode=new JRadioButton("Unicode",m_radio);
217 m_unicode.setPressedIcon(m_radioPressed);
218 m_unicode.setRolloverIcon(m_radioPressed);
219 m_unicode.setSelectedIcon(m_radioSelected);
220 m_group.add(m_unicode);
221 m_exit=new JRadioButton("Exit",m_radio);
222 m_exit.setRolloverIcon(m_radioPressed);
223 m_group.add(m_exit);
224 this.getContentPane().setLayout(m_borderLayout1);
225 this.setTitle("Illustrating streams through SQLJ");
226 this.setSize(new Dimension(681, 507));
227 this.addWindowListener(
228 new java.awt.event.WindowAdapter() {
229 public void windowClosing(WindowEvent e) {
230 m_parent.dispatchEvent("EXIT");;
231 }
232 });
233 this.setVisible(true);
234 m_panel1.setForeground(Color.black);
235 m_panel1.setBackground(Color.lightGray);
236 m_panel1.setFont(new Font("Monospaced", 0, 12));
237 m_panel1.setLayout(m_gridBagLayout1);
238 m_panel2.setBackground(Color.lightGray);
239 m_panel2.setFont(new Font("Monospaced", 0, 12));
240 m_panel2.setLayout(m_gridBagLayout2);
241 m_panel3.setBackground(Color.lightGray);
242 m_textarea1.setFont(new Font("Monospaced", 0, 14));
243 m_textarea1.setEditable(false);
244 m_textarea1.setBackground(Color.white);
245 m_textarea1.setForeground(Color.black);
246 m_textarea2.setFont(new Font("Monospaced", 0, 14));
247 m_textarea2.setEditable(false);
248 m_textarea2.setBackground(Color.white);
249 m_textarea2.setForeground(Color.black);
250 m_panel3.setLayout(m_gridBagLayout3);
251 m_label1.setText("");
252 m_label1.setFont(new Font("Dialog", 1, 16));
253 m_label2.setForeground(Color.black);
254 m_label2.setFont(new Font("Dialog", 1, 16));
255 m_label2.setText("TEXT");
256 m_label3.setForeground(Color.black);
257 m_label3.setFont(new Font("Dialog", 1, 16));
258 m_label3.setText("Stream Type : ");
259 m_ascii.setToolTipText("Click here for ascii format...");
260 m_binary.setToolTipText("Click here for binary format...");
261 m_exit.setToolTipText("Click here to exit...");
262 m_exit.setActionCommand("EXIT");
263 m_statusbar.setEditable(false);
264 m_unicode.setToolTipText("Click here for unicode format...");
265 this.getContentPane().add(m_panel1, BorderLayout.NORTH);
266 m_panel1.add(m_label1, new GridBagConstraints2
267 (0, 0, 2, 1, 0.0, 0.0,GridBagConstraints.WEST,
268 GridBagConstraints.NONE,
269 new Insets(10, 14, 0, 0), 219, 19));
270 m_panel1.add(m_textarea1, new GridBagConstraints2
271 (0, 1, 2, 1, 1.0, 1.0,GridBagConstraints.CENTER,
272 GridBagConstraints.VERTICAL,
273 new Insets(0, 5, 8, 0), 411, 141));
274 m_panel1.add(m_label2, new GridBagConstraints2
275 (2, 0, 1, 1, 0.0, 0.0,GridBagConstraints.WEST,
276 GridBagConstraints.NONE,
277 new Insets(10, 0, 0, 0), 78, 8));
278 m_panel1.add(m_textarea2, new GridBagConstraints2
279 (2, 1, 2, 1, 1.0, 1.0,GridBagConstraints.CENTER,
280 GridBagConstraints.VERTICAL,
281 new Insets(0, 4, 7, 10), 121, 142));
282 m_radioPanel.add(m_label3);
283 m_radioPanel.add(m_ascii);
284 m_radioPanel.add(m_unicode);
285 m_radioPanel.add(m_binary);
286 m_radioPanel.add(m_exit);
287 this.getContentPane().add(m_radioPanel,
288 BorderLayout.CENTER);
289 this.getContentPane().add(m_panel3, BorderLayout.SOUTH);
290 m_panel3.add(m_statusbar, new GridBagConstraints2
291 (0, 0, 1, 1, 1.0, 0.0,GridBagConstraints.WEST,
292 GridBagConstraints.HORIZONTAL,
293 new Insets(1, 3, 4, 0), 556, 11));
294 this.setVisible(true);
295
296 }
297
298 }
例程21-14
1 import java.sql.SQLException;
2 import oracle.sqlj.runtime.Oracle;
3
4 public class JDBC20Types
5 {
6
7 /***
8 Using JDBC 2.0 types from java.sql.Xxxx
9 ***/
10 #sql public static iterator JDBC20Iter
11 (int num, java.sql.Blob b, java.sql.Clob c);
12
13 private static void demoJDBC20Types() throws SQLException
14 {
15 System.out.println("*** Demo of JDBC 2.0 types");
16
17 java.sql.Blob blob;
18 java.sql.Clob clob;
19 java.sql.Ref ref;
20
21 System.out.println("SELECT of BLOB and CLOB columns");
22
23 JDBC20Iter ji;
24 #sql ji = { select * from JDBC20_tab };
25 while (ji.next())
26 {
27 System.out.print("#"+ji.num()+" ");
28 printBlob(ji.b());
29 printClob(ji.c());
30 System.out.println();
31 }
32
33 System.out.println("SELECT of REF columns");
34
35 int[] ids= new int[]{ 1, 999 };
36 java.sql.Ref[] r = new java.sql.Ref[ids.length * 2];
37
38 for (int i=0, j=0; i<ids.length; i++)
39 {
40 #sql { select first, second into :(r[j++]), :(r[j++])
41 from jdbc20_refs where id=:(ids[i]) };
42 }
43 for (int i=0; i<r.length; i++)
44 { printRef(r[i]); System.out.println(); }
45 // If you want to do more with a java.sql.Ref object you
46 // have two options:
47 // (1) Use the Ref in SQL code (for example derefence it)
48 // (2) Cast it to an oracle.sql.REF - see also below.
49 }
50
51
52 /***
53 Using similar Oracle-specific types from oracle.sql.XXXX
54 ***/
55 #sql public static iterator OracleIter
56 (int num, oracle.sql.BLOB b, oracle.sql.CLOB c);
57
58 private static void demoOracleTypes() throws SQLException
59 {
60 System.out.println("*** Demo of corrsponding Oracle types");
61
62 oracle.sql.BLOB blob;
63 oracle.sql.CLOB clob;
64 oracle.sql.REF ref;
65
66 System.out.println("SELECT of BLOB and CLOB columns");
67
68 OracleIter oi;
69 #sql oi = { select * from JDBC20_tab };
70 while (oi.next())
71 {
72 System.out.print("#"+oi.num()+" ");
73
74 // Note: the oracle.sql.XXXX types implement the
75 // java.sql.Xxxx interface.Here we call the print
76 // routines we already wrote for the latter.
77 printBlob(oi.b());
78 printClob(oi.c());
79 System.out.println();
80 }
81
82 System.out.println("SELECT of REF columns");
83
84 int[] ids= new int[]{ 1, 999 };
85 oracle.sql.REF[] r = new oracle.sql.REF[ids.length * 2];
86
87 for (int i=0, j=0; i<ids.length; i++)
88 {
89 #sql { select first, second into :(r[j++]), :(r[j++])
90 from jdbc20_refs where id=:(ids[i]) };
91 }
92 for (int i=0; i<r.length; i++)
93 {
94 // Note: Oracle provides an API to obtain the value that
95 // a REF points to
96 printRef(r[i]);
97 System.out.println();
98 }
99 }
100
101 /***
102 Main method of demo
103 ***/
104
105 public static void main(String[] args) throws SQLException
106 {
107 Oracle.connect(JDBC20Types.class, "connect.properties");
108
109 demoJDBC20Types();
110 demoOracleTypes();
111
112 Oracle.close();
113 }
114
115 /***
116 Auxiliary methods for printing java.sql.Blob,
117 java.sql.Clob, and java.sql.Ref instances.
118 ***/
119 private static void printBlob(java.sql.Blob b)
120 throws SQLException
121 {
122 System.out.print("BLOB=");
123 if (b==null) {
124 System.out.print("null ");
125 return;
126 }
127
128 byte[] barr = b.getBytes(1,(int)b.length(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -