我的JSP页面:<body>
<form action="Ser" method="post">
<table  border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#006000">
  <tr bgcolor="#F8FFF7">
    <td colspan="2" bgcolor="#F8FFF7"><div align="center"><font size="4" color="#1D5C92">
      <input name="button222" type="submit" value=" 修改 ">
&nbsp; </font> <font size="4" color="#1D5C92">
<input name="button24" type="button" value=" 返回 ">
</font></div>
 <tr bgcolor="#F6F6F6">
    <td width="78" nowrap bgcolor="#F8FFF7" class="td2">输入账号:</td>
    <td width="232" bgcolor="#F8FFF7"><input type="password" name="id"></td>
  </tr>
   <tr bgcolor="#F6F6F6">
    <td width="78" nowrap bgcolor="#F8FFF7" class="td2">输入原密码:</td>
    <td width="232" bgcolor="#F8FFF7"><input type="password" name="yuan"></td>
  </tr>
  <tr bgcolor="#F6F6F6">
    <td nowrap bgcolor="#F8FFF7" class="td2">输入新密码:</td>
    <td bgcolor="#F8FFF7"><input type="password" name="xin"></td>
  </tr>
  <tr bgcolor="#F6F6F6">
    <td nowrap bgcolor="#F8FFF7" class="td2">确认新密码:</td>
    <td bgcolor="#F8FFF7"><input type="password" name="que"></td>
  </tr>
  
  <tr>
        <td align="center"><input type="submit" name="xiugai" value="修改"/></td>
        <td align="center"><input type="button" name="fanhui" value="返回"/></td>
  </tr>
</table>
</form>
</body>
我的数据连接类:
public class DBunit {


 private Connection con;
   
  public Connection getConn()
  {   try{Class.forName("com.mysql.jdbc.Driver");
  }
  catch(ClassNotFoundException e){
  System.out.print("加载驱动器有错误");
  }
  try{con=DriverManager.getConnection("jdbc:mysql://localhost/hr","gxp","gxp");
    
  }
    catch(SQLException el){
    System.out.println("数据库连接有错误");
    } 
    
    
  return con;
  }
  public void setConn(Connection con){
   this.con = con;
  }
}
我的密码修改处理类:
public class Password {
public String getString(String   s) {
        if(s==null) 
        { s= "";} 
        return   s;}
public void pass(HttpServletRequest request)
   throws SQLException, IOException, ServletException {
// TODO Auto-generated method stub
String id=request.getParameter("id");
   id=getString(id);
    String yuan=request.getParameter("yuan");
   yuan=getString(yuan);
   String xin=request.getParameter("xin");
    xin=getString(xin);
      String que=request.getParameter("que");
        que=getString(que);
        
      DBunit db=new DBunit();
    db.getConn();
       
    Connection con=db.getConn();

  boolean bool=false;
  boolean mode=false;
  bool=(yuan.length()<=8)&&(xin.length()<=8)&&(que.length()<=8);
  if(bool==true)
  {
   Statement state=con.createStatement();
  String str="select password from pwd where id= "+ " ' "+id+ " ' ";
  ResultSet rs=state.executeQuery(str); 
  while(rs.next())
  { 
  String m=rs.getNString(2); 
  mode=(xin.equals(que))&&(yuan.equals(m));
  if(mode==true) 
  { 
   String str1= "update pwd set password= "+ " ' "+xin+ " ' where id= "+ " ' "+id+ " ' "; 
  state.executeUpdate(str1);
 
    }

  state.close();
  con.close();

  
  }
  }
}
}
我的servlet跳转类:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
// TODO Auto-generated method stub
String yuan=request.getParameter("yuan");
String id=request.getParameter("id");
DBunit dbs=new DBunit();
Connection con=dbs.getConn();
  
    Statement state = null;
