【发布时间】:2016-05-07 14:48:32
【问题描述】:
我在 oracle 数据库中有一个名为 category 的表,在这个类别表中我有 c_nm 列,数据是 category1,category2,category3,......等等 我希望所有这些数据都存储在我的 jsp 页面上的数组或字符串中。 示例:
<%@ include file"connection.jsp"%> // for connecting to data base
<%! String S1[]= new String[]%> declare a string
<%
rs=stat.executeQuery("select * from category ");
while(rs.next()){
s1[]=rs.getString(c_nm);
}
%>
这是正确的编码还是我应该选择另一种方法来做到这一点?
【问题讨论】:
-
有什么问题?这段代码能编译吗?
-
只是为了理解正确。您有一个名为“category”的数据库,其中有一个名为“category”的表,其中有一个名为“c_nm”的列,其值为“category1”、“category2”、...?
-
我没有得到数组中的所有类别是不是在声明数组时有问题
-
不要不要将 SQL(或 Java)代码放入 JSP。并且不要从 JSP 中启动数据库连接。这真是糟糕的编码风格。
-
完成后关闭结果集很重要。使用
try (ResultSet rs = stat.executeQuery("select * from category")) { /* ...process results... */ }。 (这称为“try-with-resources 块”,它确保在最后调用结果集的close方法——即使抛出异常也是如此。)