1
//例子1
2
import java.sql.*;
3
public class Example23_1
4

{ public static void main(String args[])
5
{ Connection con;Statement sql; ResultSet rs;
6
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
7
}
8
catch(ClassNotFoundException e)
9
{ System.out.println(""+e);
10
}
11
try
12
{
13
con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
14
sql=con.createStatement();
15
rs=sql.executeQuery("SELECT * FROM chengjibiao");
16
while(rs.next())
17
{ String number=rs.getString(1);
18
String name=rs.getString(2);
19
Date date=rs.getDate(3);
20
int math=rs.getInt("数学");
21
int physics=rs.getInt("物理");
22
int english=rs.getInt("英语");
23
System.out.println("学号:"+number);
24
System.out.print(" 姓名:"+name);
25
System.out.print(" 出生:"+date);
26
System.out.print(" 数学:"+math);
27
System.out.print(" 物理:"+physics);
28
System.out.print(" 英语:"+english);
29
}
30
con.close();
31
}
32
catch(SQLException e1)
{}
33
34
}
35
}
36
37
//例子2
38
import java.sql.*;
39
public class Example23_2
40

{ public static void main(String args[])
41
{ Connection con;Statement sql; ResultSet rs;
42
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
43
}
44
catch(ClassNotFoundException e)
45
{ System.out.println(""+e);
46
}
47
try
48
{ con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
49
sql=con.createStatement();
50
rs=sql.executeQuery("SELECT 姓名,英语 FROM chengjibiao WHERE 英语 >= 80 ");
51
while(rs.next())
52
{ String name=rs.getString(1);
53
int english=rs.getInt(2);
54
System.out.println(" 姓名:"+name);
55
System.out.print(" 英语:"+english);
56
}
57
con.close();
58
}
59
catch(SQLException e1)
{}
60
}
61
}
62
63
//例子3
64
import java.awt.*;import java.net.*;
65
import java.sql.*;import java.awt.event.*;
66
class DataWindow extends Frame implements ActionListener
67

{ TextField englishtext;TextArea chinesetext; Button button;
68
DataWindow()
69
{ super("英汉小词典");
70
setBounds(150,150,300,120);
71
setVisible(true);
72
englishtext=new TextField(16);
73
chinesetext=new TextArea(5,10);
74
button=new Button("确定");
75
Panel p1=new Panel(),p2=new Panel();
76
p1.add(new Label("输入要查询的英语单词:"));
77
p1.add(englishtext);
78
p2.add(button);
79
add(p1,"North");add(p2,"South");add(chinesetext,"Center");
80
button.addActionListener(this);
81
addWindowListener(new WindowAdapter()
82
{ public void windowClosing(WindowEvent e)
83
{ System.exit(0);
84
}
85
});
86
}
87
public void actionPerformed(ActionEvent e)
88
{ if(e.getSource()==button)
89
{ chinesetext.setText("查询结果");
90
try
{ Liststudent();
91
}
92
catch(SQLException ee)
{}
93
}
94
}
95
public void Liststudent() throws SQLException
96
{ String cname,ename;
97
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
98
}
99
catch(ClassNotFoundException e)
{}
100
Connection Ex1Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
101
Statement Ex1Stmt=Ex1Con.createStatement();
102
ResultSet rs=Ex1Stmt.executeQuery("SELECT * FROM 表1 ");
103
boolean boo=false;
104
while((boo=rs.next())==true)
105
{ ename=rs.getString("单词");
106
cname=rs.getString("解释");
107
if(ename.equals(englishtext.getText()))
108
{
109
chinesetext.append('\n'+cname);
110
break;
111
}
112
}
113
Ex1Con.close();
114
if(boo==false)
115
{ chinesetext.append('\n'+"没有该单词");
116
}
117
}
118
}
119
public class Example23_3
120

{ public static void main(String args[])
121
{ DataWindow window=new DataWindow();window.pack();
122
}
123
}
124
125
//例子4
126
import java.sql.*;
127
public class Example23_4
128

