运行环境:tomcat6+sqlsever2000register.jsp如下:
<!--register.jsp-->
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="user.regBean" %>
<%!
public String codeString(String s){
//显示中文
String str=s;
try{
byte b[]=str.getBytes("ISO-8859-1");
str=new String (b);
return str;
}catch(Exception e){return str;}
}
%>
<html>
<head>
<title>Reg</title>
</head>
<body bgcolor=#C0C0C0>
<center>
<%
String name,pwd,pwdr,sex,mail,success;
//get input
name=request.getParameter("name");
if(name==null)name="";
name=codeString(name);
pwd=request.getParameter("pwd");
if(pwd==null)pwd="";
pwd=codeString(pwd);
pwdr=request.getParameter("pwdr");
if(pwdr==null)pwdr="";
pwdr=codeString(pwdr);
sex=request.getParameter("sex");
if(sex==null)sex="";
sex=codeString(sex);
mail=request.getParameter("mail");
if(mail==null)mail="";
mail=codeString(mail);
//check input
if(
name.equals("")||
pwd.equals("")||
pwdr.equals("")||
mail.equals("")
){
out.println("<center><font color=#FF0000>输入不能为空!</font><br><a href='zhuce.jsp'>返回 </a></center>");
}
else if(!pwd.equals(pwdr)){
out.println("<center><font color=#FF0000>两次密码不同!</font><br><a href='zhuce.jsp'>返回 </a></center>");
}
else{
%>
<jsp:useBean id="reg" class="user.regBean" scope="page"/>
<jsp:setProperty name="reg" property="name" value="<%=name%>"/>
<jsp:setProperty name="reg" property="pwd" value="<%=pwd%>"/>
<jsp:setProperty name="reg" property="sex" value="<%=sex%>"/>
<jsp:setProperty name="reg" property="mail" value="<%=mail%>"/>
<%
success=reg.getSuccess();
if(!success.equals("true")){
out.print(success+"<center><font color=#FF0000>这个用户名已经有人用了!</font><br><a href='zhuce.jsp'>返回 </a></center>");
}
else{
out.print("<center><font color=#FF0000>恭喜你完成注册,请重新登录!</font><br><a href='index.jsp'>登录 </a></center>");
}
}
%>
</center>
</body>
</html>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
regBean.java 如下:
package user;
import java.sql.*;
public class regBean{
String id="";
String name="";
String pwd="";
String sex="";
String mail="";
String success="false";
Connection conn;
Statement sql;
ResultSet rs;
public regBean(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(ClassNotFoundException e){}
}
public void setId(String s){id=s;}
public void setName(String s){name=s;}
public void setPwd(String s){pwd=s;}
public void setSex(String s){sex=s;}
public void setMail(String s){mail=s;}
public String getId(){return id;}
public String getPwd(){return pwd;}
public String getSex(){return sex;}
public String getMail(){return mail;}
public String getName(){return name;}
public String getSuccess(){
if(name.equals("")){
success="false";
return success;
}else{
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ping","sa","123");
sql=conn.createStatement();
String condition="select*from user where name='"+name+"'";
rs=sql.executeQuery(condition);
if(rs.next()){
success="false";
conn.close();
return success;
}
else{
String insert="insert into user values(?,?,?,?,?);";
PreparedStatement pstmt=conn.prepareStatement(insert);
pstmt.setString(1,id);
pstmt.setString(2,name);
pstmt.setString(3,pwd);
pstmt.setString(4,sex);
pstmt.setString(5,mail);
pstmt.executeUpdate();
pstmt.close();
success="true";
conn.close();
return success;
}
}catch(SQLException e){
success="false";
return success;
}
}
}
}
找了好久都找不出怎样错了,请高手指点下呀,感激不尽呀
<!--register.jsp-->
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="user.regBean" %>
<%!
public String codeString(String s){
//显示中文
String str=s;
try{
byte b[]=str.getBytes("ISO-8859-1");
str=new String (b);
return str;
}catch(Exception e){return str;}
}
%>
<html>
<head>
<title>Reg</title>
</head>
<body bgcolor=#C0C0C0>
<center>
<%
String name,pwd,pwdr,sex,mail,success;
//get input
name=request.getParameter("name");
if(name==null)name="";
name=codeString(name);
pwd=request.getParameter("pwd");
if(pwd==null)pwd="";
pwd=codeString(pwd);
pwdr=request.getParameter("pwdr");
if(pwdr==null)pwdr="";
pwdr=codeString(pwdr);
sex=request.getParameter("sex");
if(sex==null)sex="";
sex=codeString(sex);
mail=request.getParameter("mail");
if(mail==null)mail="";
mail=codeString(mail);
//check input
if(
name.equals("")||
pwd.equals("")||
pwdr.equals("")||
mail.equals("")
){
out.println("<center><font color=#FF0000>输入不能为空!</font><br><a href='zhuce.jsp'>返回 </a></center>");
}
else if(!pwd.equals(pwdr)){
out.println("<center><font color=#FF0000>两次密码不同!</font><br><a href='zhuce.jsp'>返回 </a></center>");
}
else{
%>
<jsp:useBean id="reg" class="user.regBean" scope="page"/>
<jsp:setProperty name="reg" property="name" value="<%=name%>"/>
<jsp:setProperty name="reg" property="pwd" value="<%=pwd%>"/>
<jsp:setProperty name="reg" property="sex" value="<%=sex%>"/>
<jsp:setProperty name="reg" property="mail" value="<%=mail%>"/>
<%
success=reg.getSuccess();
if(!success.equals("true")){
out.print(success+"<center><font color=#FF0000>这个用户名已经有人用了!</font><br><a href='zhuce.jsp'>返回 </a></center>");
}
else{
out.print("<center><font color=#FF0000>恭喜你完成注册,请重新登录!</font><br><a href='index.jsp'>登录 </a></center>");
}
}
%>
</center>
</body>
</html>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
regBean.java 如下:
package user;
import java.sql.*;
public class regBean{
String id="";
String name="";
String pwd="";
String sex="";
String mail="";
String success="false";
Connection conn;
Statement sql;
ResultSet rs;
public regBean(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}catch(ClassNotFoundException e){}
}
public void setId(String s){id=s;}
public void setName(String s){name=s;}
public void setPwd(String s){pwd=s;}
public void setSex(String s){sex=s;}
public void setMail(String s){mail=s;}
public String getId(){return id;}
public String getPwd(){return pwd;}
public String getSex(){return sex;}
public String getMail(){return mail;}
public String getName(){return name;}
public String getSuccess(){
if(name.equals("")){
success="false";
return success;
}else{
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ping","sa","123");
sql=conn.createStatement();
String condition="select*from user where name='"+name+"'";
rs=sql.executeQuery(condition);
if(rs.next()){
success="false";
conn.close();
return success;
}
else{
String insert="insert into user values(?,?,?,?,?);";
PreparedStatement pstmt=conn.prepareStatement(insert);
pstmt.setString(1,id);
pstmt.setString(2,name);
pstmt.setString(3,pwd);
pstmt.setString(4,sex);
pstmt.setString(5,mail);
pstmt.executeUpdate();
pstmt.close();
success="true";
conn.close();
return success;
}
}catch(SQLException e){
success="false";
return success;
}
}
}
}
找了好久都找不出怎样错了,请高手指点下呀,感激不尽呀
success="false";
conn.close();
return success;
}
else{
String insert="insert into user values(?,?,?,?,?);";
PreparedStatement pstmt=conn.prepareStatement(insert);
pstmt.setString(1,id);
pstmt.setString(2,name);
pstmt.setString(3,pwd);
pstmt.setString(4,sex);
pstmt.setString(5,mail);
pstmt.executeUpdate();
pstmt.close();
success="true";
conn.close();
return success;
}
}catch(SQLException e){
success="false";
return success;
} 不知道你 这段代码想表示什么意思~?
select和*和from之间都没有空格,难道就不会报sql异常吗?
例如:
Java code:
在Dao.java:
public void createNewUser(String name, String password)
throws ClassNotFoundException, SQLException {
String sql = "insert into user(username,password) values(?,?)";
db = new Db();
// 打开数据库····1
con = db.Connect(); // 2 操作数据库 try {
// ````2.1找到操作SQL语句的对象
ps = con.prepareStatement(sql); // 2.2编辑SQL语句
ps.setString(1, name);
ps.setString(2, password); // 2.3.执行SQL语句
ps.executeUpdate(); } catch (SQLException e) {
e.printStackTrace();
} finally {
try {
// 关闭数据库
if (con != null) {
con.close();
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在Servlet RegisterUser.java
String username = request.getParameter("username");
String password = request.getParameter("password");
Dao dao = new Dao(); try {
dao.createNewUser(username, password);
request.getRequestDispatcher("login.jsp").forward(request, response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}这些是部分代码,你参考下。参数是从Jsp页面获取的。
String insert="insert into user values(?,?,?,?,?);";
PreparedStatement pstmt=conn.prepareStatement(insert);
pstmt.setString(1,id);
pstmt.setString(2,name);
pstmt.setString(3,pwd);
pstmt.setString(4,sex);
pstmt.setString(5,mail);
pstmt.executeUpdate();
pstmt.close();
success="true";
conn.close();
return success;
} 上面的 String insert="insert into user values(?,?,?,?,?);"; 有点问题。
执行 insert 语句异常 返回 false;
而且Catch里return的也是false,用户名已经有人使用