码迷,mamicode.com
首页 > 其他好文 > 详细

2019年12月9日:周一自主学习成果

时间:2019-12-09 19:55:12      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:enter   加载   each   div   alert   doctype   ESS   sub   arraylist   

通过自己的知识搭出了任务要求的基本框架,完成了前段输入数据向数据库的导入,下一步准备写登录后的界面,不同的身份对应不同的登录界面,有不同的按钮实现不同的功能。以下是程序源代码:

java代码:

package com.wyt.jdbcu;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JDBCUtils {
//静态语句块
static {
//JDBCUtils.calss获得对象
//getClassLoader()类加载器
//getResourceAsStream("db.properties")加载资源文件放到输入流中
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");
//创建Properties类型对象
Properties p = new Properties();
//加载流文件
try {
p.load(is);
String driver = p.getProperty("driver");
//加载驱动
Class.forName(driver);
System.out.println("驱动加载成功");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

package com.wyt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hjf.entity.Course;
import com.hjf.util.DBUtil;

/**
* 课程Dao
* Dao层操作数据
* @author Hu
*
*/
public class CourseDao {

/**
* 添加
* @param course
* @return
*/
public boolean add(Course course) {
String sql = "insert into text(name, password, sf) values(‘" + course.getName() + "‘,‘" + course.getPassword() + "‘,‘" + course.getSf() + "‘)";
//创建数据库链接
Connection conn = DBUtil.getConn();
Statement state = null;
boolean f = false;
int a = 0;

try {
state = conn.createStatement();
state.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
DBUtil.close(state, conn);
}

if (a > 0) {
f = true;
}
return f;
}

/**
* 删除
*
* @param id
* @return
*/
public boolean delete (int id) {
boolean f = false;
String sql = "delete from text where id=‘" + id + "‘";

Connection conn = DBUtil.getConn();
Statement state = null;
int a = 0;

try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}

if (a > 0) {
f = true;
}
return f;
}

/**
* 修改
* @param name
* @param pass
*/
public boolean update(Course course) {
String sql = "update text set name=‘" + course.getName() + "‘, password=‘" + course.getPassword() + "‘, sf=‘" + course.getSf()
+ "‘ where id=‘" + course.getId() + "‘";
Connection conn = DBUtil.getConn();
Statement state = null;
boolean f = false;
int a = 0;

try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}

if (a > 0) {
f = true;
}
return f;
}

/**
* 验证课程名称是否唯一
* true --- 不唯一
* @param name
* @return
*/
public boolean name(String name) {
boolean flag = false;
String sql = "select name from text where name = ‘" + name + "‘";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;

try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return flag;
}

/**
* 通过ID得到课程信息
* @param id
* @return
*/
public Course getCourseById(int id) {
String sql = "select * from text where id =‘" + id + "‘";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
Course course = null;

try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
String password = rs.getString("password");
String sf = rs.getString("sf");
course = new Course(id, name, password, sf);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}

return course;
}

/**
* 通过name得到Course
* @param name
* @return
*/
public Course getCourseByName(String name) {
String sql = "select * from text where name =‘" + name + "‘";
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;
Course course = null;

try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String password= rs.getString("password");
String sf = rs.getString("sf");
course = new Course(id, name, password, sf);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}

return course;
}

/**
* 查找
* @param name
* @param teacher
* @param classroom
* @return
*/
public List<Course> search(String name, String password, String sf) {
String sql = "select * from text where ";
if (name != "") {
sql += "name like ‘%" + name + "%‘";
}
if (password != "") {
sql += "password like ‘%" + password + "%‘";
}
if (sf != "") {
sql += "sf like ‘%" + sf + "%‘";
}
List<Course> list = new ArrayList<>();
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;

try {
state = conn.createStatement();
rs = state.executeQuery(sql);
Course bean = null;
while (rs.next()) {
int id = rs.getInt("id");
String name2 = rs.getString("name");
String password2 = rs.getString("password");
String sf2 = rs.getString("sf");
bean = new Course(id, name2, password2, sf2);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}

return list;
}

/**
* 全部数据
* @param name
* @param teacher
* @param classroom
* @return
*/
public List<Course> list() {
String sql = "select * from text";
List<Course> list = new ArrayList<>();
Connection conn = DBUtil.getConn();
Statement state = null;
ResultSet rs = null;

try {
state = conn.createStatement();
rs = state.executeQuery(sql);
Course bean = null;
while (rs.next()) {
int id = rs.getInt("id");
String name2 = rs.getString("name");
String password2 = rs.getString("password");
String sf2 = rs.getString("sf");
bean = new Course(id, name2, password2, sf2);
list.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}

return list;
}

}

package com.wyt.bl;

public class Course {

private int id;
private String name;
private String password;
private String sf;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSf() {
return sf;
}
public void setSf(String sf) {
this.sf = sf;
}

public Course() {}

public Course(int id, String name, String password, String sf) {
this.id = id;
this.name = name;
this.password = password;
this.sf = sf;
}

public Course(String name, String password, String sf) {
this.name = name;
this.password = password;
this.sf = sf;
}
}

 

package com.wyt.service;

import java.util.List;

import com.hjf.dao.CourseDao;
import com.hjf.entity.Course;

/**
* CourseService
* 服务层
* @author Hu
*
*/
public class CourseService {

CourseDao cDao = new CourseDao();

/**
* 添加
* @param course
* @return
*/
public boolean add(Course course) {
boolean f = false;
if(!cDao.name(course.getName())) {
cDao.add(course);
f = true;
}
return f;
}

/**
* 删除
*/
public void del(int id) {
cDao.delete(id);
}

/**
* 修改
* @return
*/
public void update(Course course) {
cDao.update(course);
}

/**
* 通过ID得到一个Course
* @return
*/
public Course getCourseById(int id) {
return cDao.getCourseById(id);
}

/**
* 通过Name得到一个Course
* @return
*/
public Course getCourseByName(String name) {
return cDao.getCourseByName(name);
}

/**
* 查找
* @return
*/
public List<Course> search(String name, String password, String sf) {
return cDao.search(name, password,sf);
}

/**
* 全部数据
* @return
*/
public List<Course> list() {
return cDao.list();
}
}

 

package com.wyt.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBUtil {

public static String db_url = "jdbc:mysql://localhost:3306/data?characterEncoding=utf8";
public static String db_user = "root";
public static String db_pass = "zhangxinyue520";

public static Connection getConn () {
Connection conn = null;

try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}

return conn;
}


public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) throws SQLException {
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql ="select * from text";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("欧了");
}else{
System.out.println("再试试");
}
}
}

 

package com.wyt.jdbc;

public class JDBC {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("驱动加载失败");
}
}

}

 

package com.wyt.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hjf.entity.Course;
import com.hjf.service.CourseService;

@WebServlet("/CourseServlet")
public class CourseServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

CourseService service = new CourseService();

/**
* 方法选择
*/
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");

if ("add".equals(method)) {
add(req, resp);
} else if ("del".equals(method)) {
del(req, resp);
} else if ("update".equals(method)) {
update(req, resp);
} else if ("search".equals(method)) {
search(req, resp);
} else if ("getcoursebyid".equals(method)) {
getCourseById(req, resp);
} else if ("getcoursebyname".equals(method)) {
getCourseByName(req, resp);
} else if ("list".equals(method)) {
list(req, resp);
}
}
private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
req.setCharacterEncoding("utf-8");
//获取数据
String name = req.getParameter("name");
String password = req.getParameter("password");
String sf = req.getParameter("sf");
Course course = new Course(name, password, sf);

//添加后消息显示
if(service.add(course)) {
req.setAttribute("message", "添加成功");
req.getRequestDispatcher("denglu1.jsp").forward(req,resp);
} else {
req.setAttribute("message", "用户已存在,请重新录入");
req.getRequestDispatcher("zhuce.jsp").forward(req,resp);
}
}

/**
* 全部
* @param req
* @param resp
* @throws ServletException
*/
private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");

List<Course> courses = service.list();
req.setAttribute("courses", courses);
req.getRequestDispatcher("list.jsp").forward(req,resp);
}