{ public static void main(String args[])
129
{ Connection con;Statement sql; ResultSet rs;
130
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
131
}
132
catch(ClassNotFoundException e)
133
{ System.out.println(""+e);
134
}
135
try
136
{ con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
137
sql=con.createStatement();
138
sql=
139
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
140
//返回可滚动的结果集:
141
rs=sql.executeQuery("SELECT 姓名,英语 FROM chengjibiao WHERE 英语 >= 80 ");
142
//将游标移动到最后一行:
143
rs.last();
144
//获取最后一行的行号:
145
int number=rs.getRow();
146
System.out.println("该表共有"+number+"条记录");
147
//为了逆序输出记录,需将游标移动到最后一行之后:
148
rs.afterLast();
149
while(rs.previous())
150
{ String name=rs.getString(1);
151
int english=rs.getInt("英语");
152
System.out.println(" 姓名:"+name);
153
System.out.print(" 英语:"+english);
154
}
155
System.out.println("单独输出第5条记录:");
156
rs.absolute(5);
157
String name=rs.getString(1);
158
int english=rs.getInt("英语");
159
System.out.println(" 姓名:"+name);
160
System.out.print(" 英语:"+english);
161
con.close();
162
}
163
catch(SQLException e1)
{}
164
}
165
}
166
167
//例子5
168
import java.sql.*;
169
public class Example23_5
170

{ public static void main(String args[])
171
{ Connection con;Statement sql; ResultSet rs;
172
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
173
}
174
catch(ClassNotFoundException e)
175
{ System.out.println(""+e);
176
}
177
try
178
{ con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
179
sql=con.createStatement();
180
String condition="SELECT 姓名,英语 FROM chengjibiao ORDER BY 英语";
181
rs=sql.executeQuery(condition);
182
while(rs.next())
183
{ String name=rs.getString(1);
184
int english=rs.getInt(2);
185
System.out.println(" 姓名:"+name);
186
System.out.print(" 英语:"+english);
187
}
188
con.close();
189
}
190
catch(SQLException e1)
{ System.out.print(e1);}
191
}
192
}
193
194
//例子6
195
import java.sql.*;
196
public class Example23_6
197

{ public static void main(String args[])
198
{ Connection con;Statement sql; ResultSet rs;
199
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
200
}
201
catch(ClassNotFoundException e)
202
{ System.out.println(""+e);
203
}
204
try
205
{ con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
206
sql=con.createStatement();
207
rs=sql.executeQuery("SELECT 姓名,数学 FROM chengjibiao WHERE 姓名 LIKE '%小%' ");
208
while(rs.next())
209
{ String name=rs.getString(1);
210
int math=rs.getInt(2);
211
System.out.println(" 姓名:"+name);
212
System.out.print(" 数学:"+math);
213
}
214
con.close();
215
}
216
catch(SQLException e1)
{ System.out.print(e1);}
217
}
218
}
219
220
//例子7
221
import java.awt.*;
222
import java.sql.*;import java.awt.event.*;
223
class DataWindow extends Frame implements ActionListener
224
{ TextField 待查英文单词_文本条,汉语解释_文本条,
225
更新英文单词_文本条,更新汉语解释_文本条,
226
填加英文单词_文本条,填加汉语解释_文本条;
227
Button 查询按扭,更新按扭,填加按扭;
228
int 查询记录=0;
229
Connection Con=null;Statement Stmt=null;
230
DataWindow()
231
{ super("英汉小词典");
232
setBounds(150,150,300,120);
233
setVisible(true);setLayout(new GridLayout(3,1));
234
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
235
catch(ClassNotFoundException e)
{}
236
try
{
237
Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
238
Stmt=Con.createStatement();
239
}
240
catch(SQLException ee)
{}
241
待查英文单词_文本条=new TextField(16);
242
汉语解释_文本条=new TextField(16);
243
更新英文单词_文本条=new TextField(16);
244
更新汉语解释_文本条=new TextField(16);
245
填加英文单词_文本条=new TextField(16);
246
填加汉语解释_文本条=new TextField(16);
247
查询按扭=new Button("查询");
248
更新按扭=new Button("更新");
249
填加按扭=new Button("填加");
250
Panel p1=new Panel(),p2=new Panel(),p3=new Panel();
251
p1.add(new Label("输入要查询的英语单词:"));
252
p1.add( 待查英文单词_文本条);
253
p1.add(new Label("显示英语单词的汉语解释:"));
254
p1.add(汉语解释_文本条);
255
p1.add(查询按扭);
256
p2.add(new Label("输入英语单词:"));p2.add( 更新英文单词_文本条);
257
p2.add(new Label("输入该单词更新的汉语解释:"));
258
p2.add(更新汉语解释_文本条);
259
p2.add(更新按扭);
260
p3.add(new Label("输入英语单词:"));p3.add( 填加英文单词_文本条);
261
p3.add(new Label("输入汉语解释:"));p3.add(填加汉语解释_文本条);
262
p3.add(填加按扭);
263
add(p1);add(p2);add(p3);
264
查询按扭.addActionListener(this);更新按扭.addActionListener(this);
265
填加按扭.addActionListener(this);
266
addWindowListener(new WindowAdapter()
267
{public void windowClosing(WindowEvent e)
268
{setVisible(false);System.exit(0); } } );
269
}
270
public void actionPerformed(ActionEvent e)
271
{if(e.getSource()==查询按扭)
272
{ 查询记录=0;
273
try
{ 查询();}
274
catch(SQLException ee)
{}
275
}
276
else if(e.getSource()==更新按扭)
277
{ try
{ 更新();}
278
catch(SQLException ee)
{}
279
}
280
else if(e.getSource()==填加按扭)
281
{ try
{ 填加();}
282
catch(SQLException ee)
{}
283
}
284
}
285
public void 查询() throws SQLException
286
{ String cname,ename;
287
Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
288
ResultSet rs=Stmt.executeQuery("SELECT * FROM 表1 ");
289
while (rs.next())
290
{ ename=rs.getString("单词"); cname=rs.getString("解释");
291
if(ename.equals( 待查英文单词_文本条.getText().trim()))
292
{ 汉语解释_文本条.setText(cname);查询记录=1; break;
293
}
294
}
295
Con.close();
296
if(查询记录==0)
297
{ 汉语解释_文本条.setText("没有该单词");
298
}
299
}
300
public void 更新() throws SQLException
301
{ String s1="'"+更新英文单词_文本条.getText().trim()+"'",
302
s2="'"+更新汉语解释_文本条.getText().trim()+"'";
303
String temp="UPDATE 表1 SET 解释 ="+s2+" WHERE 单词 = "+s1 ;
304
Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
305
Stmt.executeUpdate(temp); Con.close();
306
}
307
public void 填加() throws SQLException
308
{ String s1="'"+填加英文单词_文本条.getText().trim()+"'",
309
s2="'"+填加汉语解释_文本条.getText().trim()+"'";
310
String temp="INSERT INTO 表1 VALUES ("+s1+","+s2+")";
311
Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
312
Stmt.executeUpdate(temp);
313
Con.close();
314
}
315
}
316
public class Database
317