try {
state = con.createStatement();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
  String str="select * from pwd where id= "+ " ' "+id+ " ' ";
  ResultSet rs = null;
try {
rs = state.executeQuery(str);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();

  try {
while(rs.next())
  {  
  String n=rs.getString(1); 
      String m=rs.getString(2);
     
if(id.equals(n)&&yuan.equals(m))
{  Password s=new Password();
try {
s.pass(request);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestDispatcher ss=request.getRequestDispatcher("22.jsp");
ss.forward(request, response);

}
else
{
response.sendRedirect("11.jsp");
}
  state.close();
  con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  }
附加数据库hr,表pwd中字段id,password
如何实现密码修改,请教各位大侠我的代码有什么错误,尤其是DBunit dbs=new DBunit();数据库的连接。

解决方案 »

  1.   

      测试一下数据库是否连接成功,数据库连接不能是return吧,改为void一个函数,输出con,测试是否连接成功。我的数据库是oracle的,所以没法测试你的mySQL一步一步的测试看看。逻辑有没有错误。参数的传递好像有点别扭,再看看吧,记住一步一步的测试代码,错误就会出来,靠别人来查最后还是自己糊里糊涂的很快忘记
      

  2.   

    回答错误,数据库连接你写的是对的,不错是否连接成功,需要测试一下输出con的值
      

  3.   

    con穿出来的值是NULL怎么改啊?我不会改了
      

  4.   

    public class DBunit {
    private Connection con;//-----------你把这个Connection 定义放到getConn()方法里去试试
      
      public Connection getConn()
      { try{Class.forName("com.mysql.jdbc.Driver");
    }
    catch(ClassNotFoundException e){
    System.out.print("加载驱动器有错误");
    }
    try{con=DriverManager.getConnection("jdbc:mysql://localhost/hr","gxp","gxp");
      
      }
      catch(SQLException el){
      System.out.println("数据库连接有错误");
      }  
        
        
      return con;
      }
      

  5.   

    DBunit db=new DBunit();
    db.getConn();
    从工具类里面得到conn还要new 对象啊, 你直接把
    public Connection getConn()改成 public static Connection getConn()的,取conn时直接用Connection conn=DBunit.getConn();
      

  6.   

    还是有错误
    HTTPステータス 500 - --------------------------------------------------------------------------------type 例外レポートメッセージ 説明 The server encountered an internal error () that prevented it from fulfilling this request.例外 java.lang.NullPointerException
    ss.Ser.doPost(Ser.java:74)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    注意 原因のすべてのスタックトレースは、Apache Tomcat/7.0.23のログに記録されています
      

  7.   

    java.lang.NullPointerException
    ss.Ser.doPost(Ser.java:74)Ser.java的74行出现空指针。楼主看是哪一行?
      

  8.   


      Connection con=DBunit.getConn();
        Statement state = null;
      

  9.   

    state = con.createStatement();
      

  10.   

    我这没有环境没法运行你的代码,这有个我以前写的 你看看 也是mysql
    package utils;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;public class DBUtil {
    static{
    try {
    Class.forName("org.gjt.mm.mysql.Driver");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw new RuntimeException("加载驱动失败",e);
    }
    }
    public static Connection getConnection() throws SQLException{
    Connection conn=null;
    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/xyw","root","root");
    return conn;
    }
    public static void close(Connection conn){
    if(conn!=null){
    try {
    conn.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }}
      

  11.   


    DBunit db=new DBunit();你这里new的一个默认的连接对象,没有给它设置连接属性吧,比如url,name,password。
      

  12.   

    我没用过DBunit ,你要看它的文档。
    一般是它的构造方法里提供你的输入:DBunit db=new DBunit("ip加端口","用户名如sa","密码")
    也可能是
    DBunit db=new DBunit();
    db.setURL("ip加端口");
    db.setUser("sa");
    db.serPassword("");又或是加载配置文件,比如
    DBunit db = DBunit.config("xxx/xx/db.xml");具体要看你这个api文档。
      

  13.   

    简单地帮你查了一下,是不是用这句:
    Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:testingdb", "sa", "");链接:
    http://www.ltesting.net/ceshi/open/kydycsgj/dbunit/2007/0525/5431.html
      

  14.   

    首先得排除是不是数据库连接有问题下面给你一个我测试成功的数据库连接的方法
    public static Connection getConnection() {
    Connection conn = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    String url = DatabaseParamFactory.getURL();
    String userName = DatabaseParamFactory.getUserName();
    String password = DatabaseParamFactory.getPassword();
    conn = DriverManager.getConnection ("jdbc:mysql://192.168.10.2:3306/dhetransport_new", "root", "root");
    } catch (Exception e) {
    e.printStackTrace();
    return conn;
    }
    return conn;
    }
      

  15.   

    在Ser.java里直接调用DBunit.java,调用时候有问题,不知道怎么改,数据库连接应该没问题吧,我查过
      

  16.   

    con为空,果断不用看别的,连接数据库有问题
      

  17.   

    采用上面大侠给的数据库连接方法还是出错,是不是Ser.java的错啊?
      

  18.   

    哦,不好意思,我没仔细看你代码。其实你有连接的,只不过驱动加载有问题。
    Class.forName("com.mysql.jdbc.Driver");这一句加载到static里面:static {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
        }
      

  19.   

    我试过,但是还是提示错误,麻烦你再帮我看下其他两个Java文件的代码吧,主要实现密码修改功能