美洲杯波胆_美洲杯波胆赔率
做最好的网站
来自 计算机教程 2019-05-30 11:10 的文章
当前位置: 美洲杯波胆 > 计算机教程 > 正文

Web前端开发:SQL Jsp小项目(一)

由于自己也不是特别的理解,不能做过多的解释,因为这些是问过老师做出来的,我还没有学到这里。如果有解释错误的 指出来我改正一下。见谅~(如果学到这里了,会完善)

  Jsp的学习算是告一段落,针对这段时间的学习,写了一个Jsp小项目来巩固学到的知识。

工具类(SQLUtil)核心代码:

   框架示意图

package edu.nf.homeWork.utils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SQLUtil {

    /**
     * 
     * @param sql Sql查询语句
     * @param clazz 泛型class
     * @param params 参数值
     * @return 泛型对象
     * @throws RuntimeException
     */
    public static <T> T execute(String sql, Class<T> clazz, Object...params)throws RuntimeException {
        try {
            //获取连接对象
            Connection conn = DBUtil.getConnection();
            PreparedStatement ps = null;
            ResultSet rs = null;
            ps = conn.prepareStatement(sql);
            //循环遍历把参数插入数据库
            for (int i=1; i<=params.length; i  ) {
                ps.setObject(i, params[i-1]);
            }
            rs = ps.executeQuery();
            //调用getBean方法
            T instance = getBean(rs, clazz);
            return instance;
        } catch (Exception e) {
            throw new RuntimeException();
        } 
    }
    /**
     * 
     * @param rs 结果集
     * @param clazz 泛型class
     * @return 返回一个泛型对象
     * @throws RuntimeException
     */
    public static <T> T getBean(ResultSet rs, Class<T> clazz) throws RuntimeException {
        try {
            //实例化对象
            Object instance = clazz.newInstance();
            //获取对象的所有属性(字段)
            Field[] fields = clazz.getDeclaredFields();
            rs.next();
            for (Field field : fields) {
                //属性对应的注解
                if(field.isAnnotationPresent(Column.class)) {
                    //获取对应的值
                    String columnName = field.getAnnotation(Column.class).value();
                    Object o = rs.getObject(columnName);
                    field.setAccessible(true);
                    field.set(instance, o);
                }
            }
            //强转泛型对象
            return (T)instance;
        } catch (Exception e) {
            throw new RuntimeException();
        } 
    }
}

User list process

用注解绑定实体类(Users):

图片 1

package edu.nf.homeWork.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {

    String value();
}

UserAdd process

实体类(Users):

图片 2

package edu.nf.homeWork.entity;

import edu.nf.homeWork.utils.Column;

public class Users {
    //用注解绑定数据库里面的列
    @Column("useName")
    private String userName;
    @Column("passwords")
    private String password;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "Users [userName="   userName   ", password="   password   "]";
    }

}

  需要的界面效果:

查询单个用户的信息的方法

图片 3

    /**
     * 查询单个用户的信息
     * @param userName
     * @param passWord
     * @return
     */
    public Users findUser(String userName,String passWord) {
        //sql查询语句
        String sql="select *from useinfo where usename=? and passwords=?";
        //调用工具类返回反省对象
        //参数:传入SQL语句    Users类    后面两个参数值
        Users users = SQLUtil.execute(sql, Users.class, userName, passWord);
        return users;
    }

图片 4

 main方法执行

 

public static void main(String[] args) {
        UserDao dao = new UserDao();
        //调用查询方法
        Users u = dao.findUser("11", "11");
        //显示账号
        System.out.println("账号:" u.getUserName());
    }

 

//输出结果:

  需要工具:Eclipse、TomCat v8.0和PL/SQL

账号:11

  先用PL/SQL创建一个用户c##cms,并创建一个cms_user表单

图片 5

  

  首先,需要写一个JSP——add界面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<%
    String path = request.getContextPath();
    String basePath = request.getScheme()   "://"
              request.getServerName()   ":"   request.getServerPort()
              path   "/";
%>

