hooyantsing's Blog

第69次课程

字数统计: 4.6k阅读时长: 25 min
2019/10/24

源辰76班

第69次课程

2019.10.24

内容

JSP项目案例[廖彦]

0.DBHelper文件

DBHpelper.java

<Damai-Hooy常规版>

注:需要连接池的支持(第67次课程-连接池的创建与使用)。

%!(EXTRA markdown.ResourceType=, string=, string=)

1
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
package com.yc.damai.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class DBHelper {
    
    public static Connection getConnection()  throws SQLException {
       try {
           //创建上下文对象
           Context ctx = new  InitialContext();
           //引入服务规则:java:comp/env/ +  服务名称
           DataSource ds = (DataSource)  ctx.lookup("java:comp/env/mysql/damai");
           //从连接池中取出一个连接返回
           return ds.getConnection();
       } catch (Exception e) {
           throw new RuntimeException(e);
       }
    }
    
    //增删改
    public static int update(String  sql,Object...paramArray) {
       Connection conn = null;
       try {
           conn = getConnection();
           PreparedStatement ps =  conn.prepareStatement(sql);
           for(int  i=0;i<paramArray.length;i++) {
               ps.setObject(i+1,  paramArray[i]);
           }
           return ps.executeUpdate();
       } catch(Exception e){
           throw new RuntimeException(e);
       } finally {
           try {
               if(conn!=null) {
                  conn.close();
               }
           } catch (SQLException e) {
               throw new  RuntimeException(e);
           }
       }
    }
    
    //查
    public static List<Map<String,Object>>  selectList(String sql,Object...paramArray) {
       Connection conn = null;
       try {
           conn = getConnection();
           PreparedStatement ps =  conn.prepareStatement(sql);
           for(int  i=0;i<paramArray.length;i++) {
               ps.setObject(i+1,  paramArray[i]);
           }
           ResultSet rs = ps.executeQuery();
           ResultSetMetaData rsmd =  rs.getMetaData();
           List<Map<String,Object>> ret =  new ArrayList<>();
           while(rs.next()) {
               Map<String,Object> row = new  LinkedHashMap<>();
               for(int  i=1;i<=rsmd.getColumnCount();i++) {
                  String columnName =  rsmd.getColumnName(i);
                  Object columnValue =  rs.getObject(i);
                  row.put(columnName,  columnValue);
               }
               ret.add(row);
           }
           return ret;
       } catch(Exception e){
           throw new RuntimeException(e);
       } finally {
           try {
               if(conn!=null) {
                  conn.close();
               }
           } catch (SQLException e) {
               throw new  RuntimeException(e);
           }
       }
    }
    
    //查询一条记录
    public static Map<String,Object>  queryOne (String sql,Object...paramArray){
       Connection conn = null;
       try {
           conn = getConnection();
           PreparedStatement ps =  conn.prepareStatement(sql);
           for(int  i=0;i<paramArray.length;i++) {
               ps.setObject(i+1,  paramArray[i]);
           }
           ResultSet rs = ps.executeQuery();
           ResultSetMetaData rsmd =  rs.getMetaData();
           if(rs.next()) {
               Map<String,Object> row = new  LinkedHashMap<>();
               for(int  i=1;i<=rsmd.getColumnCount();i++) {
                  String columnName =  rsmd.getColumnName(i);
                  Object columnValue =  rs.getObject(i);
                  row.put(columnName,  columnValue);
               }
               return row;
           }
           return null;
       } catch(Exception e){
           throw new RuntimeException(e);
       } finally {
           try {
               if(conn!=null) {
                  conn.close();
               }
           } catch (SQLException e) {
               throw new  RuntimeException(e);
           }
       }
    }
    
    //分页
    public static List<Map<String,Object>>  queryPage (
           String sql,int page,int  rows,Object...paramArray){
       int startrow = (page - 1) * rows;
       int endrow = page * rows;
       sql = "select t.*\n" +
             " from ( select t.*,rownum rn  from ( "+sql+") t where rownum <= ?) t\n" +
             " where t.rn > ?";  
       Object[] newParamArray = new  Object[paramArray.length + 2];
       System.arraycopy(paramArray, 0,  newParamArray, 0, paramArray.length);
       newParamArray[newParamArray.length -  2] = endrow;
       newParamArray[newParamArray.length -  1] = startrow;
       return  selectList(sql,newParamArray);
    }
    
    //使用聚合函数查询
    public static Object selectValue(String  sql,Object...paramArray) {
       Map<String,Object> row =  queryOne(sql,paramArray);
       if(row==null) {
           return null;
       }else {
           for(Map.Entry<String, Object>  e:row.entrySet()) {
               return e.getValue();
           }
       }
       return null;
    }
}