{ public static void main(String args[])
318
{DataWindow window=new DataWindow();window.pack();
319
}
320
}
321
322
//例子8
323
(1) 客户端程序(效果如图23.11)
324
import java.net.*;import java.io.*;
325
import java.awt.*;import java.awt.event.*;
326
import java.applet.*;
327
public class Database_client extends Applet implements Runnable,ActionListener
328

{ Button 查询;TextField 英文单词_文本框,汉语解释_文本框;
329
Socket socket=null;
330
DataInputStream in=null;
331
DataOutputStream out=null;
332
Thread thread;
333
public void init()
334
{查询=new Button("查询");
335
英文单词_文本框=new TextField(10);汉语解释_文本框=new TextField(10);
336
add(new Label("输入要查询的英文单词"));add(英文单词_文本框);
337
add(new Label("汉语解释:"));add(汉语解释_文本框);add(查询);
338
查询.addActionListener(this);
339
}
340
public void start()
341
{ try
342
{socket = new Socket(this.getCodeBase().getHost(), 4331);
343
in = new DataInputStream(socket.getInputStream());
344
out = new DataOutputStream(socket.getOutputStream());
345
}
346
catch (IOException e)
{}
347
if (thread == null)
348
{thread = new Thread(this);
349
thread.setPriority(Thread.MIN_PRIORITY);
350
thread.start();
351
}
352
}
353
public void stop()
354
{try
{out.writeUTF("客户离开");}
355
catch(IOException e1)
{}
356
}
357
public void destroy()
358
{try
{out.writeUTF("客户离开");}
359
catch(IOException e1)
{}
360
}
361
public void run()
362
{String s=null;
363
while(true)
364
{ try
{s=in.readUTF();
365
}
366
catch (IOException e)
367
{汉语解释_文本框.setText("与服务器已断开");break;
368
}
369
汉语解释_文本框.setText(s);
370
}
371
}
372
public void actionPerformed(ActionEvent e)
373
{if (e.getSource()==查询)
374
{ String s=英文单词_文本框.getText();
375
if(s!=null)
376
{ try
{out.writeUTF(s);}
377
catch(IOException e1)
{}
378
}
379
380
}
381
}
382
}
383
(2) 服务器端程序:
384
import java.io.*;import java.net.*;
385
import java.util.*;import java.sql.*;
386
public class Database_server
387

