1 InitialContext ictx = new InitialContext();
2 Context ctx = (Context)ictx.lookup("java:comp/env");
3 DataSource ds = (DataSource)ctx.lookup("jdbc/NewsDB");
4 //Context ctx = new InitialContext();
5 //DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/NewsDB");
6
7 /*
8 Context initContext = new InitialContext();
9 Context envContext = (Context)initContext.lookup("java:comp/env");
10 DataSource ds = (DataSource)envContext.lookup("jdbc/NewsDB");
11 Connection conn = ds.getConnection();
12 */
1 <div id="main">
2 <div id="opt_list">
3 <a>添加新闻</a><br />
4 <a>编辑新闻</a><br />
5 <a>查找新闻</a><br />
6 <a>添加主题</a><br />
7 <a>编辑主题</a>
8 </div>
9 <%!
10 List displayNews() {
11 List newsList = new ArrayList();
12 try {
13 Class.forName("oracle.jdbc.driver.OracleDriver");
14 Connection conn = DriverManager.getConnection(
15 "jdbc:oracle:thin:@localhost:1521:accp", "JSPnewsAdmin",
16 "JSPnewsAdmin");
17
18 String sql = "select * from TNews";
19 Statement stmt = conn.createStatement();
20
21 ResultSet rs = stmt.executeQuery(sql);
22
23 String str = "";
24
25 while (rs.next()) {
26 str = "";
27 for (int i = 0; i <= 8; i++) {
28 str += rs.getObject(3) + "-";
29 }
30 newsList.add(str);
31 }
32 rs.close();
33 stmt.close();
34 conn.close();
35 } catch (ClassNotFoundException e1) {
36
37 } catch (SQLException e2) {
38
39 }
40
41 return newsList;
42
43 }
44
45 List GetNews()
46 {
47 List newsList = new ArrayList();
48
49 try {
50 Class.forName("oracle.jdbc.driver.OracleDriver");
51 Connection conn = DriverManager.getConnection(
52 "jdbc:oracle:thin:@localhost:1521:accp", "JSPnewsAdmin",
53 "JSPnewsAdmin");
54
55 String sql = "select * from TNews";
56 Statement stmt = conn.createStatement();
57
58 ResultSet rs = stmt.executeQuery(sql);
59
60 ResultSetMetaData md = rs.getMetaData();
61 int columnCount = md.getColumnCount();
62
63 String str = "";
64
65 while (rs.next()) {
66 Map rowData = new HashMap();
67 for (int i = 0; i <= columnCount; i++) {
68 rowData.put(md.getColumnName(i), rs.getObject(i));
69 }
70 newsList.add(rowData);
71 }
72
73 rs.close();
74 stmt.close();
75 conn.close();
76 } catch (ClassNotFoundException e1) {
77
78 } catch (SQLException e2) {
79
80 }
81 finally
82 {
83
84 }
85 return newsList;
86 }
87 List nlist = displayNews();
88 List nlist2 = GetNews();
89
90 %>
91 <div id="opt_area">
92 <ul class="classlist">
93 <%
94 out.print(nlist2.size());
95 for(int i = 0;i < nlist2.size(); i ++)
96 {
97 Map data = (HashMap)nlist.get(i);
98 out.print(data.get("NContent"));
99 %>
100 <li>
101 <span>
102 作者<%="Author" %>
103 <a href="#">修改</a>
104 <a href="#">删除</a>
105 </span>
106 </li>
107 <%
108 }
109 %>
110
111 <%
112 for (int i = 0; i < nlist.size(); i++) {
113
114 String[] ninfo = ((String)nlist.get(i)).split("-");
115 %>
116 <li>
117 <%=ninfo[0] %><span>作者<%="123"%>
118 <a href="#">修改</a> <a href="#">删除</a></span>
119 </li>
120 <%
121 if ((i % 5 == 0) && (i != 0)) {
122 %>
123 <li class="space">123</li>
124 <%
125 }
126 }
127 %>
128 </ul>
129 </div>
130 </div>