DBHelper.java

<Damai-廖老师版>

<Damai项目标准版>

相较于Hooy版本加入了Oracle和MySQL数据库分页查询

DBHelper.java

1
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
package com.yc.damai.util;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.io.StringReader;

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

public class DBHelper {

private static String url;

private static String user;

private static String[] password;

/*static {

InputStream in = DBHelper.class.getClassLoader().getResourceAsStream("jdbc.properties");

try {

*//**

* 璇诲彇閰嶇疆鏂囦欢

* Properties key : value

*//*

Properties p = new Properties();

p.load(in);

init( p.getProperty("driver"),

p.getProperty("url"),

p.getProperty("user"),

p.getProperty("password"));

} catch (Exception e) {

throw new RuntimeException("鏁版嵁搴撻┍鍔ㄥ姞杞藉け璐ワ紒", e);

} finally {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}*/

/**

* 鍒涘缓杩炴帴

*

* @return

* @throws NamingException

* @throws SQLException

*/

public static Connection getConnection() {

// 创建JNDI 上下文对象

try{

Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("java:comp/env/mysql/damai");

return ds.getConnection();

} catch (Exception e ){

throw new RuntimeException(e);

}

/*for (int i = 0; i < password.length; i++) {

String p = password[i];

try {

Connection conn = DriverManager.getConnection(url, user, p);

if(i>0) {

String tmp = password[i];

password[i] = password[0];

password[0] = tmp;

}

return conn;

} catch (SQLException e) {

continue;

}

}

throw new RuntimeException(String.format("鑾峰彇鏁版嵁搴撹繛鎺ュけ璐ワ紒杩炴帴URL锛�%s\t鐢ㄦ埛鍚嶏細%s\t瀵嗙爜锛�%s",

DBHelper.url, DBHelper.user, Arrays.toString(DBHelper.password)));*/

}

public static void init(String url) {

init(null, url, null, null);

}

public static void init(String url, String user, String password) {

init(null, url, user, password);

}

public static void init(String driver, String url, String user, String password) {

if( driver != null ) {

try {

Class.forName(driver);

} catch (ClassNotFoundException e) {

throw new RuntimeException("鏁版嵁搴撻┍鍔ㄥ姞杞藉け璐ワ紒", e);

}

}

DBHelper.url = url == null ? DBHelper.url : url;

DBHelper.user = user == null ? DBHelper.user : user;

DBHelper.password = password == null ? DBHelper.password : password.split(";");

}

public static void executeByReader(Reader reader) {

String endl = System.getProperty("line.separator");

Connection conn = null;

BufferedReader br = null;

StringBuilder sql = new StringBuilder();

try {

br = new BufferedReader(reader);

String line;

conn = getConnection();

Statement stat = conn.createStatement();

while ((line = br.readLine())!=null) {

//line = new String(line.getBytes("gbk"), "utf-8");

sql.append(line).append(endl);

if(line.endsWith(";")) {

System.out.println("SQL:" + sql);

stat.execute(sql.toString());

sql.delete(0, sql.length());

}

}

} catch (IOException e) {

throw new RuntimeException("鏁版嵁搴撹鍙ヨ鍙栧け璐�!", e);

} catch (SQLException e) {

throw new RuntimeException("鏁版嵁搴撴搷浣滃け璐�!", e);

} finally {

//Utils.close(reader, br, conn);

}

}

public static void executeByText(String sqltext) {

executeByReader(new StringReader(sqltext));

}

public static void executeByFile(String sqlfile) {

executeByFile(new File(sqlfile));

}

public static void executeByFile(File sqlfile) {

try {

FileInputStream fis = new FileInputStream(sqlfile);

InputStreamReader isr = new InputStreamReader(fis, "utf-8");

executeByReader(isr);

} catch (FileNotFoundException e) {

throw new RuntimeException("鏁版嵁搴撹剼鏈枃浠朵笉瀛樺湪!", e);

} catch (UnsupportedEncodingException e) {

throw new RuntimeException("鏁版嵁搴撹剼鏈枃浠剁紪鐮佹牸寮忛敊璇�!", e);

}

}

/**

* 鎵ц澧炲垹鏀筍QL

*

* @param sql

* @param param

* @return

*/

public static int update(String sql, Object... param) {

Connection conn = null;

try {

// 鑾峰彇杩炴帴

conn = getConnection();

PreparedStatement ps = conn.prepareStatement(sql);

System.out.println(sql);

for (int i = 1; i <= param.length; i++) {

System.out.println("鍙傛暟"+i+" : " + param[i-1]);

ps.setObject(i, param[i - 1]);

}

return ps.executeUpdate();

} catch (SQLException e) {

// 寮傚父杞瀷

throw new RuntimeException("SQL鎵ц閿欒锟�?", e);

} finally {

// 鎵撳嵃鍏抽棴寮傚父

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* 鍙煡璇竴鏉¤褰曠殑鏌ヨ 0~1 琛岋紝 濡傛灉鏌ュ嚭澶氭潯璁板綍锛屽垯鎶涘嚭杩愯鏈熷紓甯革紝鎻愮ず鐢ㄦ埛缁撴灉鏁板ぇ锟�?1

*

* @param sql

* @param param

* @return

*/

public static Map<String, Object> selectOne(String sql, Object... param) {

Connection conn = null;

try {

// 鑾峰彇杩炴帴

conn = getConnection();

PreparedStatement ps = conn.prepareStatement(sql);

System.out.println(sql);

for (int i = 1; i <= param.length; i++) {

System.out.println("鍙傛暟"+i+" : " + param[i-1]);

ps.setObject(i, param[i - 1]);

}

ResultSet rs = ps.executeQuery();

/**

* 鑾峰彇缁撴灉闆嗗厓鏁版嵁瀵硅薄 meta

*/

ResultSetMetaData rsmd = rs.getMetaData();

if (rs.next()) {

Map<String, Object> row = new LinkedHashMap<String, Object>();

for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {

// 鑾峰彇鍒楀悕

String ColumnName = rsmd.getColumnName(i);

// 鑾峰彇鍒楀悕

row.put(ColumnName, rs.getObject(ColumnName));

}

if(rs.next()){

throw new RuntimeException("缁撴灉鏁板ぇ锟�?1!");

} else {

return row;

}

} else {

return null;

}

} catch (SQLException e) {

// 寮傚父杞瀷

throw new RuntimeException("SQL鎵ц閿欒锟�?", e);

} finally {

// 鎵撳嵃鍏抽棴寮傚父

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* 鏌ヨ澶氭潯璁板綍 0~N 锟�?

*

* @param sql

* @param param

* @return

*/

public static List<Map<String, Object>> selectList(String sql, Object... param) {

Connection conn = null;

try {

// 鑾峰彇杩炴帴

conn = getConnection();

PreparedStatement ps = conn.prepareStatement(sql);

System.out.println(sql);

for (int i = 1; i <= param.length; i++) {

System.out.println("鍙傛暟"+i+" : " + param[i-1]);

ps.setObject(i, param[i - 1]);

}

ResultSet rs = ps.executeQuery();

List<Map<String, Object>> ret = new ArrayList<Map<String, Object>>();

/**

* 鑾峰彇缁撴灉闆嗗厓鏁版嵁瀵硅薄 meta

*/

ResultSetMetaData rsmd = rs.getMetaData();

while (rs.next()) {

Map<String, Object> row = new LinkedHashMap<String, Object>();

for (int i = 1; i < rsmd.getColumnCount() + 1; i++) {

// 鑾峰彇鍒楀悕

String ColumnName = rsmd.getColumnName(i);

// 鑾峰彇鍒楀悕

row.put(ColumnName, rs.getObject(ColumnName));

}

ret.add(row);

}

return ret;

} catch (SQLException e) {

// 寮傚父杞瀷

throw new RuntimeException("SQL鎵ц閿欒锟�?", e);

} finally {

// 鎵撳嵃鍏抽棴寮傚父

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* 鏌ヨ鎸囧畾sql璇彞 , 杩斿洖鍗曚釜锟�?, 榛樿杩斿洖绗竴琛岃褰曠殑绗竴涓瓧娈碉拷??

* 濡傛灉鏈煡璇㈠埌缁撴灉杩斿洖null

* 搴旂敤鍦烘櫙: select count(*) from * emp;

*

* @param sql

* @param params 鍙傛暟鍒楄〃

* @return

*/

public static Object selectValue(String sql, Object... param) {

Connection conn = null;

try {

// 鑾峰彇杩炴帴

conn = getConnection();

PreparedStatement ps = conn.prepareStatement(sql);

System.out.println(sql);

for (int i = 1; i <= param.length; i++) {

System.out.println("鍙傛暟"+i+" : " + param[i-1]);

ps.setObject(i, param[i - 1]);

}

ResultSet rs = ps.executeQuery();

if (rs.next()) {

//杩斿洖绗竴琛岃褰曠殑绗竴涓瓧娈碉拷??, OK 浜嗗氨杩欎箞锟�?鍗曠矖鏆达紒锛侊紒

return rs.getObject(1);

} else {

return null;

}

} catch (SQLException e) {

// 寮傚父杞瀷

throw new RuntimeException("SQL鎵ц閿欒锟�?", e);

} finally {

// 鎵撳嵃鍏抽棴寮傚父

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

/**

* ============浠ヤ笅鏄珮绾ч儴锟�? ( 閫夊仛 ) ============

* 鍒嗛〉鏌ヨ鎸囧畾sql璇彞, 杩斿洖鐨勬暟鎹皝瑁呭湪Page瀵硅薄锟�?

* 鏂规硶璋冪敤绀轰緥 :

* selectPage("select * from emp where empno > ?" , 2 , 5, 1000)

* @param sql

* @param page 鏌ヨ鐨勯〉锟�?

* @param rows 姣忛〉鐨勮锟�?

* @param params 鍙傛暟鍒楄〃

* @return

*/

public static Page selectPageForOracle(String sql, int page, int rows, Object... params) {

String totalSql = "select count(*) from ("+sql+")";

Object totalObj = selectValue(totalSql, params);

long total = Long.parseLong(totalObj.toString());

int startRow = (page - 1 ) * rows;

int endRow= page * rows + 1;

String pageSql = " select * from "

+ "(select a.*,rownum rn from ("+sql+") a where rownum < "+endRow+") a"

+ " where a.rn > " + startRow;

List<Map<String,Object>> data = selectList(pageSql,params);

return new Page(total, page, rows, data);

}

public static Page selectPageForMysql(String sql, int page, int rows, Object... params) {

String totalSql = "select count(*) from ("+sql+") a";

Object totalObj = selectValue(totalSql, params);

long total = Long.parseLong(totalObj.toString());

int startRow = (page - 1 ) * rows;

String pageSql = "select * from ("+sql+") a limit " + startRow + ", " + rows;

List<Map<String,Object>> data = selectList(pageSql,params);

return new Page(total, page, rows, data);

}

/**

* 鍒嗛〉鏌ヨ缁撴灉灏佽绫伙紙鍏辨湁鐨勯潤鎬佺殑鍐呴儴绫伙級

*/

public static class Page {

// 褰撳墠椤垫暟锟�?

private List<Map<String,Object>> data;

// 鎬昏锟�?

private long total;

private int page;

private int rows;

public Page(long total, int page, int rows, List<Map<String,Object>> data) {

this.data = data;

this.page = page;

this.rows = rows;

this.total = total;

}

public List<Map<String,Object>> getData() {

return data;

}

public long getTotal() {

return total;

}

public int getPage(){

return page;

}

public int getFirstPage(){

return 1;

}

public int getPreviousPage(){

return page > 1 ? page - 1 : 1;

}

public int getNextPage(){

int lastpage = getLastPage();

return page < lastpage ? page + 1 : lastpage;

}

public int getLastPage(){

long lastPage = total / rows;

return (int) (total % rows == 0 ? lastPage : (lastPage + 1));

}

}

/**

* 鍒ゆ柇鍙傛暟 param 鏄惁涓虹┖ , 涓嶄负绌哄垯杩斿洖鏉′欢 condition, 骞朵笖锟�? param 鍔犲埌 params 锟�?

* 濡傛灉涓虹┖, 鍒欐斁鍥炵┖瀛楃锟�? ( "" ), 鐢ㄤ簬鎷兼帴sql

* 璇ユ柟娉曞彲鑳戒細琚锟�? dao 绫讳腑浣跨敤 , 鍥犳锟�?鍚庤鏀惧埌 DBHelper 涓幓,鎴愪负锟�?涓伐鍏锋柟锟�?

* @param param 鍙傛暟锟�?

* @param params 鍙傛暟闆嗗悎

* @param condition 鏉′欢sql

* @return

*/

public static String buildCondition(Object param, ArrayList<Object> params, String condition) {

if(param!=null) {

if(param instanceof String) {

String sparam = (String) param;

sparam = sparam.trim();

if(sparam.isEmpty()) {

return "";

} else {

param = sparam;

}

}

params.add(param);

return condition;

} else {

return "";

}

}

public static void main(String[] args) throws UnsupportedEncodingException {

String sqlfile = DBHelper.class.getResource("鏁版嵁搴撹剼鏈�.sql").getPath();

sqlfile = URLDecoder.decode(sqlfile, System.getProperty("file.encoding"));

DBHelper.init("jdbc:mysql://127.0.0.1/test");

DBHelper.executeByFile(sqlfile);
}
}

1.”include”JSP命令和”include”JSP动作标签

**276891a9887d8b66538d0868e7062d88.png

**

Header.jsp

1
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
<%@page import="java.util.*"%>
<%@page  import="com.yc.damai.util.DBHelper"%>
<%@ page language="java"  contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<div class="container header">
    
<div class="span5">
       <div class="logo">
           <a href="index.jsp">
               <img  src="image/r___________renleipic_01/logo.png" alt="依依不舍"/>
           </a>
       </div>
    </div>
    <div class="span9">
<div class="headerAd">
    <img src="image/header.jpg" width="320"  height="50" alt="正品保障" title="正品保障"/>
</div> </div>
<div class="span10 last">
       <div class="topNav clearfix">
           <ul>
               
               
               <li id="headerLogin"  class="headerLogin" style="display:  list-item;">
                  <a href="login.jsp">登录</a>|
               </li>
               <li id="headerRegister"  class="headerRegister" style="display:  list-item;">
                  <a href="reg.jsp">注册</a>|
               </li>
               
               
               
               <li id="headerUsername"  class="headerUsername"></li>
               <li id="headerLogout"  class="headerLogout">
                  <a>[退出]</a>|
               </li>
                      <li>
                          <a>会员中心</a>
                          |
                      </li>
                      <li>
                          <a>购物指南</a>
                          |
                      </li>
                      <li>
                          <a>关于我们</a>
                          
                      </li>
           </ul>
       </div>
       <div class="cart">
           <a  href="cart.jsp">购物车</a>
       </div>
           <div class="phone">
               客服热线:
               <strong>96008/53277764</strong>
           </div>
    </div>
    
    
<div class="span24">
       <ul class="mainNav">
                  <li>
                      <a href="index.jsp">首页</a>
                      |
                  </li>
                  
                  <%
                      String cSql = "select  * from category";
                      List<Map<String,Object>> clist =  DBHelper.selectList(cSql);
                      for(Map<String,Object>  row : clist) {
                  %>
                  <li><a  href="clist.jsp?cid=<%=row.get("cid")%>"><%=row.get("cname")%></a>|</li>
                  <%}%>
                          
       </ul>
    </div>
</div>

Footer.jsp

1
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
<%@ page language="java"  contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<div class="container footer">
    <div class="span24">
       <div class="footerAd">
                  <img  src="image/footer.jpg" width="950"  height="52" alt="我们的优势" title="我们的优势"/>
</div> </div>
    <div class="span24">
       <ul class="bottomNav">
                  <li>
                      <a >关于我们</a>
                      |
                  </li>
                  <li>
                      <a>联系我们</a>
                      |
                  </li>
                  <li>
                      <a >诚聘英才</a>
                      |
                  </li>
                  <li>
                      <a >法律声明</a>
                      |
                  </li>
                  <li>
                      <a>友情链接</a>
                      |
                  </li>
                  <li>
                      <a target="_blank">支付方式</a>
                      |
                  </li>
                  <li>
                      <a  target="_blank">配送方式</a>
                      |
                  </li>
                  <li>
                      <a >SHOP++官网</a>
                      |
                  </li>
                  <li>
                      <a >SHOP++论坛</a>
                      
                  </li>
       </ul>
    </div>
    <div class="span24">
       <div class="copyright">Copyright ©  2005-2013 Mango商城 版权所有</div>
    </div>
</div>

2.分类查询

clist.jsp

1
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
<%@page import="java.util.*"%>
<%@page  import="com.yc.damai.util.DBHelper"%>
<%@ page language="java"  contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"  content="text/html; charset=UTF-8"/>
<title>大麦商城</title>
<link href="css/common.css" rel="stylesheet"  type="text/css"/>
<link href="css/product.css"  rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- 引入子页面 include 命令 -->
<%@include file="/common/Header.jsp" %>
<div class="container productList">
    
       <div class="span6">
           <div class="hotProductCategory">
               <%
                  String csSql = "select *  from categorysecond";
                  List<Map<String,Object>>  cslist = DBHelper.selectList(csSql);
                  // clist 在上面查询过了
                  for(Map<String,Object> c  : clist) {
               %>         
               <dl>
                  <dt>
                      <a  href="clist.jsp?cid=<%=c.get("cid")  %>"><%=c.get("cname") %></a>
                  </dt>
                  <%for(Map<String,Object>  cs : cslist){%>
                      <%if(cs.get("cid").equals(c.get("cid"))){%>
                      <dd>
                          <a  href="clist.jsp?csid=<%=cs.get("csid")  %>"><%=cs.get("csname") %></a>
                      </dd>
                      <%} %>
                  <%} %>
               </dl>
               <%} %>
       </div>
</div>
       
       <div class="span18 last">
           
           <form id="productForm"  action="image/蔬菜 - Powered By Mango  Team.htm" method="get">
               <input type="hidden"  id="brandId" name="brandId" value=""/>
               <input type="hidden"  id="promotionId" name="promotionId"  value=""/>
               <input type="hidden"  id="orderType" name="orderType" value=""/>
               <input type="hidden"  id="pageNumber" name="pageNumber"  value="1"/>
               <input type="hidden"  id="pageSize" name="pageSize" value="20"/>
                  
               <div id="result"  class="result table clearfix">
                      <ul>
                      
                      <%
                          String sql =  "select * from product a join categorysecond  b"
                                    + " on  a.csid = b.csid"
                                    + "  where 1=1";
                          String cid =  request.getParameter("cid");
                          String csid =  request.getParameter("csid");
                          String queryId;
                          if(csid!=null){
                              sql += " and  b.csid = ?";
                              queryId =  csid;
                          } else {
                              sql += " and  b.cid = ?";
                              queryId = cid;
                          }
                          List<Map<String,Object>> plist =  DBHelper.selectList(sql,queryId);
                          for(Map<String,Object> p : plist){
                      %>
                          <li>
                              <a  href="detail.jsp?1">
                                 <img  src="<%=p.get("image") %>" width="170"  height="170" style="display:  inline-block;"/>
                                 <span  style='color:green'>
                                     <%=p.get("pname") %>  
                                 </span>
                                 <span  class="price">
                                     商城价:  ¥<%=p.get("shop_price") %>/份
                                 </span>
                              </a>
                          </li>
                      <%} %>
                      
                      </ul>
               </div>
    
    <div class="pagination">
    第1/6
    
    
               
                  <span  class="currentPage">1</span>
               
               
           
               
               
                  <a  href="clist.jsp?1&pageIndex=2">2</a>
               
           
               
               
                  <a  href="clist.jsp?1&pageIndex=3">3</a>
               
           
               
               
                  <a  href="clist.jsp?1&pageIndex=4">4</a>
               
           
               
               
                  <a  href="clist.jsp?1&pageIndex=5">5</a>
               
           
               
               
                  <a  href="clist.jsp?1&pageIndex=6">6</a>
               
           
               
           
               <a class="nextPage"  href="clist.jsp?1&pageIndex=2">&nbsp;</a>
               <a class="lastPage"  href="clist.jsp?1&pageIndex=6">&nbsp;</a>
               
    </div>
           </form>
       </div>
    </div>
<!-- 引入子页面 include JSP动作标签 -->
<jsp:include page="/common/Footer.jsp"/>
</body></html>
CATALOG