import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Process extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();

String id = request.getParameter("id");
String password = request.getParameter("password");
String name = request.getParameter("name");
String tel = request.getParameter("tel");
String email = request.getParameter("email");

String str = new String("");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=information";

Connection conn = null;
Statement stat = null;
ResultSet rs = null;
int x = 0;
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn = DriverManager.getConnection(url,"root","123456");
stat = conn.createStatement();
rs = stat.executeQuery("select * from information");
x = 0;
while(rs.next())
{
str = rs.getString("id");
if(x!=1&&(str.equals(id)))
{
out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
out.println("<head>");
out.println("<meta http-equiv='Content-Type' content='text/html; charset='utf-8' />");
out.println("<title>无标题文档</title>");
out.println("</head>");
out.println("<body>");
out.println("用户名存在,请重新填写");
out.println("<form action='index.html'><input type=submit value='返回' ></form>");
out.println("</body>");
out.println("</html>");
x = 1; 
out.println("heihei");
}
}
if(x==0)
{
stat.executeUpdate("insert into information values('"+id+"','"+password+"','"+name+"','"
+tel+"','"+email+"')");
}
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}catch(SQLException e)
{
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try
{
stat.close();
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
out.println("welcome to here");
}
}连接mysql当id重复是能显示返回界面
但是当连接的是sqlserver的时候就不显了 就算id重复也插入数据库
为什么  当时sqlserver的时候str.equals(id)不起作用
高手指点一下

解决方案 »

  1.   

     
    if(!rs.next())         这样试试!
    楼主的这个方法不好!
     if(!rs1.next()){
             String sql = "insert into t_user values  (xxxxxxx)";
             int rs = db.executeUpdate(con,sql);
             //response.sendRedirect("index.html");
             out.println("<script>alert('注册成功!');</script>  <script>location.href='login_s.jsp'</script>");
             System.out.println(rs);
         }
         out.println("<script>alert('用户名已经存在…… 请重新注册!');</script>  <script>location.href='rege.jsp'</script>");
             }
      

  2.   

    这个运行时候没有错误 就是不出来结果
    同样的代码把驱动之类的改一改连mysql又能达到效果
    连sqlserver就不行  能够插入
    当输入重复的他则又插入了  (想让他弹出个返回按钮,告诉用户重复了)
      

  3.   

    在有mysql向sqlserver加载驱动的时间好好的改下!
      

  4.   

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    这个应该这样写吧!
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
    还有你的驱动是Sqlserver2000的吧
      

  5.   

    if(x!=1&&(str.equals(id)))  把条件改下改成if(x!=1||str.equals(id))