JAVA-myEclipse KEY

可直接使用


import java.io.*;
 
public class MyEclipseGen {
    private static final String LL = "Decompiling this copyrighted software is a violation of both your license agreement and the Digital Millenium Copyright Act of 1998 (http://www.loc.gov/copyright/legislation/dmca.pdf). Under section 1204 of the DMCA, penalties range up to a $500,000 fine or up to five years imprisonment for a first offense. Think about it; pay for a license, avoid prosecution, and feel better about yourself.";
    public String getSerial(String userId, String licenseNum) {
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.add(1, 3);
        cal.add(6, -1);
        java.text.NumberFormat nf = new java.text.DecimalFormat("000");
        licenseNum = nf.format(Integer.valueOf(licenseNum));
        String verTime = new StringBuilder("-").append(new java.text.
                SimpleDateFormat("yyMMdd").format(cal.getTime())).append("0").
                         toString();
        String type = "YE3MP-";
        String need = new StringBuilder(userId.substring(0, 1)).append(type).
                      append("300").append(licenseNum).append(verTime).toString();
        String dx = new StringBuilder(need).append(LL).append(userId).toString();
        int suf = this.decode(dx);
        String code = new StringBuilder(need).append(String.valueOf(suf)).
                      toString();
        return this.change(code);
    }
 
    private int decode(String s) {
        int i;
        char[] ac;
        int j;
        int k;
        i = 0;
        ac = s.toCharArray();
        j = 0;
        k = ac.length;
        while (j < k) {
            i = (31 * i) + ac[j];
            j++;
        }
        return Math.abs(i);
    }
 
    private String change(String s) {
        byte[] abyte0;
        char[] ac;
        int i;
        int k;
        int j;
        abyte0 = s.getBytes();
        ac = new char[s.length()];
        i = 0;
        k = abyte0.length;
        while (i < k) {
            j = abyte0[i];
            if ((j >= 48) && (j <= 57)) {
                j = (((j - 48) + 5) % 10) + 48;
            } else if ((j >= 65) && (j <= 90)) {
                j = (((j - 65) + 13) % 26) + 65;
            } else if ((j >= 97) && (j <= 122)) {
                j = (((j - 97) + 13) % 26) + 97;
            }
            ac[i] = (char) j;
            i++;
        }
        return String.valueOf(ac);
    }
 
    public MyEclipseGen() {
        super();
    }
 
    public static void main(String[] args) {
        try {
            System.out.println("please input register name:");
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    System.in));
            String userId = null;
            userId = reader.readLine();
            MyEclipseGen myeclipsegen = new MyEclipseGen();
            String res = myeclipsegen.getSerial(userId, "20");
            System.out.println("Serial:" + res);
            reader.readLine();
        } catch (IOException ex) {
        }
    }
}

 

--------------------- 
作者:sunsean1230 
来源:优快云 
原文:https://blog.youkuaiyun.com/sunxiaosunxiao/article/details/6685321 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