<html>
<head>
<base href="<%=basePath%>" />
<title>用户添加</title>
<link rel="stylesheet" type="text/css" title="xp"
    href="css/skins/xp/validator/component.css" />
<link rel="stylesheet" type="text/css" title="xp"
    href="css/skins/xp/navbar/nav.css" />
<link rel="stylesheet" type="text/css" title="xp"
    href="css/skins/xp/table/skin.css" />
<link rel="stylesheet" type="text/css" title="xp"
    href="css/skins/xp/time/skin.css" />
<script type="text/javascript" src="jscript/time/calendar.js"></script>
<script type="text/javascript" src="jscript/time/calendar-zh.js"></script>
<script type="text/javascript" src="jscript/time/calendar-setup.js"></script>
<script type="text/javascript" src="jscript/common.js"></script>
<script type="text/javascript" src="jscript/validator/form_validator.js" /></script>



<style type="text/css">
body, table, td, select, textarea, input {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
}
</style>
</head>
<body>
    <div id="main">


        <form name="backuserform" method="post" action="admin/UserAddServlet"
            onSubmit='return submitForm(document.forms[0]);'>
            <table class="standard">
                <thead>
                    <tr>
                        <th align="center" colspan="2">用户添加</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td align="left">用户名</td>
                        <td align="left"><input name="name" type="text" TABINDEX="1"
                            id="name" />
                            <div class="Info">
                                <div id="name_info"></div>
                            </div></td>
                    </tr>
                    <tr>
                        <td align="left">用户密码</td>
                        <td align="left"><input name="password" type="password"
                            value="" TABINDEX="2" id="password" />
                            <div class="Info">
                                <div id="password_info"></div>
                            </div></td>
                    </tr>

                    <tr>
                        <td align="left">角色</td>
                        <td align="left"><select name="role" TABINDEX="4" id="role">
                                <option value="1">超级管理员</option>
                        </select>

                            <div class="Info">
                                <div id="role_info"></div>
                            </div></td>
                    </tr>

                    <tr>
                        <td align="left">是否有效</td>
                        <td align="left"><select name="valid" TABINDEX="3" id="valid">
                                <option value="1">有效</option>
                                <option value="0">无效</option>
                        </select>
                            <div class="Info">
                                <div id="valid_info"></div>
                            </div></td>
                    </tr>
                    <tr>
                        <td align="left">EMAIL</td>
                        <td align="left"><input name="email" type="text" value=""
                            TABINDEX="5" id="email" />
                            <div class="Info">
                                <div id="email_info"></div>
                            </div></td>
                    </tr>

                    <tr>
                        <td align="left">电话</td>
                        <td align="left"><input name="phone" type="text" value=""
                            TABINDEX="6" id="phone" />
                            <div class="Info">
                                <div id="phone_info"></div>
                            </div></td>
                    </tr>

                    <tr>
                        <td colspan="2" align="center"><input class="submitButton"
                            type="submit" TABINDEX="7" name="submit" value="提&nbsp;交">
                            <input type="button" name="返回" class="submitButton" value="返回"
                            onclick="history.back();"></td>
                    </tr>

                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="2" style="text-align: left"></td>

                    </tr>
                </tfoot>
            </table>
        </form>
    </div>
</body>
</html>

本页面接收的数据打包封装并发送到UserServlet

package com.jaovo.jcms.user;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import com.jaovo.jcms.service.UserService;

public class UserAddServlet extends HttpServlet{

    public UserAddServlet() {
        super();
    }

    @Override
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        //获取 客户端(浏览器)提交的数据
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String valid = request.getParameter("valid");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");

        //把数据封装到User对象里面去
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        user.setPassword(password);
        user.setPhone(phone);
        user.setTime_stamp(new Timestamp(new Date().getTime()));//获取系统当前时间,当做注册时间

        //这里要进行插入数据库
        UserService us = new UserService();
        us.addUser(user);

        this.getServletContext()
            .getRequestDispatcher("/admin/user_list.jsp")
            .forward(request, response);
    }
}

本文由美洲杯波胆发布于计算机教程,转载请注明出处:Web前端开发:SQL Jsp小项目(一)

关键词: