JSP+数据库的问题,我相信一定有高手,急!

cm84n7281 2004-12-21 11:30:00
问题是为什么表单的内容提交到register.jsp页面时不出register.html页的内容呢,听说缺一个JAVABEAN不知如何写。
现在把这全部代码写出来。望高手帮助我!

register.html页代码

<!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=gb2312">
<title>用户注册界面</title>
<script language="javascript">
function isValid(form){
if(form.name.value==""){
alert(" 用户名不能为空!");
return false;
}
else if((form.password.value.length<3)||(form.password.value.length>8)){
alert("密码必须是3-8位字母或数字!");
return false;
}
else if((form.e_mail.value=="")||(form.e_mail.value.indexOf('@',0)==-1)||
(form.e_mail.value.indexOf('.',0)==-1)||(form.e_mail.value.length<6)){
alert("请您输入合法的E-mail地址!");
return false;
}
else{
return false;
}
}
</script>
</head>
<body>
<p align="center">JSP表单程序设计</center>
<br>
<form method="post" action="register.jsp" onSubmit="isValid(this);">
<table border="1" cellspacing="1" height="191" bordercolorlight="#993300" bordercolordark="#00CC66" width="500">
<tr>
<td height="14" valign="top" colspan="2">
<p>请输入您的个人信息(标注*的内容必须填写)</p>
</td>
</tr>
<tr>
<td height="14" width="76" align="center">用户名:</td>
<td width="411" height="14" align="left"><font color="#524581">
<input type="text" name="name" size="20">
<font color="#FF0080">*</font></font></td>
</tr>
<tr>
<td height="14" width="76" align="center">密码:</td>
<td width="411" height="14" align="left"><font color="#524581">
<input name="pwd" type="password" id="password" size="20" maxlength="20">
<font color="#FF0080">*</font></font></td>
</tr>
<tr>
<td height="14" width="76" align="center">确认密码:</td>
<td width="411" height="14" align="left"><font color="#524581">
<input type="password" name="Ok_password" size="20" maxlength="20">
<font color="#FF0080">*</font></font></td>
</tr>
<tr>
<td height="14" width="76" align="center">性别:</td>
<td width="411" height="14" align="left"><font color="#524581">
男<input type="radio" name="sex" value="male" checked>
女<input type="radio" name="sex" value="female">
<font color="#FF0080">*</font></font></td>
</tr>
<tr>
<td height="14" width="76" align="center">E-mail:</td>
<td width="411" height="14" align="left"><font color="#524581">
<input type="text" name="e_mail" size="40" maxlength="40">
<font color="#FF0088">*</font></font></td>
</tr>
<tr>
<td height="14" width="76" align="center">主页:</td>
<td width="411" height="6" align="left"><font color="#524581">
<input type="text" name="url" size="40" maxlength="40" value="http://">
<font color="#FF0088">*</font></font></td>
</tr>
</table>
<br>
<input type="submit" size="4" value="立即注册">
</form>
</center>
</body>
</html>


register.jsp页代码
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>注册用户确认</title>
</head>
<body>
<center>
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="RegisterBean" scope="page" class="DataBase.MyDbBean"/>
<%
//获得传递来的变量
String name=request.getParameter("name");
String password=request.getParameter("password");
String re_password=request.getParameter("Ok_password");
String sex=request.getParameter("sex");
String e_mail=request.getParameter("e_mail");
String url=request.getParameter("url");
String sql="select * from reg where names='"+name+"'";
RegisterBean.OpenConn("register","","");
ResultSet rs=RegisterBean.executeQuery("sql");
//以用户输入名为条件检索记录,如存在表示用户名已被占用
if(rs.next()){
rs.close();
out.println("您的用户名已被占用,请换一个重新再试");
//自动跳到注册界面
response.sendRedirect("register.html");
}
else{
//否则,向数据库中插入记录
String strSQL="insert into reg values('"+name+"','"+pwd+"','"+re_password+"','"+sex+"','"+e_mail+"','"+url+"')";
RegisterBean.executeUpdate(strSQL);
%>
<!--显示用户信息-->
<table border="1" cellspacing="1" height="191" width="527" bordercolordark="#FF0000" bordercolorlight="#CCCCCC"#">
<tr>
<td height="1" width="517" valign="top" colspan="2">
<p>用户信息如下: </p>
</td>
</tr>
<tr>
<td height="14" width="123" align="left">用户名:</td>
<td width="388" height="10" align="left"><font color="#524581">
<%=name%></font></td>
</tr>
<tr>
<td height="14" width="123" align="left">密码:</td>
<td width="388" height="10" align="left"><font color="#524581">
<%=password%></font></td>
</tr>
<tr>
<td height="14" width="123" align="left">确认密码:</td>
<td width="388" height="10" align="left"><font color="#524581">
<%=re_password%></font></td>
</tr>
<tr>
<td height="14" width="123" align="left">性别:</td>
<td width="388" height="10" align="left"><font color="#524581">
<%=sex%></font></td>
</tr>
<tr>
<td height="14" width="123" align="left">E-mail:</td>
<td width="388" height="10" align="left"><font color="#524581">
<%=e_mail%></font></td>
</tr>
<tr>
<td height="14" width="123" align="left">主页:</td>
<td width="388" height="10" align="left"><font color="#524581">
<%=url%></font></td>
</tr>
</table>
<%
out.println("您注册成功了!");
rs.close();
}
%>
</body>
</html>
...全文
226 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
luckhk 2004-12-22
  • 打赏
  • 举报
