次程序就是项目中一个页面。没有办法,我们的人员目前只能写出这样的程序
。希望大家帮我侃侃,这个也面有没有问题。
JSP + JAVABEAN

--------



<%@ page contentType="text/html; charset=iso-8859-1" %>
<%@page import=
"java.sql.ResultSet"%>
<%@page import=
"java.text.*"%>
<%@page import =
"java.io.*"%>
<%@page import=
"DataBase.*"%>
<jsp:useBean id=
"p" scope="page" class="page.page"/>
<%! opendb op;
%>
<%try{
op= new opendb();
ResultSet rs = null;
//±&pound;&acute;&aelig;&sup2;é&Ntilde;&macr;&frac12;á&sup1;&ucirc;
int count=0;
//&raquo;&ntilde;&Egrave;&iexcl;&frac14;&Ccedil;&Acirc;&frac14;&micro;&Auml;&Ecirc;&yacute;&Aacute;&iquest;

%>
<html>
<head>
<meta name=
"GENERATOR" content="Microsoft FrontPage 5.0">
<meta name=
"ProgId" content="FrontPage.Editor.Document">

<title>IDB</title>
<meta http-equiv=
"Content-Type" content="text/html; charset=iso-8859-1">
<!-- Fireworks MX Dreamweaver MX target. Created Sun Jun 01 14:18:09 GMT+0800 (?D1ú±ê×?ê±??) 2003-->
<link href=
"../images/css1.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor=
"ffffff" topmargin="0">
<table width=
"716" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan=
"3"><table width="756" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src=
"../images/index_r1_c1.jpg" width="751" height="26"></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan=
"3"><table width="751" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan=
"2"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cabversion=6,0,29,0" width="751" height="96">
<param name=
"movie" value="../images/english.swf">
<param name=
"quality" value="high">
<embed src=
"../images/english.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="751" height="96"></embed>
</object>
</td>
</tr>
<tr>
<td width=
"645"><img src="../images/index_r3_c1_1.jpg" width="525" height="92" border="0" usemap="Map" href="#"><img src="../images/index_r3_c12_1.jpg" width="120" height="92" border="0" usemap="Map2"></td>
<td width=
"106" align="center" valign="bottom" background="../images/index_r3_c15_1.jpg"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cabversion=6,0,29,0" width="75" height="75">
<param name=
"movie" value="../images/index.swf">
<param name=
"quality" value="high">
<embed src=
"../images/index.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="75" height="75"></embed>
</object>
</td>
</tr>
</table>
<map name=
"Map">
<area shape=
"rect" coords="458,49,522,88" href="../index/index_guest.jsp" target="_blank">
<area shape=
"rect" coords="381,46,442,86" href="mailto:ytidb@public.ytptt.sd.cn%20%3Cytidb@yt-public.sd.cninfo.net%3E">
</map>
<map name=
"Map2">
<area shape=
"rect" coords="26,19,118,34" href="../website/index.jsp">
<area shape=
"rect" coords="28,42,115,60" href="../questionnaire/index.jsp" target="_blank">
<area shape=
"rect" coords="28,62,110,80" href="http://www.yantai.gov.cn">
</map></td>
</tr>
<tr>
<td colspan=
"3"><img src="../images/index_r4_c1.jpg" alt="" name="index_r4_c1" width="751" height="36" border="0" usemap="index_r4_c1Map">
<map name=
"index_r4_c1Map"><area shape="rect" coords="104,10,172,27" href="../aboutus/index.jsp">
<area shape=
"rect" coords="380,11,486,28" href="../business/index.jsp">
<area shape=
"rect" coords="176,9,273,28" href="../mayor/index.jsp">
<area shape=
"rect" coords="279,9,371,27" href="../yantaitoday/index.jsp">
<area shape=
"rect" coords="494,9,625,27" href="../investment/index.jsp">
<area shape=
"rect" coords="630,9,737,28" href="../tour/index.jsp">
<area shape=
"rect" coords="11,8,97,27" href="../hompage/index.jsp">
</map></td>
</tr>
<tr>
<td width=
"151" valign="top" background="../images/index_r10_c1_1.jpg">
<table width=
"94%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan=
"3"><img name="index_r5_c1" src="../images/index_r5_c1.jpg" width="149" height="14" border="0" alt=""></td>
</tr>
<tr>
<td width=
"8%" valign="top" background="../images/index_r6_c1.jpg">&nbsp;</td>
<td width=
"87%" valign="top"><div align="center"><img name="index_r6_c3" src="../images/index_r6_c3.jpg" width="129" height="44" border="0" alt=""><br>
<select name=
"select2" size="1" class="smallist">
<option>Agriculture
<option>Light Industry
<option>Textile
<option>Electronics
<option>Pharmacy
<option>Building Materials
<option>Metal Industry
<option>Chemical Industry
<option>Machinery
<option>Infrastructure
<option>Tourism
<option>Others
</select>
</div></td>
<td width=
"5%" valign="top" background="../images/index_r6_c5.jpg">&nbsp;</td>
</tr>
<tr>
<td valign=
"top"><img name="index_r9_c1" src="../images/index_r9_c1.jpg" width="13" height="8" border="0" alt=""></td>
<td valign=
"top"><img name="index_r9_c3" src="../images/index_r9_c3.jpg" width="129" height="8" border="0" alt=""></td>
<td valign=
"top"><img name="index_r9_c5" src="../images/index_r9_c5.jpg" width="7" height="8" border="0" alt=""></td>
</tr>
<tr>
<td colspan=
"3"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cabversion=6,0,29,0" width="147" height="90">
<param name=
"movie" value="../images/index_2.swf">
<param name=
"quality" value="high">
<embed src=
"../images/index_2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="147" height="90"></embed></object></td>
</tr>
<tr>
<td colspan=
"3"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cabversion=6,0,29,0" width="147" height="87">
<param name=
"movie" value="../images/index_4.swf">
<param name=
"quality" value="high">
<embed src=
"../images/index_4.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="147" height="87"></embed></object></td>
</tr>
<tr>
<td colspan=
"3"><div align="center"><font size="2"> </font></div>
<div align=
"center"><font size="2"> </font></div></td>
</tr>
<tr>
<td height=
"50" colspan="3"> <div align="center"><img src="../images/index_r17_c2_2.jpg" width="121" height="37"></div></td>
</tr>
<tr>
<td height=
"51" colspan="3"> <div align="center"><img src="../images/index_r17_c2_3.jpg" width="121" height="37"></div></td>
</tr>
<tr>
<td height=
"51" colspan="3"> <div align="center"><img src="../images/index_r17_c2_4.jpg" width="121" height="37"></div></td>
</tr>
<tr>
<td colspan=
"3">&nbsp;</td>
</tr>
<tr>
<td colspan=
"3">&nbsp;</td>
</tr>
</table>
</td>
<td width=
"4" background="../images/index_r5_c6.jpg">&nbsp;</td>
<td width=
"561" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width=
"13%"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cabversion=6,0,29,0" width="430" height="76">
<param name=
"movie" value="../images/index_18.swf">
<param name=
"quality" value="high">
<embed src=
"../images/index_18.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="430" height="76"></embed></object></td>
<td width=
"87%" align="center"><div align="right"></div></td>
</tr>
<tr>
<td colspan=
"2"><table width="100%" height="19" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height=
"19">&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
<table width=
"100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width=
"65%"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr class=
"ziti">
<td colspan=
"2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><table width=
"100%" height="120" border="0" cellpadding="0" cellspacing="0" class="ti">
<%
//&sup1;ú&frac14;&Ograve;&frac14;&para;&Ouml;&Oslash;&micro;&atilde;&Ocirc;°&Ccedil;&oslash;
rs=op.getdb(
"select * from tb_flags where data_cata_code='TZENGJYQ' or data_cata_code='TZENSJYQ' OR data_cata_code='TZENQTYQ'",0);
while(rs.next()){
String name=rs.getString(
"FLAGS_NAME").trim();
%>

<img src=
"../images/index_r7_c7_1.jpg" width="14" height="12">
<%
if(name.equals(
"Yantai Economic & Technological Development Area")){
%>

<a href=
"index_show.jsp?id=<%=rs.getString("FLAGS_ID")%>"><%=name%>
</a><br><br>
<%}else{
String id=rs.getString(
"FLAGS_ID");
String id1=
"9999";
ResultSet rss= op.getdb(
"select distinct tt.TEXT_ID,tt.TITLE,tt.PUBLICTIME from tb_text tt,tb_flags tf where tt.FLAG="+id+" and tf.data_cata_code='TZENGJYQ' AND tt.LANGUAGE_CODE='ENGLISH'");
if(rss.next())
id1=rs.getString(
"TEXT_ID");
rss.close();

%>

<a href=
"index_show1.jsp?id=<%=id1%>"><%=name%>
</a><br><br>

<%}%>
<%
}rs.close();
%>
</table>
</td>
</tr>
</table> <p>&nbsp;</p>
</td>
</tr>
<tr class=
"ziti">
<td colspan=
"2"><img src="../images/index_r19_c7.jpg" width="580" height="10"></td>
</tr>
<tr class=
"ziti">
<td colspan=
"2"><div align="center"><font color="#666666" size="2">Gopyright
Reserved By Yantai Investment Development Board<br>
Yantai Integrated Information Center Provides Technical Support</font></div></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height=
"87" colspan="3">&nbsp;</td>
</tr>
</table>
</body>
</html>
<%
}finally{
op.Conclose();
}
%>

