package JavaBean;
public class TransferManager {
DBAction db=new DBAction();
private String sql="";
public TransferManager(){}

public boolean checkTransferUserID(String MyUserID)//检查账号是否存在
{
sql="select * from users where userID='"+MyUserID+"'";
        try{
   if(db.Query(sql)!=null){
   System.out.print("账号存在!");
   return true;  
}else
{
System.out.print("账号不存在!");
}
}catch(Exception e){
e.printStackTrace();
System.out.print("账号不存在!");
}
finally{
db.Close();
}
return false;
}
public boolean checkTransferOutUserID(String MyUserID,String password)//检查转出账号是否存在
{
sql="select * from users where userID='"+MyUserID+"' and password='"+password+"'";
try{
   if(db.Query(sql)!=null){
   System.out.print("转出账号存在!");
  return true;  
   }
}catch(Exception e){
System.out.print("转出账号密码不正确!");
e.printStackTrace();
}
finally{
db.Close();
}
return false;
}
public boolean checkTransferInUserID(String YouUserID)//检查转入账号是否存在
{
sql="select * from users where userID='"+YouUserID+"'";
try{
if(db.Query(sql)!=null){
System.out.print("转入账号存在!");
return true;
}
}catch(Exception e){
System.out.print("转入账号不存在!");
e.printStackTrace();
}finally{
db.Close();
}
return false;
}
public boolean checkTransferMoney(String MyUserID,double Money)//检查转出金额是否大于账户余额
{
sql="select * from users where userID='"+MyUserID+"' and assets >="+Money+"";
try{
if(db.Query(sql)!=null){
System.out.print("转入金额足够存在!");
return true;
}
}catch(Exception e){
System.out.print("转入金额不足存在!");
e.printStackTrace();
}finally{
db.Close();
}
return false;

}
public boolean writeLog(String tranID,String operation,double Origamount,double Payments,String tradetime){
sql="insert into transRecord values('"+tranID+"','"+operation+"',"+Origamount+","+Payments+",'"+tradetime+"')";
try{
if(db.Update(sql)==1)
{
System.out.print("写回日志文件!");
return true;
}
}catch(Exception e){
System.out.print("写回日志文件失败!");
e.printStackTrace();
}finally{
db.Close();
}
return false;
}
public boolean addMoney(String YouUserID,double Money){
sql="update users set assets=assets+"+Money+" where userID='"+YouUserID+"'";
try{
if(db.Update(sql)==1)
{
System.out.print("入账成功!");
return true;
}
}catch(Exception e){
System.out.print("入账失败!");
e.printStackTrace();
}finally{
db.Close();
}
return false;
}
public boolean subtractMoney(String MyUserID,double Money){
sql="update users set assets=assets-"+Money+" where userID+'"+MyUserID+"'";
try{
if(db.Update(sql)==1)
{
System.out.print("出账成功!");
return true;
}
}catch(Exception e){
System.out.print("出账失败!");
e.printStackTrace();
}finally{
db.Close();
}
return true;
}
public boolean resendMessage(boolean flag){
if(flag==true)
{
System.out.print("转账成功!");
return true;
}
System.out.print("转账失败!");
return false;
}
}我的servlet调用:
package Servlet;
import JavaBean.*;
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 Transfer extends HttpServlet {
private double Origamount;
    public Transfer(){
     super();
     System.out.print("实例化Servlet!");
    
    }
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
super.destroy(); 
System.out.print("销毁Servlet!");
// Just puts "destroy" string in log
// Put your code here
}
/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request,response);
System.out.print("正在处理Servlet!");
} /**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=gbk");
response.setCharacterEncoding("gbk");
request.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
try{
String MyUserID=request.getParameter("MyUserID");
String password=request.getParameter("password");
String YouUserID=request.getParameter("YouUserID");
String operation="转账";
String P=request.getParameter("Money");
double Payments=Double.parseDouble(P);
java.text.SimpleDateFormat date=new java.text.SimpleDateFormat("yyyy-MM-dd");
String tradetime=date.format( new java.util.Date());
String m=request.getParameter("Money");
double Money=Double.parseDouble(m);
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
DBAction db=new DBAction();
TransferManager tm=new TransferManager();
String sql="select assets from users where userID='"+MyUserID+"'";
ResultSet rs=db.Query(sql);
    if(rs.next())
    {
   String O;
   O = rs.getString("assets");
   Origamount=Double.parseDouble(O);
    }
try{
            if(tm.checkTransferInUserID(MyUserID)==false)
            {
             out.print("<script language='javascript'>alert('转出账号不存在!')</script>");
     response.setHeader("Refresh","0;URL=trade.jsp");  
     }
            else if(tm.checkTransferOutUserID(MyUserID, password)==false)
            {
             out.print("<script language='javascript'>alert('密码有误,请重新输入!')</script>");
     response.setHeader("Refresh","0;URL=trade.jsp");             
            }
            else if(tm.checkTransferInUserID(YouUserID)==false)
            {
             out.print("<script language='javascript'>alert('转入账号不存在,请查询输入!')</script>");
     response.setHeader("Refresh","0;URL=trade.jsp");             
            }
            else if(tm.checkTransferMoney(MyUserID, Money)==false)
            {
             out.print("<script language='javascript'>alert('您的余额不足,请查询输入!')</script>");
     response.setHeader("Refresh","0;URL=trade.jsp");
            }else 
            {
             String tranID = null;
             String sql1="select top 1 tranID  from transRecord order by tranID desc";
         ResultSet rs1=db.Query(sql1);
         if(rs.next())
         {
         String a=rs1.getString("tranID");
     double tranI=Double.parseDouble(a)+1;
     tranID=new Double(tranI).toString();
             if(tm.writeLog(tranID, operation, Origamount, Payments, tradetime)){ 
             try{
                 db.getCon().setAutoCommit(false);
                 tm.addMoney(YouUserID, Money);
                 tm.subtractMoney(MyUserID, Money);
                 db.getCon().commit();
                 operation="入账";
                 tm.writeLog(tranID, operation, Origamount, Payments, tradetime);
             }catch(Exception e){
             db.getCon().rollback();
             }
                }                       
         }
         sql="select top 1 operation from transRecord order by tranID desc ";
         if(rs.next()){
         operation=rs.getString("operation");
         }
             if(operation.equals("入账"))
             {
             if(tm.resendMessage(true))
             {
             out.print("<html><body>");
             out.print("转账成功!三秒后自动跳转到首页!");
             out.print("</body></html>");
             response.setHeader("Refresh","3;URL=Userindex.jsp");
             }
            
             }else{
             out.print("<script language='javascript'>alert('由于网络原因,转账失敗,请稍后再试!')</script>");
         response.setHeader("Refresh","0;URL=trade.jsp"); 
             }
            
            
            }
            
}catch(Exception e){
try
            {
                db.getCon().rollback();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
                
            }
            e.printStackTrace();    
            return; }finally{
db.Close();
}
out.flush();
out.close();
}catch(Exception e){
e.printStackTrace();

}
System.out.print("正在处理Servlet!");
} /**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
super.init();
System.out.print("初始化Servlet!");
// Put your code here
}}
为什么我无论输入什么他都正确的啊!(除非有空字符串,但我在jsp页面中由函数限制了)
然后我随便输入,就有:
弹出对话框:由于网络原因,转账失败,请稍后再试!
然后:
转入账号存在!转出账号存在!转入账号存在!转入金额足够存在!正在处理Servlet!
这是怎么回事?请大家帮忙看看~

解决方案 »

  1.   

    太多了,眼睛都看花了,debug下吧!
      

  2.   

    debug了,全都是写看不懂得东西……语法是没错的,但好像逻辑过了,怎么都调用不到正确的结果……
      

  3.   

    你们看一个就好了:
    public boolean checkTransferUserID(String MyUserID)//检查账号是否存在
    {
    sql="select * from users where userID='"+MyUserID+"'";
      try{
    if(db.Query(sql)!=null){
    System.out.print("账号存在!");
    return true;   
    }else
    {
    System.out.print("账号不存在!");
    }
    }catch(Exception e){
    e.printStackTrace();
    System.out.print("账号不存在!");
    }
    finally{
    db.Close();
    }
    return false; 
    }
    这个函数,和
     if(tm.checkTransferInUserID(MyUserID)==false)
      {
      out.print("<script language='javascript'>alert('转出账号不存在!')</script>");
      response.setHeader("Refresh","0;URL=trade.jsp");   
      }
      就这两个,都无法正确运行……
    刚学的,不太懂啊……
      

  4.   

    单步调试:
    Source not found for GenericObjectPool.borrowObject() line: 766
    Source not found for CursorableLinkedList.removeFirst() line: 571
    TOMCAT:
    2010-6-28 15:22:24 org.apache.catalina.core.AprLifecycleListener init
    信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\MyEclipse 7.0M1\bin;C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin
    2010-6-28 15:22:24 org.apache.coyote.http11.Http11Protocol init
    信息: Initializing Coyote HTTP/1.1 on http-8080
    2010-6-28 15:22:24 org.apache.catalina.startup.Catalina load
    信息: Initialization processed in 906 ms
    2010-6-28 15:22:24 org.apache.catalina.core.StandardService start
    信息: Starting service Catalina
    2010-6-28 15:22:24 org.apache.catalina.core.StandardEngine start
    信息: Starting Servlet Engine: Apache Tomcat/6.0.10
    2010-6-28 15:22:24 org.apache.catalina.startup.HostConfig deployWAR
    信息: Deploying web application archive 11.war
    2010-6-28 15:22:27 org.apache.coyote.http11.Http11Protocol start
    信息: Starting Coyote HTTP/1.1 on http-8080
    2010-6-28 15:22:27 org.apache.jk.common.ChannelSocket init
    信息: JK: ajp13 listening on /0.0.0.0:8009
    2010-6-28 15:22:27 org.apache.jk.server.JkMain start
    信息: Jk running ID=0 time=0/78  config=null
    2010-6-28 15:22:28 org.apache.catalina.startup.Catalina start
    信息: Server startup in 3516 ms
    实例化Servlet!初始化Servlet!转入账号存在!密码正确!转入账号存在!org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool exhausted
    at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:103)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
    at JavaBean.DBAction.getCon(DBAction.java:26)
    at JavaBean.DBAction.Query(DBAction.java:37)
    at JavaBean.TransferManager.checkTransferMoney(TransferManager.java:66)
    at Servlet.Transfer.doPost(Transfer.java:105)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
    at java.lang.Thread.run(Unknown Source)
    Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
    at org.apache.tomcat.dbcp.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:756)
    at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:95)
    ... 19 more
    转入金额足够!
    (我输入的全都是不对的数据,数据库没有的)
    天哪!我快抓狂了!
      

  5.   

     db.Query(sql)!=null 估计这个判断有问题 
      

  6.   

    if(tm.checkTransferInUserID(MyUserID)==false)
      {
      out.print("<script language='javascript'>alert('转出账号不存在!')</script>");
      response.setHeader("Refresh","0;URL=trade.jsp");   
      }
      else if(tm.checkTransferOutUserID(MyUserID, password)==false)
      {
      out.print("<script language='javascript'>alert('密码有误,请重新输入!')</script>");
      response.setHeader("Refresh","0;URL=trade.jsp"); 
      }
      else if(tm.checkTransferInUserID(YouUserID)==false)
      {
      out.print("<script language='javascript'>alert('转入账号不存在,请查询输入!')</script>");
      response.setHeader("Refresh","0;URL=trade.jsp"); 
      }
      else if(tm.checkTransferMoney(MyUserID, Money)==false)
      {
      out.print("<script language='javascript'>alert('您的余额不足,请查询输入!')</script>");
      response.setHeader("Refresh","0;URL=trade.jsp");
      }else  
      {
      String tranID = null;
      String sql1="select top 1 tranID from transRecord order by tranID desc";
      ResultSet rs1=db.Query(sql1);
      if(rs.next())
      {
      String a=rs1.getString("tranID");
      double tranI=Double.parseDouble(a)+1;
      tranID=new Double(tranI).toString();
      if(tm.writeLog(tranID, operation, Origamount, Payments, tradetime)){  
      try{
      db.getCon().setAutoCommit(false);
      tm.addMoney(YouUserID, Money);
      tm.subtractMoney(MyUserID, Money);
      db.getCon().commit();
      operation="入账";
      tm.writeLog(tranID, operation, Origamount, Payments, tradetime);
      }catch(Exception e){
      db.getCon().rollback();
      }
      } 
      }
      sql="select top 1 operation from transRecord order by tranID desc ";
      if(rs.next()){
      operation=rs.getString("operation");
      }
    把==改成.equals试下
      

  7.   

    org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool exhausted会不会是写JDBC连接时候 有问题 CLASS.FORNAME什么的
      

  8.   

    我将它分开了……还是错,都不晓得是为什么?……
    Servlet:
    package Servlet;
    import JavaBean.*;
    import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class ModifyAccount extends HttpServlet { /**
     * 
     */
    private static final long serialVersionUID = 1L; /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    } /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {  doPost(request,response);
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String userID=request.getParameter("MyUserID");
    String password=request.getParameter("password");
    String newpsw1=request.getParameter("newpassword1");
    String newpsw2=request.getParameter("newpassword2");
    ModifyAccountManager mam=new ModifyAccountManager();
    try{
    mam.getUserData(userID, password, newpsw1, newpsw2);
    if(mam.checkUserID(userID)==false){
    out.print("用户账号不存在!");
    }else{
    out.print("该账号存在!");
    }
    }catch(Exception e){
    e.toString();
    e.printStackTrace();
    }
    out.flush();
    out.close();
    } /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
    // Put your code here
    }}
    JaveBean:
    package JavaBean;
    import java.sql.*;
    public class ModifyAccountManager {
    DBAction db=new DBAction();
    ResultSet rs=null;
    String Authority="";
    String sql="";
    boolean flag;
    String userID="";
    String password="";
    String newpsw1="";
    String newpsw2="";
    public void getUserData(String userID,String password,String newpsw1,String newpsw2){
    this.userID=userID;
    this.password=password;
    this.newpsw1=newpsw1;
    this.newpsw2=newpsw2;
    }
    public boolean checkUserID(String userID){
    flag=false;
    sql="select * from users where userID='"+userID+"'";
    rs=db.Query(sql);
    if(rs==null){
    System.out.print("用户名不存在!");
    }else{
    flag=true;
    }
    return flag;
    }
    public boolean Modify(){
    flag=false;
    sql="select * from users where userID='"+userID+"'";
    rs=db.Query(sql);
    if(rs==null){
    System.out.print("用户名不存在!");
    }
    return flag;
    }}