我的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=" 修改 ">
</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();数据库的连接。
<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=" 修改 ">
</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();数据库的连接。
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;
}
db.getConn();
从工具类里面得到conn还要new 对象啊, 你直接把
public Connection getConn()改成 public static Connection getConn()的,取conn时直接用Connection conn=DBunit.getConn();
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のログに記録されています
ss.Ser.doPost(Ser.java:74)Ser.java的74行出现空指针。楼主看是哪一行?
Connection con=DBunit.getConn();
Statement state = null;
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();
}
}
}}
DBunit db=new DBunit();你这里new的一个默认的连接对象,没有给它设置连接属性吧,比如url,name,password。
一般是它的构造方法里提供你的输入: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文档。
Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:testingdb", "sa", "");链接:
http://www.ltesting.net/ceshi/open/kydycsgj/dbunit/2007/0525/5431.html
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;
}
Class.forName("com.mysql.jdbc.Driver");这一句加载到static里面:static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}