⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinfoaction.java~14~

📁 《struts 应用开发完全手册》中的源代码第一到第五章。
💻 JAVA~14~
字号:
package com.action;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import com.actionForm.UserInfoActionForm;
import com.dao.Chin;
import java.sql.*;

public class UserInfoAction
    extends Action {
  private final String dbDriver = "com.mysql.jdbc.Driver"; //连接sql数据库的方法
  private final String url =
      "jdbc:mysql://127.0.0.1:3306/db_mysql";
  private final String userName = "root";
  private final String password = "111";
  private Connection con = null;

  public UserInfoAction() {
    try {
      Class.forName(dbDriver).newInstance(); //加载数据库驱动
      con = DriverManager.getConnection(url, userName, password);
      con.setAutoCommit(true);
    }
    catch (Exception ex) {
      System.out.println("数据库加载失败");
    }
  }

//对数据库的增加、修改和删除的操作
  public boolean executeUpdate(UserInfoActionForm form) {
    String sql = "insert into tb_user01 values ('" + form.getName() + "','" +
        form.getAge() + "','" + form.getSex() + "','" + form.getProfession() +
        "')";
    System.out.println(sql);
    try {
      Statement stmt = con.createStatement();
      int iCount = stmt.executeUpdate(sql);
      System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
      return true;
    }
    catch (SQLException e) {
      return false;
    }

  }

  public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws
      SQLException {
    UserInfoActionForm userInfoActionForm = (UserInfoActionForm) form;
//    userInfoActionForm.setName(Chin.toChinese(userInfoActionForm.getName()));
//    userInfoActionForm.setAge(Chin.toChinese(userInfoActionForm.getAge()));
//    userInfoActionForm.setSex(Chin.toChinese(userInfoActionForm.getSex()));
//    userInfoActionForm.setProfession(Chin.toChinese(userInfoActionForm.
//        getProfession()));



    String messger = "添加用户失败!!!";
    if (executeUpdate(userInfoActionForm)) {
      messger = "添加用户成功!!!";
    }
    request.setAttribute("messger", messger);
    con.close();
    return mapping.findForward("insertUserInfo");
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -