登录注册网页中注册部分
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%><%@ page import="javax.swing.*"%>
<html>
<body>
<% String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPwd="123";
String dbName="login";
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(driverName);
Connection conn=DriverManager.getConnection(url);
String sql="insert into logininfo(userName,userPwd) values(?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
%>
<script language="javascript">
function reg()
{
document.register.submit();
}
</script>
<script language="javascript">
function registersucceed()
{
alert("恭喜!注册成功!!");
window.location.href="login.jsp";
}
</script>
<script language="javascript">
function registerfail()
{
alert("Sorry!已有该用户名!!");
window.location.href="register.jsp";
}
</script>
<form name="register" method ="post">
用户:<input type="text" name="name"><br>
密码:<input type="password" name="pwd"><br>
<input type="button" value="注册" onclick="reg();">
<%
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
pstmt.setString(1,name);
pstmt.setString(2,pwd);
String sql1="select * from logininfo where userName='"+name+"' ";
PreparedStatement pstmt1=conn.prepareStatement(sql1);
ResultSet rs=pstmt1.executeQuery();
if(rs.next())
{
out.print("<script >registerfail();</script>");
}
else if(name!=null)
{
out.print("<script >registersucceed();</script>");
pstmt.executeUpdate();
}
if (pstmt!=null)
{
pstmt.close();
}
if (conn!=null)
{
conn.close();
}
%>
</form>
</body>
</html>