這個是注冊插入數據代碼,在網頁中可以運行成功,說明我的數據庫連接是沒問題的
<%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>
分析了很久,不知道出現了什麼問題,希望大家可以幫幫我
把判斷方法關於result部分改寫成如下部分就可以運行了,謝謝大家了:
public static int runselectsql(String uid){
Connection con=null;
PreparedStatement ps=null;
ResultSet res=null;
try{
con=getConn();
String sql ="select count(*) from tb_vip where userid=?";
ps=con.prepareStatement(sql);
ps.setString(1,uid);
res=ps.executeQuery();
while(res.next())
{
x=res.getInt(1);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
res.close();
ps.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
return x;
}
jsp代碼改寫成這樣
<%
String uid=request.getParameter("uid");
String pwd=request.getParameter("pwd");
if(DBHelper.runselectsql(uid)==0){
Object[]params={uid,pwd};
String sql="insert into tb_vip values(?,?)";
if(DBHelper.runupdatesql(sql, params)){
out.print("已經成功注冊");
}
}
else{
out.print("注冊失敗,用戶名已存在");
}
%>