回复
MyDbBean.java

package DataBase
.....
public void OpenConn(String *,String *,String *){}
public ResultSet executeQuery(String *){}
public void executeUpdate(String *){}

至少要有以上三个方法
gaoyu858 2004-12-22
  • 打赏
  • 举报
回复
这个和你需要的差不多,你拿过去修改一下。修改成功后告诉我哦。我的Email:gaoyu858@126.com 13349951345@hb165.com. 我要整个的代码(包括register.html register.jsp DataBase.MyDbBean)
谢谢 !!!


package DataBase;


import java.sql.*;
import java.util.*;
import java.math.*;

public class UserEntity
{
private String id;
private String userName;
private String userPassword;
.
.
.

public static String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
public static String sConnStr = "jdbc:odbc:cart";
public static Connection conn = null;


public String addUser()throws SQLException{

try {
Class.forName(sDBDriver);
}catch(java.lang.ClassNotFoundException e){
System.err.println("UserEntity: " + e.getMessage());
}
try{
conn = DriverManager.getConnection(sConnStr);
String sqlinsert="insert into user(user_name,user_password,user_groupID)values(?,?,?)";
//设置参数
//preStatement.setString(1,query);
//preStatement.setInt(2,(2+1));
//使用excuteUpdate方法执行带参数的sql命令
// preStatement.executeUpdate();

PreparedStatement prepStmt = conn.prepareStatement(sqlinsert);

//prepStmt.setString(1,this.id );
prepStmt.setString(1,userName);
prepStmt.setString(2,userPassword);
prepStmt.setInt(3,userGroupID);

prepStmt.executeUpdate();
//prepStmt.close();
//conn.close();
}catch(SQLException ex){
System.err.println("UserEntity-addUser executeQuery: " + ex.getMessage());
}
//取得插入用户主键值。
//get the id
//初始化查询字符串 slelectStatment
String selectStatement ="select id " +"from user where user_name=? order by id desc ";
//创建Preparestatement对象prepstmt.
PreparedStatement prepStmt = conn.prepareStatement(selectStatement);
//设置用户名。
prepStmt.setString(1,userName);
//执行查询,并将查询结果存入rs中。
ResultSet rs = prepStmt.executeQuery();
//取得第一条记录
rs.next();
//设置resutl为主键值,并返回。
String result=rs.getString(1);

return result;


}
gaoyu858 2004-12-22
  • 打赏
  • 举报
回复
用JavaBean 实现的连接池类DBConnPool.你可以看看。
如果修改成功后,请把你整个注册代码留给我。
我的Email:gaoyu858@126.com 13349951345@hb165.com.


package 文件夹;
import java.sql.*;
import java.util.*;

/*连接池类.能够根据要求创建新连接,直到最大连接数为止.*/
public class DBConnPool {
//实际使用中的连接数
private int inUse=0;
//空闲连接
private Vector connections = new Vector();
//连接池名
private String poolname;
//数据库标识
private String dbid;
//驱动程序名
private String drivername;
//数据库账号
private String username;
//数据库密码
private String passwd;
//最大连接数
private int maxconn;

public DBConnPool(String poolname, String drivername, String dbid, String username, String passwd, int maxconn) {
this.poolname = poolname;
this.dbid = dbid;
this.drivername = drivername;
this.username = username;
this.passwd = passwd;
this.maxconn = maxconn;
}

/*将连接返回给连接池*/
public synchronized void releaseConnection(Connection con) {
// 将指定连接加入到向量末尾
connections.addElement(con);
//连接数减一
inUse--;
}

/*从连接池得到一个连接*/
public synchronized Connection getConnection() {
Connection con = null;
if (connections.size() > 0) {
// 获取连接列表中获得第一个连接
con = (Connection) connections.elementAt(0);
connections.removeElementAt(0);
//如果此连接已关闭,则继续获取
try {
if (con.isClosed())
con = getConnection();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//如果实际使用的连接小于最大连接数,就新创建一个连接
else if (maxconn == 0 || inUse < maxconn) {
con = newConnection();
}
if (con != null) {
//连接数增一
inUse++;
}
//返回一个连接
return con;
}

/*创建新的连接*/
private Connection newConnection() {
Connection con = null;
try {
//加载驱动程序
Class.forName(drivername);
//建立连接
con = DriverManager.getConnection(dbid, username, passwd);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
//返回该连接
return con;
}

/*关闭所有连接*/
public synchronized void closeConn() {
Enumeration allConnections = connections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
connections.removeAllElements();
}
}
zxf_1 2004-12-22
  • 打赏
  • 举报
回复
顶一下
programeyonger 2004-12-22
  • 打赏
  • 举报
回复
对啊,能有Bean的代码!!
cm84n7281 2004-12-22
  • 打赏
  • 举报
回复
能不能说详细点最好有这个JAVABEAN的代码
这样说我不明白?
junyi2003 2004-12-22
  • 打赏
  • 举报
回复
...................
用mysql或者D版oracle/sql-server
cm84n7281 2004-12-22
  • 打赏
  • 举报
回复
还是不行呀

还是报错,请求高手的帮助!数据库应该用什么数据库呀
谢谢,急!
lancezhao 2004-12-21
  • 打赏
  • 举报
回复
MyDbBean建立这个BEAN,里面就是连接数据库然后更新数据库就可以了,记得package要跟那个JSP文件的一致
lancezhao 2004-12-21
  • 打赏
  • 举报
回复
少BEAN

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