下面的程序是我从书上抄写的,其他都没有什么错误,就是在编译时出现try without catch or finally ;}expect;这两个错误;
书上说JSP不会抛出SQLException,可以通过编译,可是在我这里为什么不行呢?
辛苦大家了!!!<%@page contentType="text/html; charset=GBK" errorPage="error.jsp" %>
<%@page import="bookstore.*"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="userBean" scope="page" class="bookstore.User"/>
<jsp:setProperty name="userBean" property="*"/>
<%
Connection conn = null;
try {
conn = DBConnection.getConnection();
PreparedStatement pstat = conn.prepareStatement(
"select name from user where id=? and password=?");
//pstat.setint(1,userBean.getId());
pstat.setInt(1, userBean.getId());
pstat.setString(2, userBean.getPassword());
ResultSet rs = pstat.executeQuery();
if (rs.next()) {
userBean.setId(rs.getInt(1));
session.setAttribute("ses_userBean", userBean);
%>
<jsp:forward page="welcome.jsp"></jsp:forward>
<%
} else {%>
<jsp:forward page=""></jsp:forward>
}
}
finally{
if(conn!=null)
conn.close();}
%>
书上说JSP不会抛出SQLException,可以通过编译,可是在我这里为什么不行呢?
辛苦大家了!!!<%@page contentType="text/html; charset=GBK" errorPage="error.jsp" %>
<%@page import="bookstore.*"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="userBean" scope="page" class="bookstore.User"/>
<jsp:setProperty name="userBean" property="*"/>
<%
Connection conn = null;
try {
conn = DBConnection.getConnection();
PreparedStatement pstat = conn.prepareStatement(
"select name from user where id=? and password=?");
//pstat.setint(1,userBean.getId());
pstat.setInt(1, userBean.getId());
pstat.setString(2, userBean.getPassword());
ResultSet rs = pstat.executeQuery();
if (rs.next()) {
userBean.setId(rs.getInt(1));
session.setAttribute("ses_userBean", userBean);
%>
<jsp:forward page="welcome.jsp"></jsp:forward>
<%
} else {%>
<jsp:forward page=""></jsp:forward>
}
}
finally{
if(conn!=null)
conn.close();}
%>
解决方案 »
- sql异常,求教
- <form target="_blank">被浏览器拦截,如何表单提交弹出新窗口不被拦截
- tomcat内存问题
- 关于session监视器
- spring中设置hibernate.cfg.xml问题
- jsp 编码不识别问题
- 高手看过来:怎么在.jsp.html 页面画一个 calendar ?(随当前的月份而变化) 并且点击上面的数字得到值并提交?
- sqlServer显示汉字问题
- 挑战真正的JSP高手—Request.getParameter(String content)的中文长度受限问题!!!
- 请教:jsp用unicode存放字符串到数据库(mysql),读出来是乱码,怎么办?
- jsp连接sqlserver2005(express)的问题,请大家帮帮忙
- 有关一个hibernate关联查询的问题.
}catch(Exception ee){}finally{}这是捕捉异常的格式,可以多个catch ,楼主自己修改一下吧,很明显是语法错误的。
<% //---这个地方少了个
}
}
finally{
if(conn!=null)
conn.close();}
%>
<jsp:forward page=""></jsp:forward>
<% //---这个地方少了个
}
}
finally{
if(conn!=null)
conn.close();}
%>
catch
catchcatchcatchcatchcatch
catch
catch
catch
<%@page import="bookstore.*"%>
<%@page import="java.sql.*"%>
<jsp:useBean id="userBean" scope="page" class="bookstore.User"/>
<jsp:setProperty name="userBean" property="*"/>
<%
Connection conn = null;
try {
conn = DBConnection.getConnection();
PreparedStatement pstat = conn.prepareStatement( "select name from user where id=? and password=?");
pstat.setInt(1, userBean.getId());
pstat.setString(2, userBean.getPassword());
ResultSet rs = pstat.executeQuery();
if (rs.next()) {
userBean.setId(rs.getInt(1));
session.setAttribute("ses_userBean", userBean);
%>
<jsp:forward page="welcome.jsp"></jsp:forward>
<%
} else {%>
<jsp:forward page=""></jsp:forward>
<% }
}catch(Exception e){
out.println("gfsdfdsa");
}
finally{
if(conn!=null)
conn.close();
}
%>
lz,试试
bigc2001(大C) 和 shan1119(大天使,卐~解!) 说得那样,就是
错在了那里。不过同时也谢谢liuzi123() 写那么多,可惜不是,错在
那里。
总而言之,谢谢各位!!!!!!!!!