首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

Java web求解,解决办法

2012-04-11 
Java web求解,急急急!!!!!!我的JSP页面:bodyform actionSer methodposttableborder0 align

Java web求解,急急急!!!!!!
我的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();数据库的连接。

[解决办法]
测试一下数据库是否连接成功,数据库连接不能是return吧,改为void一个函数,输出con,测试是否连接成功。我的数据库是oracle的,所以没法测试你的mySQL一步一步的测试看看。逻辑有没有错误。参数的传递好像有点别扭,再看看吧,记住一步一步的测试代码,错误就会出来,靠别人来查最后还是自己糊里糊涂的很快忘记
[解决办法]
回答错误,数据库连接你写的是对的,不错是否连接成功,需要测试一下输出con的值
[解决办法]
代码太长,你直接描述一下你现在出什么错了,在哪里出的错。
[解决办法]
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;
}

------解决方案--------------------


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

Connection conn=DBunit.getConn();

[解决办法]
让异常显示出来容易知道错误在哪
[解决办法]
java.lang.NullPointerException
ss.Ser.doPost(Ser.java:74)

Ser.java的74行出现空指针。楼主看是哪一行?
[解决办法]
我这没有环境没法运行你的代码,这有个我以前写的 你看看 也是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();
}
}
}

}

热点排行