当前位置: 代码迷 >> 应用服务器 >> web project 读取数据库内容有关问题
  详细解决方案

web project 读取数据库内容有关问题

热度:5518   发布时间:2013-02-26 00:00:00.0
web project 读取数据库内容问题
我想做一个查看自己的用户信息的功能,在主页面点一个<a href="viewaccount.do">View Account</a>连接,直接可以显示数据库中内容

链接页面:<a href="viewaccount.do">View Account</a>

web.xml配置:
  <servlet>
<servlet-name>ViewAccountServlet</servlet-name>
<servlet-class>com.lu.servlet.VeiwAccountServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewAccountServlet</servlet-name>
<url-pattern>/viewaccount.do</url-pattern>
</servlet-mapping>

ViewAccountServlet.java:
public class ViewAccountServlet extends HttpServlet {
/**

*/
private static final long serialVersionUID = 1L;

@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html;charset=gb2312");
// 取出表单中用户输入的用户名
String username = request.getParameter("username");
System.out.println("=================>"+username);
// 取出表单中用户输入的密码
System.out.println("u能传?"+username);

PrintWriter pw=resp.getWriter();
HttpSession session = request.getSession();
session.setAttribute("username", username);
System.out.println("ok");
request.setAttribute("username", username);


String sql="select * from account where username='" + username + "';";

try {
Connection con=ConnectionManager.getConnection();
Statement stmt=con.createStatement();
ResultSet rsLogon = stmt.executeQuery(sql);

ArrayList a=new ArrayList();


while(rsLogon.next()){ 
String Username=rsLogon.getString("username");
String Telephone=rsLogon.getString("telephone");
String Email=rsLogon.getString("email");
Account m=new Account();
m.setUsername(Username);
m.setTelephone(Telephone);
m.setEmail(Email);
a.add(m);
}
// session.setAttribute("b", a);
resp.sendRedirect("viewaccount.jsp");
System.out.println("ok");

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//
// pw.println(true);
// System.out.println("===pw==="+pw);
// resp.sendRedirect("success.jsp");
}



@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw=resp.getWriter();
String username = req.getParameter("username");
System.out.println("u能传过来吗?"+username);

HttpSession session = req.getSession();
session.setAttribute("username", username);
req.setAttribute("username", username);


String sql="select * from account where username='" + username + "';";

try {
Connection con=ConnectionManager.getConnection();
Statement stmt=con.createStatement();
ResultSet rsLogon = stmt.executeQuery(sql);

ArrayList a=new ArrayList();


while(rsLogon.next()){ 
String Username=rsLogon.getString("username");
String Telephone=rsLogon.getString("telephone");
String Email=rsLogon.getString("email");
Account m=new Account();
m.setUsername(Username);
m.setTelephone(Telephone);
  相关解决方案