Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 急啊~!一个servlet通过BEAN读取数据库问题~!
naughty026





发贴: 3
于 2005-10-11 18:15 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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