<%
 request.setCharacterEncoding("gb2312");
  String username1=request.getParameter("username1");
  String password1=request.getParameter("password1");
  String sql=null;
    %>
    <%String url="jdbc:mysql://localhost:3306/javaweb"; 
      String user="root"; 
      String password="6949453"; 
      Connection conn=null;
       PreparedStatement pstmt=null;
       try{ 
           Class.forName("com.mysql.jdbc.Driver"); 
           conn=DriverManager.getConnection(url,user,password); 
           } 
           catch(ClassNotFoundException e) 
           {out.println("cannot find the driver!");} 
         catch(SQLException e) 
           {out.println("cannot connect the sql");} 
          
         try{
            sql="INSERT INTO user_table(userid,username,password) VALUES(null,?,?)";
            pstmt=conn.prepareStatement(sql);
             pstmt.setString(2,username1);
             pstmt.setString(3,password1);
             
             pstmt.executeUpdate();
             }
             
          catch(SQLException e){
             out.println("cannot add information");
            }
           try{
              if(pstmt !=null)
              {pstmt.close();
              pstmt=null;}
            
              if(conn !=null)
              {conn.close();
               conn=null;}
               }catch(Exception e){}
             %>
数据库连接都是正常的。在后面抛出异常:cannot add information
大家看看问题在哪里啊
           

解决方案 »

  1.   

    汗怎么写得这么奇怪INSERT INTO user_table(userid,username,password) VALUES(null,?,?)";pstmt.setString(2,username1);//
    pstmt.setString(3,password1);//你上面就2个问号,这里这个3哪来的还有你有MYSQL,userid这是你主键吧,MYSQL有自增的,你的INSERT语句怎么。null?再然后,你catch怎么不输出错误信息,输出看看就知道很多事了
      

  2.   

      try{
      sql="INSERT INTO user_table(userid,username,password) VALUES(null,?,?)";
      pstmt=conn.prepareStatement(sql);
      pstmt.setString(2,username1);
      pstmt.setString(3,password1);
        
      pstmt.executeUpdate();
      }
        
      catch(SQLException e){
      out.println("cannot add information");
      }  问题出在这里面. 你是MYSQL  id是自动增长的吧! 语句就不要带ID啊    INSERT INTO user_table(username,password) VALUES(?,?)   pstmt.setString(1,username1);
       pstmt.setString(2,password1);说明下(以后你用pstmt.set+xx的时候 标位从1开始啊,这个是代表从第一个问号开始的没有其他的意思.)   例外  pstmt.execute(); 这个好像是非更新吧  添加就是添加 又不是修改 删除
      

  3.   

    catch(SQLException e){
      out.println("cannot add information");
      }
    表示有异常生了.应该这样catch(SQLException e){
      out.println("cannot add information");
       e.printStackTrace();
      }