这个是注册插入数据代码,在网页中可以运行成功,说明我的数据库连接是没问题的
<%String uid=request.getParameter("uid");
String pwd=request.getParameter("pwd");
Object[]paramss={uid,pwd};
String sqll="insert into tb_vip values(?,?)";
if(DBHelper.runupdatesql(sqll, paramss))
{
out.print("已经成功注册");
}
else
out.print("服务器问题请与客服联系");
%>
下面这段是判断数据库中用户名是否存在,不存在才可以用上面代码注册,但是运行后不管数据库中是否存在用户名,网页中只打印“注册失败,用户名不存在”
<%
String uid=request.getParameter("uid");
String pwd=request.getParameter("pwd");
Object[] params={uid};
String sql ="select count(*)from tb_vip where userid=?";
Result result=DBHelper.runselectsql(sql, params);
if(result.getRowCount()==0){
Object[]paramss={uid,pwd};
String sqll="insert into tb_vip values(?,?)";
if(DBHelper.runupdatesql(sqll, paramss)){
out.print("已经成功注册");
}else{out.print("服务器问题请与客服联系");}}
else{
out.print("注册失败,用户名已存在");
}
%>
下面是java数据库处理代码部分
package jspex;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
public class DBHelper {
private static final String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url = "jdbc:sqlserver://localhost:1433;databasename=studentm";
private static final String uname = "sa";
private static final String upass = "c111111";
public static Connection getConn(){
Connection conn = null;
try{
Class.forName(className);
conn = DriverManager.getConnection(url,uname, upass);
} catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void closeConn(Connection conn){
try{
if(conn!=null){
conn.close();
}
} catch(Exception e){
e.printStackTrace();
}
}
public static void closePstmt(PreparedStatement pstmt){
try{
if(pstmt!=null){
pstmt.close();
}
} catch(Exception e){
e.printStackTrace();
}
}
public static void closeRs(ResultSet rs){
try{
if(rs!=null){
rs.close();
}
} catch(Exception e){
e.printStackTrace();
}
}
public static boolean runupdatesql(String sql,Object[]params){
Connection con=null;
PreparedStatement ps=null;
try{
con=getConn();
ps=con.prepareStatement(sql);
for(int i=0;i<params.length;i++){
ps.setObject(i+1, params[i]);
}
ps.executeUpdate();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
finally{
try{
ps.close();
con.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
public static Result runselectsql(String sql,Object[]params){
Connection con=null;
PreparedStatement ps=null;
ResultSet res=null;
Result result=null;
try{
con=getConn();
ps=con.prepareStatement(sql);
for(int i=0;i<params.length;i++){
ps.setObject(i+1, params[i]);
}
res=ps.executeQuery();
result=ResultSupport.toResult(res);
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
res.close();
ps.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
return result;
}
}
下面是注册页面,我为了好找错误,还把相关js功能注释掉了
<html>
<head>
<title>reg</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#loginform{
width:300px;
height:auto;
}
label{
width:60px;
height:30px;
text-align:left;
font-size:14px;
font-weight:hold;
vertical-align:bottom;
}
.inpute{
width:160px;
height:20px;
}
.row{
width:300px;
height:30px;
}
</style>
<script type="text/javascript">
function checkform(){
var name=document.getElementById("uid");
var pass=document.getElementById("pwd");
/*var rpass=document.getElementById("rpwd");*/
if(name.value.length<=0){
alert("用户名不能为空");
name.focus();
return false;
}
if(pass.value.length>=8){
alert("密码长度不对");
pass.focus();
return false;
}
/**if(pass!=rpass){
alert("密码不一致");
return false;
}**/
return true;
}
</script>
</head>
<body>
<div id="loginform">
<form onsubmit="return checkform();"action="doreg.jsp" method="post">
<div class="row">
<label for="uid">用户名: </label>
<input name="uid" type="text" class="inpute" id="uid">
</div>
<div class="row">
<label for="pwd">密码: </label>
<input name="pwd" type="password" class="inpute" id="pwd">
</div>
</div>
<!-- <div class="row">
<label for="rpwd">确认密码:</label>
<input name="rpwd" type="password" class="inpute" id="rpwd">
</div> -->
<div class="row">
<input type="submit" value="注册"/><input type="reset" value="重新填写"/>
</div>
</form>
</div>
</body>
</html>
分析了很久,不知道出现了什么问题,希望大家可以帮帮我