第一种写法
public void getit(Connection con,String id)throws Exception
{
String sql="select * from .. where id="+id;
Statement sm = conn.createStatement();
ResultSet rs = sm.executeQuery(sql);
if(rs.next){
System.out.print(rs.getString(2));
}
if(rs!=null)
rs.close();
rs=null;
if(sm!=null)
sm.close();
sm=null;
}
第二种写法
public void getit(Connection con,String id)throws Exception
{
Statement sm =null;
ResultSet rs=null;
String sql="select * from .. where id="+id;
try{
sm = conn.createStatement();
rs = sm.executeQuery(sql);
if(rs.next){
System.out.print(rs.getString(2));
}}catch(Exception e){
throw new Exception(e);
}finally{
if(rs!=null)
rs.close();
rs=null;
if(sm!=null)
sm.close();
sm=null;
}
}
第二种写法比较安全,但是他会影响性能。
public void getit(Connection con,String id)throws Exception
{
String sql="select * from .. where id="+id;
Statement sm = conn.createStatement();
ResultSet rs = sm.executeQuery(sql);
if(rs.next){
System.out.print(rs.getString(2));
}
if(rs!=null)
rs.close();
rs=null;
if(sm!=null)
sm.close();
sm=null;
}
第二种写法
public void getit(Connection con,String id)throws Exception
{
Statement sm =null;
ResultSet rs=null;
String sql="select * from .. where id="+id;
try{
sm = conn.createStatement();
rs = sm.executeQuery(sql);
if(rs.next){
System.out.print(rs.getString(2));
}}catch(Exception e){
throw new Exception(e);
}finally{
if(rs!=null)
rs.close();
rs=null;
if(sm!=null)
sm.close();
sm=null;
}
}
第二种写法比较安全,但是他会影响性能。
解决方案 »
- 用JavaMail发送邮件,如何在邮件中插入图片
- javaweb初学者可以看看我们写的这个示例项目
- 页面定时执行判断语句的问题
- struts+hibernate图片存储问题!
- 求救:再线等答案!!!
- 关于ELIPSE的问题,请高手指点一下
- jsp+JavaBeans中那个.java文件必须要有package ???;吗?
- jspSmartUpload 下载中文问题
- acess数据库连接的错误(jsp)?
- java.lang.NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/Strin
- 请问这个页面怎么提交后什么看不见??
- 对于在网页中显示数据如果用APPLET写个GRID大家觉得如何?
{
Statement sm =null;
ResultSet rs=null;
String sql="select * from .. where id="+id;
try{
sm = conn.createStatement();
rs = sm.executeQuery(sql);
if(rs.next){
System.out.print(rs.getString(2));
}}catch(Exception e){
e.printStack();
}finally{
if(rs!=null)
rs.close();
rs=null;
if(sm!=null)
sm.close();
sm=null;
}
}
这么写不好吗
但是如果resultset 或者statement 自身在出现错误的时候能自行关系,那使用第一种写法更好。是不是?