# 题目重述 在 MyEclipse 8.5 环境下创建名为 `bookmis` 的 Web 工程,使用 MySQL 5.1 数据库(驱动为 mysql-connector-java-5.1.0-bin.jar),实现以下功能: 1. 创建数据库 `bookdb`,包含 `userinfo` 和 `book` 表,插入指定数据; 2. 添加 Struts2 和 Hibernate 支持,使用逆向工程生成持久层代码并完善 DAO 方法; 3. 实现登录功能:`login.jsp` 页面含职位下拉框,登录失败提示“查无此人”,成功则跳转至 `main.jsp` 显示欢迎信息及两个链接; 4. 图书信息添加:通过 `bookadd.jsp` 添加图书,自动增长 ID; 5. 图书信息浏览:在 `bookshow.jsp` 中显示所有图书; 6. 图书信息删除、编辑(预更新与更新)、退出(销毁 Session 并返回登录页)。 --- # 详解 ### **1. 数据库环境准备** 使用 **MySQL 5.1** 创建数据库与表: ```sql CREATE DATABASE bookdb CHARACTER SET utf8 COLLATE utf8_general_ci; USE bookdb; -- 用户表 CREATE TABLE userinfo ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20), job VARCHAR(10), sex VARCHAR(2), pwd VARCHAR(15) ); -- 图书表 CREATE TABLE book ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(40), time DATE, publish VARCHAR(20), author VARCHAR(20) ); -- 插入用户数据 INSERT INTO userinfo (username, job, sex, pwd) VALUES ('张楠', '经理', '女', '13512356789'), ('Cici', '销售', '女', '15578965623'), ('赵航', '员工', '男', '17789562356'); -- 插入图书数据 INSERT INTO book (name, time, publish, author) VALUES ('Java程序', '2025-01-13', '人民邮电', '张小飞'), ('C++', '2024-04-09', '清华大学', '刘能'), ('夜航船', '2015-09-09', '人民文学', '张岱'); ``` > 注意:确保 MySQL 服务运行正常,使用 JDBC 连接时 URL 示例为: > `jdbc:mysql://localhost:3306/bookdb?useUnicode=true&characterEncoding=UTF-8` 将 `mysql-connector-java-5.1.0-bin.jar` 放入项目 `/WebContent/WEB-INF/lib/` 目录中。 --- ### **2. 开发环境搭建(MyEclipse 8.5 + Struts2 + Hibernate)** #### (1)新建 Web 项目 - 打开 MyEclipse 8.5 → New → Web Project → 输入项目名 `bookmis` - 目标运行时选择 Tomcat 6.x 或兼容版本 - 动态 Web 模块版本选 2.5(MyEclipse 8.5 默认) #### (2)添加 Struts2 支持 右键项目 → MyEclipse → Add Struts Capabilities → 选择 Struts 2.x 版本 会自动生成 `struts.xml`、过滤器配置等基础结构。 #### (3)添加 Hibernate 支持 右键项目 → MyEclipse → Add Hibernate Capabilities - 选择 Hibernate 3.x - 配置 `hibernate.cfg.xml` - 数据库连接参数填写正确(MySQL 5.1) - 使用逆向工程生成实体类和映射文件(*.hbm.xml) #### (4)生成 DAO 类 使用 MyEclipse 的 Hibernate Reverse Engineering 工具生成: - `Userinfo.java`, `Userinfo.hbm.xml`, `UserinfoDAO` - `Book.java`, `Book.hbm.xml`, `BookDAO` 手动补充 `BookDAO` 中的 `findAll()`、`delete()`、`update()` 等方法。 --- ### **3. 登录功能实现** #### (1)`login.jsp` ```jsp <%@ page contentType="text/html; charset=UTF-8"%> <html> <head><title>登录</title></head> <body> <form action="user_login" method="post"> 用户名:<input type="text" name="userinfo.username" required><br> 密码:<input type="password" name="userinfo.pwd" required><br> 职位: <select name="userinfo.job"> <option value="经理">经理</option> <option value="销售">销售</option> <option value="员工">员工</option> </select><br> <input type="submit" value="登录"> </form> <% if ("fail".equals(request.getParameter("status"))) { %> <font color="red">查无此人</font> <% } %> </body> </html> ``` #### (2)`UserinfoAction.java` ```java public class UserinfoAction extends ActionSupport { private Userinfo userinfo; private HttpSession session; public String execute() { UserinfoDAO dao = new UserinfoDAO(); List<Userinfo> list = dao.findByUsername(userinfo.getUsername()); if (!list.isEmpty() && list.get(0).getPwd().equals(userinfo.getPwd()) && list.get(0).getJob().equals(userinfo.getJob())) { session = ServletActionContext.getRequest().getSession(); session.setAttribute("user", list.get(0)); return SUCCESS; } else { return "fail"; } } // getter and setter public Userinfo getUserinfo() { return userinfo; } public void setUserinfo(Userinfo userinfo) { this.userinfo = userinfo; } } ``` #### (3)`struts.xml` 配置 ```xml <package name="default" extends="struts-default"> <action name="user_*" class="UserinfoAction" method="{1}"> <result name="success">/main.jsp</result> <result name="fail">login.jsp?status=fail</result> </action> <action name="book_*" class="BookAction" method="{1}"> <result name="add">/bookadd.jsp</result> <result name="list">/bookshow.jsp</result> <result name="main">/main.jsp</result> <result name="updatePre">/bookedit.jsp</result> <result name="success" type="redirectAction">book_findAll</result> </action> </package> ``` --- ### **4. 主页 `main.jsp`** ```jsp <%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% Userinfo user = (Userinfo) session.getAttribute("user"); if (user == null) response.sendRedirect("login.jsp"); %> 欢迎 <b><%= user.getUsername() %></b> 登录!<br> <a href="book_add">图书信息添加</a><br> <a href="book_findAll">图书信息浏览</a><br> <a href="user_logout">退出系统</a> ``` --- ### **5. 图书添加功能** #### (1)`bookadd.jsp` ```jsp <form action="book_add" method="post"> 图书名称:<input type="text" name="book.name"><br> 出版时间:<input type="date" name="book.time"><br> 出版社:<input type="text" name="book.publish"><br> 作者:<input type="text" name="book.author"><br> <input type="submit" value="添加"> </form> ``` #### (2)`BookAction.java` 添加方法 ```java public class BookAction extends ActionSupport { private Book book; private List<Book> bookList; private HttpSession session; public String add() { BookDAO dao = new BookDAO(); dao.save(book); return "main"; } public String findAll() { BookDAO dao = new BookDAO(); bookList = dao.findAll(); session = ServletActionContext.getRequest().getSession(); session.setAttribute("books", bookList); return "list"; } public String delete() { BookDAO dao = new BookDAO(); dao.delete(dao.findById(Integer.parseInt(ServletActionContext.getRequest().getParameter("id")))); return "success"; } public String preupdate() { BookDAO dao = new BookDAO(); Book b = dao.findById(Integer.parseInt(ServletActionContext.getRequest().getParameter("id"))); session = ServletActionContext.getRequest().getSession(); session.setAttribute("editBook", b); return "updatePre"; } public String update() { BookDAO dao = new BookDAO(); dao.attachDirty(book); // 更新 return "success"; } public String logout() { session = ServletActionContext.getRequest().getSession(); session.invalidate(); // 销毁 Session return "login"; } // getter and setter public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public List<Book> getBookList() { return bookList; } public void setBookList(List<Book> bookList) { this.bookList = bookList; } } ``` --- ### **6. 图书浏览 `bookshow.jsp`** ```jsp <table border="1"> <tr><th>图书名称</th><th>出版时间</th><th>出版社</th><th>作者</th><th>操作</th></tr> <s:iterator value="#session.books" var="b"> <tr> <td>${b.name}</td> <td>${b.time}</td> <td>${b.publish}</td> <td>${b.author}</td> <td> <a href="book_delete?id=${b.id}">删除</a> | <a href="book_preupdate?id=${b.id}">编辑</a> </td> </tr> </s:iterator> </table> ``` --- ### **7. 编辑功能补充** #### `bookedit.jsp` ```jsp <% Book b = (Book) session.getAttribute("editBook"); %> <form action="book_update" method="post"> <input type="hidden" name="book.id" value="<%=b.getId()%>"> 图书名称:<input type="text" name="book.name" value="<%=b.getName()%>"><br> 出版时间:<input type="date" name="book.time" value="<%=b.getTime()%>"><br> 出版社:<input type="text" name="book.publish" value="<%=b.getPublish()%>"><br> 作者:<input type="text" name="book.author" value="<%=b.getAuthor()%>"><br> <input type="submit" value="更新"> </form> ``` --- ### **8. 退出功能** 已在 `UserinfoAction` 或 `BookAction` 中实现 `logout()` 方法,配置结果跳转至 `login.jsp`。 --- ## 知识点 ### 1. **Struts2 请求处理机制** Struts2 通过拦截器封装请求,Action 接收参数,`method="{1}"` 实现通配符映射,降低配置量。 ### 2. **Hibernate 逆向工程** 利用 MyEclipse 反向生成实体与 DAO,简化数据库交互,核心基于 ORM 映射文件(.hbm.xml)。 ### 3. **JDBC 连接 MySQL 5.1** 使用 `mysql-connector-java-5.1.0-bin.jar` 驱动,配置 `hibernate.cfg.xml` 中 URL、用户名、密码完成连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值