当前位置: 代码迷 >> Java Web开发 >> 一个论坛登陆与注册连接数据库问题
  详细解决方案

一个论坛登陆与注册连接数据库问题

热度:379   发布时间:2007-09-25 11:34:04.0
一个论坛登陆与注册连接数据库问题

//登陆页面
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>bookStore</title>
</head>
<body>

<h1>欢迎光临</h1>
<form action="sucess" method="POST">
<table border="1">
<tbody>
<tr>
<td>用户名:<input type="text" name="user"></td>
</tr>
<tr>
<td>密码 :<input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="提交" /> <A href="register">还没注册</A></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
//注册页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

//<html lang='zh'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>注册页面</title>
</head>
<body>
<form action="register" method="POST">
<table border="1">
<tbody>
<tr>
<td>用户名:</td>
<td><input type="text" name="user" value="" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password1" value="" /></td>
</tr>
<tr>
<td>核对密码:</td>
<td><input type="password" name="password2" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
<td><input type="submit" value="重填" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
//注册处理
/*
* Register.java
*
* Created on 2007年9月25日, 上午8:11
*/

package moon;

import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Register extends HttpServlet {
String driverClass;
String url;
String user;
String password;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("text/html;charset=GBK");
String userName = request.getParameter("user");
String userPassword1 = request.getParameter("password1");
String userPassword2 = request.getParameter("password2");
String userPassword = null;
initJDBC();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM user");
}
catch(Exception e)
{
e.printStackTrace();
}
if(userPassword1!=userPassword2)
{
out.print("<h1>2次输入密码不一致</h1>");
}
else if(userPassword1==userPassword2)
{
userPassword = userPassword1;
}
else if(userName==null)
{
out.print("<h1>用户名不能为空</h1>");
}
else
{
try
{
while(rs.next())
{
if(rs.getString("user").equals(user))
{
out.print("<h1>用户已被注册</h1>");
}
else
{
stmt.executeUpdate("INSERT INTO user VALUES (userName,userPassword)");
out.println("<h1>注册成功</h1>");
out.println("<h2>你的用户名为:"+userName+"</h2>");
out.println("<h2>你的密码为:"+userPassword+"</h2>");
}

}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
out.close();
}
private void initJDBC()
{
ServletContext context = getServletContext();
driverClass = context.getInitParameter("driverClass");
url = context.getInitParameter("url");
user = context.getInitParameter("password");
password = context.getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
processRequest(request, response);
}


public String getServletInfo() {
return "Short description";
}

}
//登陆处理
/*
* Scuess.java
*
* Created on 2007年9月25日, 上午9:23
*/

import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Scuess extends HttpServlet {
String driverClass;
String url;
String user;
String password;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("text/html;charset=GBK");
String userName = request.getParameter("user");
String userPassword = request.getParameter("password");
initJDBC();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM user");
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if(rs.getString("user").equals(user)&&rs.getString("password").equals(userPassword))
{

out.println("<h1>欢迎你"+userName+"</h1>");
}
else
{
out.println("<h1>输入的用户名或者密码有误</h1>");
}
}
catch (SQLException ex)
{
ex.printStackTrace();
}
out.close();
/* finally
{
if(rs!=null)
rs.close(); //为什么一执行这里就有问题
if(stmt!= null);
stmt.close();
if(conn!= null)
conn.close();
}
}*/
}
private void initJDBC()
{
ServletContext context = getServletContext();
driverClass = context.getInitParameter("driverClass");
url = context.getInitParameter("url");
user = context.getInitParameter("password");
password = context.getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(Exception e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

public String getServletInfo() {
return "Short description";
}
}
//程序出不来,不知道什么原因,我是跑在netBeans上的,连JSP都跑不起来提示我错误500,高手帮看下

搜索更多相关主题的帖子: quot  数据库  论坛  library  target  

----------------解决方案--------------------------------------------------------


你在finally中 写

finally
{
if(conn!= null)
conn.close();
}


----------------解决方案--------------------------------------------------------

if(conn!=null){
conn.close();

}


----------------解决方案--------------------------------------------------------
哪里来那么多"close()"?
----------------解决方案--------------------------------------------------------
stmt.executeUpdate("INSERT INTO user VALUES (userName,userPassword)");
这里 错了
改成
stmt.executeUpdate("INSERT INTO user VALUES ("+userName+","+userPassword"+")");

最好 自己检查

自己的 SQL 语句啦~!

----------------解决方案--------------------------------------------------------
我再看看
谢谢兄弟们
----------------解决方案--------------------------------------------------------
以下是引用xrqsjj在2007-9-25 12:58:09的发言:


你在finally中 写

finally
{
if(conn!= null)
conn.close();
}

什么意思


----------------解决方案--------------------------------------------------------
finally{

//这里面写的是关闭连接的
//一般都是按ResultSet---> Statement ---> Connection
//的顺序关闭

}
----------------解决方案--------------------------------------------------------
以下是引用skyland84在2007-9-25 20:07:06的发言:
stmt.executeUpdate("INSERT INTO user VALUES (userName,userPassword)");
这里 错了
改成
stmt.executeUpdate("INSERT INTO user VALUES ("+userName+","+userPassword"+")");

最好 自己检查

自己的 SQL 语句啦~!

真仔细


----------------解决方案--------------------------------------------------------
以下是引用夜雨葬花魂在2007-9-26 21:03:16的发言:
finally{

//这里面写的是关闭连接的
//一般都是按ResultSet---> Statement ---> Connection
//的顺序关闭

}

难道我不是这么做的吗?
呵呵

----------------解决方案--------------------------------------------------------

  相关解决方案