Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Servlet/JSP/JSF/JavaFX Script
打印话题 寄给朋友 订阅主题 |
作者 | 急啊~!一个servlet通过BEAN读取数据库问题~! |
naughty026
发贴: 3 |
于 2005-10-11 18:15
servlet代码如下: package login; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Login extends HttpServlet { private ResultSet rs = cb.selectLog(sql); private String password=null; private String sql = "select * from admin"; connBean cb = new connBean(); while(rs.next()) { password = rs.getString(2); } catch(SQLException e) { e.printStackTrace(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { HttpSession session = request.getSession(); String pass = request.getParameter("pass"); if(pass!=null&&pass.equals(password)) { RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/web/success.htm"); rd.forward(request, response); } else { session.invalidate(); RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/web/fail.htm"); rd.forward(request, response); } } } BEAN代码如下: package login; import java.sql.*; public class connBean { private String DbName = "naughty"; private String pass = "1456987"; private String url = "jdbc:mysql://localhost/"+DbName+"?user=root&password="+pass+"&useUnicode=true&characterEncoding=8859_1"; private Connection con = null; private Statement stmt = null; private ResultSet rs = null; private String name = ""; private String sex = ""; private String email = ""; private String address = ""; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setSex(String sex) { this.sex = sex; } public String getSex() { return sex; } public void setEmail(String email) { this.email = email; } public String getEmail() { return email; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } public void BulidCon() { try { String url="jdbc:mysql://localhost/"+DbName+"?user=root&password="+pass+ "&useUnicode=true&characterEncoding=8859_1"; Class.forName("com.mysql.jdbc.Driver").newInstance(); System.out.println("Success loading Mysql Driver!"); con=DriverManager.getConnection(url); stmt=con.createStatement(); System.out.println("连接成功"); } catch (Exception e) { e.printStackTrace(); } } public void updateLog() { try { String sql = "insert into naughty values('" + name + "','" + sex + "','" + email + "','" + address + "')"; BulidCon(); stmt.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } } public ResultSet selectLog(String sql) { try { BulidCon(); rs = stmt.executeQuery(sql); } catch(Exception e) { System.out.println(e.toString()); } return rs; } public void close() { try { con.close(); stmt.close(); rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } BEAN编译没问题~但servlet出现这些错误: Login.java:15: 非法的类型开始 while(rs.next()) ^ Login.java:44: 需要 <标识符> } ^ 2 错误 请大虾们帮下手啊~~~~~~~ 感谢ING~~~~~~~ 请求JAVA学习方向????? |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
1440 | 急啊~!一个servlet通过BEAN读取数据库问题~! | naughty026 | 3183 | 2005-10-11 18:15 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |