Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 servlet调用bean无法更新信息(无错误提示)
Adolfmark





发贴: 7
积分: 1
于 2009-02-02 16:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
servlet调用bean 更改用户信息时无法更新用户信息 ,服务器没有提示错误,就是更新不了用户信息,不知道是什么原因 ,javabean代码如下:
package com.myblog.Model;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.myblog.User.*;
import com.myblog.Dao.*;
import com.myblog.Dao.DBUtils;
public class OperationUser {
  
  public User demo(){
    /**
     * Goback User
     *
     * */
    Connection conn = null;   //每次连接数据库都需要一个Connection
    Statement stmt = null;
    ResultSet rs = null;
    String sql = null;
    DBUtils db = new DBUtils();
    try{
      conn = db.getConnection();
      conn.setAutoCommit(false);
      
      stmt = conn.createStatement();
      sql="select max(Blog_User_id) as uid from Blog_User";
      rs = stmt.executeQuery(sql);
      User user = new User();
      while(rs.next()){
        user.setUser_id(rs.getInt("uid"));     //获取列值
      }
      conn.commit();
      return user;
    }catch(SQLException e){
      try{
        conn.rollback();
      }catch(SQLException e1){
        e1.printStackTrace();
      }
      e.printStackTrace();
    }finally{
      db.closeAll(rs, stmt, conn);
    }
    return null;
  }

  
  public int insertOrupdate(User user,String saveOrupdate){
    DBUtils db = new DBUtils();
    Connection conn = null;
    PreparedStatement pstmt = null;
    try{
      conn = db.getConnection();
      //conn = DBUtils.getConnection();
      String sql="";
      if("save".equals(saveOrupdate)){
        //sql="insert into Blog_User";
      }else if("update".equals(saveOrupdate)){
        sql = "update Blog_User set Blog_User_sex=?,Blog_User_qq=?,Blog_User_msn=?,Blog_User_phone=?where Blog_User_id=?";
        System.out.println(sql);
      }
      conn.setAutoCommit(false);
      pstmt = conn.prepareStatement(sql);
      pstmt.setInt(0, user.getUser_sex());
      pstmt.setString(1, user.getUser_qq() );
      pstmt.setString(2, user.getUser_msn());
      pstmt.setString(3, user.getUser_phone());
      pstmt.setInt(4, user.getUser_id());
      
      int result = pstmt.executeUpdate();
      commit(conn);
      return result;
    }catch(SQLException e){
      rollback(conn);
    }finally{
    
      }
    return 0; //if result !=0; insert or update success;
  }
  
  public void commit(Connection conn){
    try{
      conn.commit();
    }catch(SQLException e){
      e.printStackTrace();
    }
  }
  
  public void rollback(Connection conn){
    try{
      conn.rollback();
    }catch(SQLException e){
      e.printStackTrace();
    }
  }
}

调用bean的servlet代码如下
package com.myblog.Model;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.myblog.User.*;

public class addOrmodify extends HttpServlet{
  public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
    response.setContentType("text ml;charset=gb2312");
    request.setCharacterEncoding("gb2312");
    /**
     * request information
     *
     * */
String user_sex2 = request.getParameter("user_sex");
String user_qq = request.getParameter("user_qq");
String user_msn = request.getParameter("user_msn");
String user_phone = request.getParameter("user_phone");
   String isSave = request.getParameter("option");
int user_sex = 0;
if(user_sex2 != null && user_sex2.matches("\\d+"))
user_sex = Integer.parseInt(user_sex2);
    
//System.out.println(isSave);
    
    OperationUser ou = new OperationUser();
    /**
     * @isSave
     * Judge insert or update
     * */
    if("save".equals(isSave)){
      User user = new User();
      int result = ou.insertOrupdate(user,"save");
      if(result>0){
        
        System.out.println("输入成功");
      }

    }else if("update".equals(isSave)){
      User user2 = ou.demo();      // select from table by id;
      
      user2.setUser_sex(user_sex);
      user2.setUser_qq(user_qq);
      user2.setUser_msn(user_msn);
      user2.setUser_phone(user_phone);

      int resultnum = ou.insertOrupdate(user2,"update");
      System.out.println(user2.getUser_id());
      if(resultnum >0){
        System.out.println("修改成功");
      }
      
    }
    
  
  }
  public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
  {
    doGet(request,response);
  }
  
  public int converParameter(String key){
    return key != null && key.matches("\\d+")?Integer.valueOf(key):0;
  }
}




[转帖]面向对象的思维方法

话题树型展开
人气 标题 作者 字数 发贴时间
9218 servlet调用bean无法更新信息(无错误提示) Adolfmark 4349 2009-02-02 16:49
7617 Re:servlet调用bean无法更新信息(无错误提示) JiafanZhou 95 2009-02-04 20:41

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923