初学者的问题
初学JSP有许多问题不懂,最近就有一个做登录的,在javabean中定义了两个类
public class ConnUtil {
public static Connection getuser() {
Connection conn = null;
try {
String url = "jdbc:odbc:news ";
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
conn = DriverManager.getConnection(url, " ", " ");
} catch (Exception e) {
System.out.print(e.toString());
}
return conn;
}
public static void closeConnection(Connection conn) {
try {
conn.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
还有一个查询表的
public class UserDenglu {
Connection conn = ConnUtil.getuser();
public ResultSet getdentlu(Connection conn, HttpServletRequest request) {
ResultSet rs = null;
try {
String username = request.getParameter( "username ");
String password = request.getParameter( "password ");
Statement smt = conn.createStatement();
String sql = "select username,password from usersdenglu where username= ' "+username+ " ' ";
sql+= "and password= ' "+password+ " ' ";
smt.executeQuery(sql);
System.out.println(sql);
} catch (Exception e) {
System.out.println(e.toString());
}
return rs;
}
在页面中这样调用:
<%
String username = request.getParameter( "username ");
String password = request.getParameter( "password ");
request.setCharacterEncoding( "gb2312 ");
Connection conn = ConnUtil.getuser();
UserDenglu bean = new UserDenglu();
ResultSet rs = bean.getdentlu(conn, request);
if(rs.getString( "Username ")!=null && rs.getString( "password ")!=null){
if (rs.next()) {
session.setAttribute( "username ", username);
session.setAttribute( "password ", password);
response.sendRedirect( "homepage.jsp ");
}
} else {
out.print( " <script language=javascript> alert( '用户名或密码错误! ') ");
}
%>
但是运行时说第一个if语句哪儿有问题;真的不知道怎么解决,麻烦各位高手帮忙看一下,是哪儿错呢?在下不胜感激
[解决办法]
应该是action属性没有设置好