这段代码是乎没有什么明显的问题,不过你们对SQL的使用不规范,把参数直接拼加到SQL会使很多大型数据库(比如:ORACLE之类)的共享池迅速爆满。

至于内存漏洞的测试,不是一项小工程,先试着用PROFILE工具看看吧。

基于你的现状给点个人建议:
1)修改所有SQL调用代码
2)调整JVM的参数,适当放大JVM内存和调整垃圾回收参数
3)每天晚上(服务闲置时)自动重启你的服务


如何,增大虚拟机参数?垃圾回收参数?
我们的系统跑在 JAVA 1.4.1_01 + TOMCAT4.1.24 FOR 红旗 LINUX 3.0
--------------
如何:调整JVM的参数,适当放大JVM内存和调整垃圾回收参数?
---------------
??? 这段代码是乎没有什么明显的问题,不过你们对SQL的使用不规范,把参数直接拼加到SQL会使很多大型数据库(比如:ORACLE之类)的共享池迅速爆满。

: 大家不都是这么作吗? 不加参数我怎么取数据?
能举一个例子吗??
-----------------

希望在大家的指导下提高我自己。。。。



rs=op.getdb("select * from tb_flags where data_cata_code='TZENGJYQ' or data_cata_code='TZENSJYQ' OR data_cata_code='TZENQTYQ'",0);