{ public static void main(String args[])
388
{ ServerSocket server=null;Server_thread thread;
389
Socket you=null;
390
while(true)
391
{ try
{ server=new ServerSocket(4331);
392
}
393
catch(IOException e1)
{System.out.println("正在监听");}
394
try
{ you=server.accept();
395
}
396
catch (IOException e)
397
{System.out.println("正在等待客户");
398
}
399
if(you!=null)
400
{new Server_thread(you).start();
401
}
402
else
{continue;}
403
}
404
}
405
}
406
class Server_thread extends Thread
407

{ Socket socket;Connection Con=null;Statement Stmt=null;
408
DataOutputStream out=null;DataInputStream in=null;
409
String s=null;
410
Server_thread(Socket t)
411
{ socket=t;
412
try
{in=new DataInputStream(socket.getInputStream());
413
out=new DataOutputStream(socket.getOutputStream());
414
}
415
catch (IOException e)
{}
416
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
417
}
418
catch(ClassNotFoundException e)
419
{
420
}
421
try
{ Con=DriverManager.getConnection("jdbc:odbc:test","gxy","ookk");
422
Stmt=Con.createStatement();
423
}
424
catch(SQLException ee)
{}
425
}
426
public void run()
427
{ while(true)
428
{try
{ s=in.readUTF();
429
int n=0;
430
ResultSet rs=
431
Stmt.executeQuery("SELECT * FROM 表1 WHERE 单词 ="+"'"+s+"'" );
432
while (rs.next())
433
{ String 英语单词=rs.getString("单词");
434
if(s.equals(英语单词))
435
{ out.writeUTF(rs.getString("解释")); n=1;break;
436
}
437
if(n==0)
{out.writeUTF("没有此单词");}
438
}
439
sleep(45);
440
}
441
catch(Exception ee)
442
{ break;
443
}
444
445
}
446
}
447
}
448

2

3

4



5



6



7

8

9



10

11

12



13

14

15

16

17



18

19

20

21

22

23

24

25

26

27

28

29

30

31

32



33

34

35

36

37

38

39

40



41



42



43

44

45



46

47

48



49

50

51

52



53

54

55

56

57

58

59



60

61

62

63

64

65

66

67



68

69



70

71

72

73

74

75

76

77

78

79

80

81

82



83



84

85

86

87

88



89



90



91

92



93

94

95

96



97



98

99



100

101

102

103

104

105



106

107

108



109

110

111

112

113

114

115



116

117

118

119

120



121



122

123

124

125

126

127

128



129



130



131

132

133



134

135

136



137

138

139

140

141

142

143

144

145

146

147

148

149

150



151

152

153

154

155

156

157

158

159

160

161

162

163



164

165

166

167

168

169

170



171



172



173

174

175



176

177

178



179

180

181

182

183



184

185

186

187

188

189

190



191

192

193

194

195

196

197



198



199



200

201

202



203

204

205



206

207

208

209



210

211

212

213

214

215

216



217

218

219

220

221

222

223

224



225

226

227

228

229

230

231



232

233

234



235



236



237

238

239

240



241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267



268



269

270

271



272



273



274



275

276

277




278



279

280

281




282



283

284

285

286



287

288

289

290



291

292



293

294

295

296

297



298

299

300

301



302

303

304

305

306

307

308



309

310

311

312

313

314

315

316

317



318



319

320

321

322

323

324

325

326

327

328



329

330

331

332

333

334



335

336

337

338

339

340

341



342



343

344

345

346



347

348



349

350

351

352

353

354




355



356

357

358




359



360

361

362



363

364




365

366

367



368

369

370

371

372

373



374



375

376




377



378

379

380

381

382

383

384

385

386

387



388



389

390

391




392

393



394



395

396

397



398

399

400



401

402



403

404

405

406

407



408

409

410

411



412



413

414

415



416



417

418

419



420

421



422

423

424



425

426

427



428




429

430

431

432

433



434

435



436

437



438

439

440

441

442



443

444

445

446

447

448
