注册 登录
编程论坛 J2EE论坛

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

大嘴先生2 发布于 2007-09-25 11:34, 1967 次点击

//登陆页面
<%@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.html">还没注册</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,高手帮看下

18 回复
#2
xrqsjj2007-09-25 12:58


你在finally中 写

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

#3
xrqsjj2007-09-25 12:59

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

}

#4
china25qd2007-09-25 19:57
哪里来那么多"close()"?
#5
skyland842007-09-25 20:07
stmt.executeUpdate("INSERT INTO user VALUES (userName,userPassword)");
这里 错了
改成
stmt.executeUpdate("INSERT INTO user VALUES ("+userName+","+userPassword"+")");

最好 自己检查

自己的 SQL 语句啦~!
#6
大嘴先生22007-09-26 18:38
我再看看
谢谢兄弟们
#7
大嘴先生22007-09-26 18:41
以下是引用xrqsjj在2007-9-25 12:58:09的发言:


你在finally中 写

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

什么意思

#8
夜雨葬花魂2007-09-26 21:03
finally{

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

}
#9
千里冰封2007-09-27 14:48
以下是引用skyland84在2007-9-25 20:07:06的发言:
stmt.executeUpdate("INSERT INTO user VALUES (userName,userPassword)");
这里 错了
改成
stmt.executeUpdate("INSERT INTO user VALUES ("+userName+","+userPassword"+")");

最好 自己检查

自己的 SQL 语句啦~!

真仔细

#10
大嘴先生22007-09-27 18:25
以下是引用夜雨葬花魂在2007-9-26 21:03:16的发言:
finally{

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

}

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

#11
大嘴先生22007-09-27 18:31
以下是引用skyland84在2007-9-25 20:07:06的发言:
stmt.executeUpdate("INSERT INTO user VALUES (userName,userPassword)");
这里 错了
改成
stmt.executeUpdate("INSERT INTO user VALUES ("+userName+","+userPassword"+")");

最好 自己检查

自己的 SQL 语句啦~!

为什么要这样改呢?!
多麻烦呀!?
再指教下!

#12
千里冰封2007-09-27 19:11

如果你不这样改的话,你就是直接把userName和userPassword这两个字符串写入数据库了

stmt.executeUpdate("INSERT INTO user VALUES ("+userName+","+userPassword"+")");
如果这样的话,就是把userName和userPassword变量所代表的内容写入数据库去了,你希望是哪种呢?

#13
大嘴先生22007-09-28 12:21

呵呵,理解了
我还在想,到底是哪出了毛病呢
谢谢了
#14
大嘴先生22007-09-28 12:22

那为什么 /* finally
{
if(rs!=null)
rs.close(); //为什么一执行这里就有问题
if(stmt!= null);
stmt.close();
if(conn!= null)
conn.close();
}
}*/
代码加上就报错呢?
也不提示try/catch
不关的话是不是很占资源呢?!

#15
无缘今生2007-09-28 21:53

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

最好 自己检查

自己的 SQL 语句啦~![/QUOTE]

好像还是有问题啊.

是不是应该写成这样啊:
stmt.executeUpdate("INSERT INTO user VALUES ("+userName+","+userPassword+")");
(去掉后面倒数第三个双引号)

#16
luoxian_20032007-09-30 11:24
你的最后一个if后面多了一个 “ }”
唐僧告诉我们编程一定要仔细
#17
xrqsjj2007-10-06 16:39

关闭最外面的一个就行了!!!!写那么多.麻烦!!

#18
大嘴先生22007-10-08 20:22
不是才学么
详细点总是没什么错误的
#19
大嘴先生22007-10-08 20:23
可是还是没有回答我为什么报错呢
1