/**
* 通过ID得到Course
* @param req
* @param resp
* @throws ServletException
*/
private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
Course course = service.getCourseById(id);
req.setAttribute("course", course);
req.getRequestDispatcher("detail2.jsp").forward(req,resp);
}

/**
* 通过名字查找
* 跳转至删除
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
Course course = service.getCourseByName(name);
if(course == null) {
req.setAttribute("message", "查无此人!");
req.getRequestDispatcher("del.jsp").forward(req,resp);
} else {
req.setAttribute("course", course);
req.getRequestDispatcher("detail.jsp").forward(req,resp);
}
}

/**
* 删除
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
service.del(id);
req.setAttribute("message", "删除成功!");
req.getRequestDispatcher("del.jsp").forward(req,resp);
}

/**
* 修改
* @param req
* @param resp
* @throws IOException
* @throws ServletException
*/
private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
String name = req.getParameter("name");
String password = req.getParameter("password");
String sf = req.getParameter("sf");
Course course = new Course(id, name, password, sf);

service.update(course);
req.setAttribute("message", "修改成功");
req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp);
}

/**
* 查找
* @param req
* @param resp
* @throws ServletException
*/
private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String password = req.getParameter("password");
String sf = req.getParameter("sf");
List<Course> courses = service.search(name, password, sf);
req.setAttribute("courses", courses);
req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
}
}

 

jsp代码:

主页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登陆or注册</title>
</head>
<body>
<a href="denglu1.jsp">用户登录</a>
<a href="zhuce.jsp">用户注册</a>
</body>
</html>

注册页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户注册</title>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){

%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<h1 style="color: black;">身份注册</h1>
<a href="denglu.jsp">返回主页</a>
<form action="CourseServlet?method=add" method="post" onsubmit="return check()">
<div class="a">
用户名<input type="text" id="name" name="name"/>
</div>
<div class="a">
密&nbsp;&nbsp;&nbsp;码<input type="password" id="password" name="password" />
</div>
<div class="a">
身&nbsp;&nbsp;&nbsp;份<input type="text" id="sf" name="sf" />
</div>
<div class="a">
<button type="submit" class="b">提&nbsp;&nbsp;&nbsp;交</button>
</div>
</form>
</div>
<script type="text/javascript">
function check() {
var name = document.getElementById("name");;
var password = document.getElementById("password");
var sf = document.getElementById("sf");

//非空
if(name.value == ‘‘) {
alert(‘用户名不能为空‘);
name.focus();
return false;
}
if(password.value == ‘‘) {
alert(‘密码不能为空‘);
password.focus();
return false;
}
if(sf.value == ‘‘) {
alert(‘身份信息不能为空‘);
sf.focus();
return false;
}
}
</script>
</body>
</html>

 

登录页面:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户登录</title>
</head>
<body>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){

%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<div align="center">
<h1 style="color: black;">用户登录</h1>
<a href="denglu.jsp">返回主页</a>
<form action="CourseServlet?method=search" method="post" onsubmit="return check()">
<div class="a">
用户名<input type="text" id="name" name="name"/>
</div>
<div class="a">
密&nbsp;&nbsp;&nbsp;码<input type="text" id="password" name="password" />
</div>
<div class="a">
<button type="submit" class="b">登&nbsp;&nbsp;&nbsp;陆</button>
</div>
</form>
</div>
<script type="text/javascript">
function check() {
var name = document.getElementById("name");;
var password = document.getElementById("password");

//非空
if(name.value == ‘‘) {
alert(‘用户名为空‘);
return false;
}
if(password.value ==‘‘){
alert(‘密码为空‘);
return false;
}
}
</script>
</body>
</html>

由于登陆之后的searchlist页面并没有写所以在点击登录后显示404错误--源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示。

下一步将围绕整体思路,建模后逐一写出不同用户登陆后显示的不同页面,以“身份”这一条件进行判断。

2019年12月9日:周一自主学习成果

标签:enter   加载   each   div   alert   doctype   ESS   sub   arraylist   

原文地址:https://www.cnblogs.com/zhangxinyue/p/12012480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!