在Jsp要杜绝使用ResulSet 或其它SQL直接相关的操作。
参考我主页的“资料”栏目中 用户注册系统 实例。
都需要封装到Javabeans中。

斑竹我是这么做的,业务方法都放在JAVABEAN中。。最后将结果RESULTset 对象 放回给JSP 然后便利输出。。。(不知这种方法是否正确、?)

上面的程序 ,为了方便期间临时写上的。。。

还有,现在JAVA项目 都需要优化JAVA虚拟机吗? 有这样的参考资料吗。?

另外我还发现,如果在应用服务器中有一个页面产生连接数据库不正常。就会导致其他应用(如,其他的虚拟主机)停掉。 就是说,个别页面问题会影响全局。。。! 这种问题不知大家是怎么解决的。。??

别把你的数据库对象定义成静态全局就不会有你说得互相影响的问题。

<%! opendb op; %>
|
<% opendb op; %>

配置JVM要根据具体调试的情况,我就没法给你建议了。

http://java.sun.com/docs/hotspot/VMOptions.html

我的天!这样的代码会让你崩溃的!干吗把resultset返回到表示层???
一般的作法是,在业务逻辑层将结果封装成值对象,以集合的形式(准确的说法是容纳值对象的集合或包含集合的值对象)返回。在jsp中单纯是显示而以

而且,为什么会重起应用服务器都不起作用?太奇怪了...照理说JAVA应用服务器(如果使用java写成的)只是一个java进程,这个进程杀掉以后,不管你的代码中有多少内存泄漏都会释放的...
你的数据库是在同一台机器上吗?是不是数据库造成的?


我们一台做应用,一台做数据库。

----------------------------------
我的天!这样的代码会让你崩溃的!干吗把resultset返回到表示层???
一般的作法是,在业务逻辑层将结果封装成值对象,以集合的形式(准确的说法是容纳值对象的集合或包含集合的值对象)返回。在jsp中单纯是显示而以 .

----------------------------------
我看过值对象。没用过,不知道数据库结果集,是返回什么类型的?数组吗?

在程序当中能这样用吗?《!String title=null;》


再问一个,很庞大应用系统,一般数据库连接池配置最大多少?最小多少?

-------------------------------------------

感谢大家的帮助。



to cute:
你应该先好好看看这里的帖子,关于数据库返回结果应该是`ResultSet或collection早就讨论过了,相信对你有帮助。

一个并行访问比较大的系统,不是增加数据库连接池就解决问题,这些都是表面上的。

从你的源代码知道,你的系统需要重新架构设计,否则没有出路,我碰到过你这样的系统,重复设计其实不难,在设计时要注意性能设计,加入cache,访问数据库的连接池也是很慢的,cache你的实体对象,减少网络操作。

良好的设计和优雅的编程会基本杜绝内存泄漏或其它复杂的问题。

很多人做J2EE的DEMO版可以,但是要真正成为一个成熟产品,则比较有良好的架构设计以及对Java系统深入把握的能力,使用J2EE不象以前使用其它语言,修修补补能够混过去,这种办法用在java上,就会发生死机等现象来报复你,所以说,做好Java系统需要一定功力,不是随便谁都可以做一个成熟的J2EE产品的。

从你的源代码知道,你的系统需要重新架构设计,否则没有出路,我碰到过你这样的系统,重复设计其实不难,在设计时要注意性能设计,加入cache,访问数据库的连接池也是很慢的,cache你的实体对象,减少网络操作。
-----------------------------------


如何架构新的系统? 我们现在的2层结构要设计成3层结构吗?(我们现在不想再用EJB技术)。 斑竹能够提供其他实现技术?


另外,我们结构应为没有采用EJB 技术,还需要返回的值对象吗 ?

今天用 JProbe 进行内存益处测试。 运行时出现问题:

这个问题出在哪里?
-----------------


The JAVA_HOME environment variable is not defined
This environment variable is needed to run this program
Using CATALINA_BASE: F:\Tomcat 4.1
Using CATALINA_HOME: F:\Tomcat 4.1
Using CATALINA_TMPDIR: F:\Tomcat 4.1\temp
Using JAVA_HOME:
JProbe(tm) Memory Debugger Version 4.0.2
(c)1998-2002 by Sitraka Inc.
All rights reserved. JProbe is a trademark of Sitraka Inc.

Waiting for connection from local JProbe viewer on port 4444 ....connected
2003-6-28 8:59:35 org.apache.commons.modeler.Registry loadRegistry
信息: Loading registry information
2003-6-28 8:59:36 org.apache.commons.modeler.Registry getRegistry
信息: Creating new Registry instance
2003-6-28 8:59:38 org.apache.commons.modeler.Registry getServer
信息: Creating MBeanServer
2003-6-28 8:59:42 org.apache.coyote.http11.Http11Protocol init
严重: Error initializing endpoint
java.net.BindException: Address already in use: JVM_Bind:80
at org.apache.tomcat.util.net.PoolTcpEndpoint.initEndpoint(PoolTcpEndpoi
nt.java:280)
at org.apache.coyote.http11.Http11Protocol.init(Http11Protocol.java:150)

at org.apache.coyote.tomcat4.CoyoteConnector.initialize(CoyoteConnector.
java:1117)
at org.apache.catalina.core.StandardService.initialize(StandardService.j
ava:579)
at org.apache.catalina.core.StandardServer.initialize(StandardServer.jav
a:2246)
at org.apache.catalina.startup.Catalina.start(Catalina.java:511)
at org.apache.catalina.startup.Catalina.execute(Catalina.java:400)
at org.apache.catalina.startup.Catalina.process(Catalina.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203)
Catalina.start: LifecycleException: Protocol handler initialization failed: jav
a.net.BindException: Address already in use: JVM_Bind:80
LifecycleException: Protocol handler initialization failed: java.net.BindExcept
ion: Address already in use: JVM_Bind:80
at org.apache.coyote.tomcat4.CoyoteConnector.initialize(CoyoteConnector.
java:1119)
at org.apache.catalina.core.StandardService.initialize(StandardService.j
ava:579)
at org.apache.catalina.core.StandardServer.initialize(StandardServer.jav
a:2246)
at org.apache.catalina.startup.Catalina.start(Catalina.java:511)
at org.apache.catalina.startup.Catalina.execute(Catalina.java:400)
at org.apache.catalina.startup.Catalina.process(Catalina.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203)
Catalina.stop: LifecycleException: This server has not yet been started
LifecycleException: This server has not yet been started
at org.apache.catalina.core.StandardServer.stop(StandardServer.java:2213
)
at org.apache.catalina.startup.Catalina.start(Catalina.java:543)
at org.apache.catalina.startup.Catalina.execute(Catalina.java:400)
at org.apache.catalina.startup.Catalina.process(Catalina.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:203)
请按任意键继续 . . .

为了解决上面问题。我看便了所有关于这样的文章。。
关于返回RESULT 还是 collection 今天下午也全部研究透了。。。
两个字: “经典”!!!

但是其中,只有一点不明白: 关闭CONNECTION 后还能使用 Resultset??

我就搞不明白了。。。怎么可能亚。。我做的 所有项目。这样做都是不可能的 !!! 我认为根JDBC版本没有关系。! 难道根系统架构有关系。
只有实现EJB可以这么做吗???

关于这方面资料。班组没有贴出来。。。我还是在郁闷当中!

还想再提一个问题: 再JSP页面直接使用SQL语句查询! 对项目性能来说,会有什么样的问题出现??? (这里不谈耦合性!) 我想到得到答案是:系统会出现什么样的问题。。???

站在巨人的肩膀上,我会象箭一般的速度前进!

不能使用Resultset了,这是MySQL JDBC的一个bug,在另外一个帖子里讨论了。

关于直接在Jsp使用SQL语句的缺点是,首先是层次不清楚,很多人以为层次不清楚只是代码质量的问题,其实大错特错,层次不清楚关系到运行性能以及出现问题时追查问题的难度,这点不用我解释很多,如果层次清楚,Junit这样的单元测试可以将一个个单元搞得清清楚楚,如果合并起来还是有性能问题或内存泄漏,那么重点就放在模块或组件的衔接上追查。

如果你层次不清楚,谁知道是在一个模块出问题还是组合时出问题?

这是最大的问题,所以层次不清楚也就是紧耦合性,很多没有深入理解耦合性的概念,以为只是理论,实际上松耦合性是软件追求的最大目标,有了松耦合性,才会有复用,才会有模块或组件,才会有多层结构,才会层次清楚,才会出了问题方便维护,方便拓展....等等。

出现问题不能从语言角度追查,当然这方面也不是没有,但是更重要的是从设计思想和框架设计去追查,这也是就是架构设计的重要性。