一、下载安装数据库(因为我的电脑之前安装的MySQL不能用,所以这次换成了SQLyog。安装,还不算难,不过之前安装过MySQL的,一定要把相关的东西尤其是表格什么的删干净!!!)
下载网址:https://downloads.mysql.com/archives/community/
下载64msi(版本视情况而定)
教程:
1. 打开软件,点击Next安装Mysql
2. 选择Custom定制安装。
3. 选则安装路径,建议保持默认路径或者把C盘改为D盘。然后下一步,
4. 点击Install安装
5. 点击NextàFinish完成安装。
6. 等待各个软件安装完成,点击下一步,则显示配置界面,直接下一步即可,
7. 点击下一步,知道出现数据路径配置界面,选择数据库数据存储路径。
8. 点击下一步,知道出现语言配置界面,选择最后一项,下拉框选择utf-8。
9. 点击下一步,知道出现配置mysql的链接密码界面,把密码设置为root,点下一步,进行配置。
10. 安装完成,点击finish完成安装
安装mysql界面操作软件
1. 打开“SQLyog 12.0.8.0.zip”文件进行安装,打开后会选择语言,选择自己熟悉的,点击ok。
2. 之后安装默认选项下一步安装即可。直到安装完成。
3 .建表
①右击表——创建新表然后根据自己需要建立新表。
二、建立Java web 项目(前提是配置好tomcat环境,网上有详细教程,就不详细说了)
1. eclipse新建一个Dynamic Web Project
2.将连接mysql的驱动jar包(mysql-connector-java-5.1.7-bin.jar)copy到WEB-INF下的lib目录下(jar包可以在网上下载)
3.新建五个包,entity存放实体类(User),dao存放数据库操作类(UserDao),servlet存放控制类(addServlet,updateServlet,deleteServlet,listServlet),util存放开发帮助类service是算法实现(这里是数据库操作帮助类,封装了数据库连接部分代码,避免大量重复代码)
4. 新建类,java类放在对应包中,jsp页面放在WebContent目录下,替换web.xml中内容,结构如图
三、快乐地搬代码时间
1 //Course.java
2 packagecom.hjf.entity;3
4 public classCourse {5
6 private intid;7 privateString name;8 privateString teacher;9 privateString classroom;10
11 public intgetId() {12 returnid;13 }14 public void setId(intid) {15 this.id =id;16 }17 publicString getName() {18 returnname;19 }20 public voidsetName(String name) {21 this.name =name;22 }23 publicString getTeacher() {24 returnteacher;25 }26 public voidsetTeacher(String teacher) {27 this.teacher =teacher;28 }29 publicString getClassroom() {30 returnclassroom;31 }32 public voidsetClassroom(String classroom) {33 this.classroom =classroom;34 }35
36 publicCourse() {}37
38 public Course(intid, String name, String teacher, String classroom) {39 this.id =id;40 this.name =name;41 this.teacher =teacher;42 this.classroom =classroom;43 }44
45 publicCourse(String name, String teacher, String classroom) {46 this.name =name;47 this.teacher =teacher;48 this.classroom =classroom;49 }50 }
1 //CourseDao.java
2 packagecom.hjf.dao;3
4 importjava.sql.Connection;5 importjava.sql.ResultSet;6 importjava.sql.SQLException;7 importjava.sql.Statement;8 importjava.util.ArrayList;9 importjava.util.List;10
11 importcom.hjf.entity.Course;12 importcom.hjf.util.DBUtil;13
14 /**
15 * 课程Dao16 * Dao层操作数据17 *@authorHu18 *19 */
20 public classCourseDao {21
22 /**
23 * 添加24 *@paramcourse25 *@return
26 */
27 public booleanadd(Course course) {28 String sql = “insert into course(name, teacher, classroom) values(‘” + course.getName() + “‘,'” + course.getTeacher() + “‘,'” + course.getClassroom() + “‘)”;29 //创建数据库链接
30 Connection conn =DBUtil.getConn();31 Statement state = null;32 boolean f = false;33 int a = 0;34
35 try{36 state =conn.createStatement();37 state.executeUpdate(sql);38 } catch(Exception e) {39 e.printStackTrace();40 } finally{41 //关闭连接
42 DBUtil.close(state, conn);43 }44
45 if (a > 0) {46 f = true;47 }48 returnf;49 }50
51 /**
52 * 删除53 *54 *@paramid55 *@return
56 */
57 public boolean delete (intid) {58 boolean f = false;59 String sql = “delete from course where id='” + id + “‘”;60
61 Connection conn =DBUtil.getConn();62 Statement state = null;63 int a = 0;64
65 try{66 state =conn.createStatement();67 a =state.executeUpdate(sql);68 } catch(SQLException e) {69 e.printStackTrace();70 } finally{71 DBUtil.close(state, conn);72 }73
74 if (a > 0) {75 f = true;76 }77 returnf;78 }79
80 /**
81 * 修改82 *@paramname83 *@parampass84 */
85 public booleanupdate(Course course) {86 String sql = “update course set name='” + course.getName() + “‘, teacher='” + course.getTeacher() + “‘, classroom='” +course.getClassroom()87 + “‘ where id='” + course.getId() + “‘”;88 Connection conn =DBUtil.getConn();89 Statement state = null;90 boolean f = false;91 int a = 0;92
93 try{94 state =conn.createStatement();95 a =state.executeUpdate(sql);96 } catch(SQLException e) {97 e.printStackTrace();98 } finally{99 DBUtil.close(state, conn);100 }101
102 if (a > 0) {103 f = true;104 }105 returnf;106 }107
108 /**
109 * 验证课程名称是否唯一110 * true — 不唯一111 *@paramname112 *@return
113 */
114 public booleanname(String name) {115 boolean flag = false;116 String sql = “select name from course where name = ‘” + name + “‘”;117 Connection conn =DBUtil.getConn();118 Statement state = null;119 ResultSet rs = null;120
121 try{122 state =conn.createStatement();123 rs =state.executeQuery(sql);124 while(rs.next()) {125 flag = true;126 }127 } catch(SQLException e) {128 e.printStackTrace();129 } finally{130 DBUtil.close(rs, state, conn);131 }132 returnflag;133 }134
135 /**
136 * 通过ID得到课程信息137 *@paramid138 *@return
139 */
140 public Course getCourseById(intid) {141 String sql = “select * from course where id ='” + id + “‘”;142 Connection conn =DBUtil.getConn();143 Statement state = null;144 ResultSet rs = null;145 Course course = null;146
147 try{148 state =conn.createStatement();149 rs =state.executeQuery(sql);150 while(rs.next()) {151 String name = rs.getString(“name”);152 String teacher = rs.getString(“teacher”);153 String classroom = rs.getString(“classroom”);154 course = newCourse(id, name, teacher, classroom);155 }156 } catch(Exception e) {157 e.printStackTrace();158 } finally{159 DBUtil.close(rs, state, conn);160 }161
162 returncourse;163 }164
165 /**
166 * 通过name得到Course167 *@paramname168 *@return
169 */
170 publicCourse getCourseByName(String name) {171 String sql = “select * from course where name ='” + name + “‘”;172 Connection conn =DBUtil.getConn();173 Statement state = null;174 ResultSet rs = null;175 Course course = null;176
177 try{178 state =conn.createStatement();179 rs =state.executeQuery(sql);180 while(rs.next()) {181 int id = rs.getInt(“id”);182 String teacher = rs.getString(“teacher”);183 String classroom = rs.getString(“classroom”);184 course = newCourse(id, name, teacher, classroom);185 }186 } catch(Exception e) {187 e.printStackTrace();188 } finally{189 DBUtil.close(rs, state, conn);190 }191
192 returncourse;193 }194
195 /**
196 * 查找197 *@paramname198 *@paramteacher199 *@paramclassroom200 *@return
201 */
202 public Listsearch(String name, String teacher, String classroom) {203 String sql = “select * from course where “;204 if (name != “”) {205 sql += “name like ‘%” + name + “%'”;206 }207 if (teacher != “”) {208 sql += “teacher like ‘%” + teacher + “%'”;209 }210 if (classroom != “”) {211 sql += “classroom like ‘%” + classroom + “%'”;212 }213 List list = new ArrayList<>();214 Connection conn =DBUtil.getConn();215 Statement state = null;216 ResultSet rs = null;217
218 try{219 state =conn.createStatement();220 rs =state.executeQuery(sql);221 Course bean = null;222 while(rs.next()) {223 int id = rs.getInt(“id”);224 String name2 = rs.getString(“name”);225 String teacher2 = rs.getString(“teacher”);226 String classroom2 = rs.getString(“classroom”);227 bean = newCourse(id, name2, teacher2, classroom2);228 list.add(bean);229 }230 } catch(SQLException e) {231 e.printStackTrace();232 } finally{233 DBUtil.close(rs, state, conn);234 }235
236 returnlist;237 }238
239 /**
240 * 全部数据241 *@paramname242 *@paramteacher243 *@paramclassroom244 *@return
245 */
246 public Listlist() {247 String sql = “select * from course”;248 List list = new ArrayList<>();249 Connection conn =DBUtil.getConn();250 Statement state = null;251 ResultSet rs = null;252
253 try{254 state =conn.createStatement();255 rs =state.executeQuery(sql);256 Course bean = null;257 while(rs.next()) {258 int id = rs.getInt(“id”);259 String name2 = rs.getString(“name”);260 String teacher2 = rs.getString(“teacher”);261 String classroom2 = rs.getString(“classroom”);262 bean = newCourse(id, name2, teacher2, classroom2);263 list.add(bean);264 }265 } catch(SQLException e) {266 e.printStackTrace();267 } finally{268 DBUtil.close(rs, state, conn);269 }270
271 returnlist;272 }273
274 }
1 //CourseService.java
2 packagecom.hjf.service;3
4 importjava.util.List;5
6 importcom.hjf.dao.CourseDao;7 importcom.hjf.entity.Course;8
9 /**
10 * CourseService11 * 服务层12 *@authorHu13 *14 */
15 public classCourseService {16
17 CourseDao cDao = newCourseDao();18
19 /**
20 * 添加21 *@paramcourse22 *@return
23 */
24 public booleanadd(Course course) {25 boolean f = false;26 if(!cDao.name(course.getName())) {27 cDao.add(course);28 f = true;29 }30 returnf;31 }32
33 /**
34 * 删除35 */
36 public void del(intid) {37 cDao.delete(id);38 }39
40 /**
41 * 修改42 *@return
43 */
44 public voidupdate(Course course) {45 cDao.update(course);46 }47
48 /**
49 * 通过ID得到一个Course50 *@return
51 */
52 public Course getCourseById(intid) {53 returncDao.getCourseById(id);54 }55
56 /**
57 * 通过Name得到一个Course58 *@return
59 */
60 publicCourse getCourseByName(String name) {61 returncDao.getCourseByName(name);62 }63
64 /**
65 * 查找66 *@return
67 */
68 public Listsearch(String name, String teacher, String classroom) {69 returncDao.search(name, teacher, classroom);70 }71
72 /**
73 * 全部数据74 *@return
75 */
76 public Listlist() {77 returncDao.list();78 }79 }
1 //CourseServlet.java
2 packagecom.hjf.servlet;3
4 importjava.io.IOException;5 importjava.util.List;6
7 importjavax.servlet.ServletException;8 importjavax.servlet.annotation.WebServlet;9 importjavax.servlet.http.HttpServlet;10 importjavax.servlet.http.HttpServletRequest;11 importjavax.servlet.http.HttpServletResponse;12
13 importcom.hjf.entity.Course;14 importcom.hjf.service.CourseService;15
16 @WebServlet(“/CourseServlet”)17 public class CourseServlet extendsHttpServlet {18
19 private static final long serialVersionUID = 1L;20
21 CourseService service = newCourseService();22
23 /**
24 * 方法选择25 */
26 protected void service(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {27 req.setCharacterEncoding(“utf-8”);28 String method = req.getParameter(“method”);29
30 if (“add”.equals(method)) {31 add(req, resp);32 } else if (“del”.equals(method)) {33 del(req, resp);34 } else if (“update”.equals(method)) {35 update(req, resp);36 } else if (“search”.equals(method)) {37 search(req, resp);38 } else if (“getcoursebyid”.equals(method)) {39 getCourseById(req, resp);40 } else if (“getcoursebyname”.equals(method)) {41 getCourseByName(req, resp);42 } else if (“list”.equals(method)) {43 list(req, resp);44 }45 }46
47 /**
48 * 添加49 *@paramreq50 *@paramresp51 *@throwsIOException52 *@throwsServletException53 */
54 private void add(HttpServletRequest req, HttpServletResponse resp) throwsIOException, ServletException {55 req.setCharacterEncoding(“utf-8”);56 //获取数据
57 String name = req.getParameter(“name”);58 String teacher = req.getParameter(“teacher”);59 String classroom = req.getParameter(“classroom”);60 Course course = newCourse(name, teacher, classroom);61
62 //添加后消息显示
63 if(service.add(course)) {64 req.setAttribute(“message”, “添加成功”);65 req.getRequestDispatcher(“add.jsp”).forward(req,resp);66 } else{67 req.setAttribute(“message”, “课程名称重复,请重新录入”);68 req.getRequestDispatcher(“add.jsp”).forward(req,resp);69 }70 }71
72 /**
73 * 全部74 *@paramreq75 *@paramresp76 *@throwsServletException77 */
78 private void list(HttpServletRequest req, HttpServletResponse resp) throwsIOException, ServletException{79 req.setCharacterEncoding(“utf-8”);80
81 List courses =service.list();82 req.setAttribute(“courses”, courses);83 req.getRequestDispatcher(“list.jsp”).forward(req,resp);84 }85
86 /**
87 * 通过ID得到Course88 *@paramreq89 *@paramresp90 *@throwsServletException91 */
92 private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throwsIOException, ServletException{93 req.setCharacterEncoding(“utf-8”);94 int id = Integer.parseInt(req.getParameter(“id”));95 Course course =service.getCourseById(id);96 req.setAttribute(“course”, course);97 req.getRequestDispatcher(“detail2.jsp”).forward(req,resp);98 }99
100 /**
101 * 通过名字查找102 * 跳转至删除103 *@paramreq104 *@paramresp105 *@throwsIOException106 *@throwsServletException107 */
108 private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throwsIOException, ServletException{109 req.setCharacterEncoding(“utf-8”);110 String name = req.getParameter(“name”);111 Course course =service.getCourseByName(name);112 if(course == null) {113 req.setAttribute(“message”, “查无此课程!”);114 req.getRequestDispatcher(“del.jsp”).forward(req,resp);115 } else{116 req.setAttribute(“course”, course);117 req.getRequestDispatcher(“detail.jsp”).forward(req,resp);118 }119 }120
121 /**
122 * 删除123 *@paramreq124 *@paramresp125 *@throwsIOException126 *@throwsServletException127 */
128 private void del(HttpServletRequest req, HttpServletResponse resp) throwsIOException, ServletException{129 req.setCharacterEncoding(“utf-8”);130 int id = Integer.parseInt(req.getParameter(“id”));131 service.del(id);132 req.setAttribute(“message”, “删除成功!”);133 req.getRequestDispatcher(“del.jsp”).forward(req,resp);134 }135
136 /**
137 * 修改138 *@paramreq139 *@paramresp140 *@throwsIOException141 *@throwsServletException142 */
143 private void update(HttpServletRequest req, HttpServletResponse resp) throwsIOException, ServletException{144 req.setCharacterEncoding(“utf-8”);145 int id = Integer.parseInt(req.getParameter(“id”));146 String name = req.getParameter(“name”);147 String teacher = req.getParameter(“teacher”);148 String classroom = req.getParameter(“classroom”);149 Course course = newCourse(id, name, teacher, classroom);150
151 service.update(course);152 req.setAttribute(“message”, “修改成功”);153 req.getRequestDispatcher(“CourseServlet?method=list”).forward(req,resp);154 }155
156 /**
157 * 查找158 *@paramreq159 *@paramresp160 *@throwsServletException161 */
162 private void search(HttpServletRequest req, HttpServletResponse resp) throwsIOException, ServletException{163 req.setCharacterEncoding(“utf-8”);164 String name = req.getParameter(“name”);165 String teacher = req.getParameter(“teacher”);166 String classroom = req.getParameter(“classroom”);167 List courses =service.search(name, teacher, classroom);168 req.setAttribute(“courses”, courses);169 req.getRequestDispatcher(“searchlist.jsp”).forward(req,resp);170 }171 }
1 //DBUtil.java
2 packagecom.hjf.util;3
4 importjava.sql.Connection;5 importjava.sql.DriverManager;6 importjava.sql.PreparedStatement;7 importjava.sql.ResultSet;8 importjava.sql.SQLException;9 importjava.sql.Statement;10
11 /**
12 * 数据库连接工具13 *@authorHu14 *15 */
16 public classDBUtil {17
18 public static String db_url = “jdbc:mysql://localhost:3306/course”;19 public static String db_user = “root”;20 public static String db_pass = “root”;21
22 public staticConnection getConn () {23 Connection conn = null;24
25 try{26 Class.forName(“com.mysql.jdbc.Driver”);//加载驱动
27 conn =DriverManager.getConnection(db_url, db_user, db_pass);28 } catch(Exception e) {29 e.printStackTrace();30 }31
32 returnconn;33 }34
35 /**
36 * 关闭连接37 *@paramstate38 *@paramconn39 */
40 public static voidclose (Statement state, Connection conn) {41 if (state != null) {42 try{43 state.close();44 } catch(SQLException e) {45 e.printStackTrace();46 }47 }48
49 if (conn != null) {50 try{51 conn.close();52 } catch(SQLException e) {53 e.printStackTrace();54 }55 }56 }57
58 public static voidclose (ResultSet rs, Statement state, Connection conn) {59 if (rs != null) {60 try{61 rs.close();62 } catch(SQLException e) {63 e.printStackTrace();64 }65 }66
67 if (state != null) {68 try{69 state.close();70 } catch(SQLException e) {71 e.printStackTrace();72 }73 }74
75 if (conn != null) {76 try{77 conn.close();78 } catch(SQLException e) {79 e.printStackTrace();80 }81 }82 }83
84 public static void main(String[] args) throwsSQLException {85 Connection conn =getConn();86 PreparedStatement pstmt = null;87 ResultSet rs = null;88 String sql =”select * from course”;89 pstmt =conn.prepareStatement(sql);90 rs =pstmt.executeQuery();91 if(rs.next()){92 System.out.println(“空”);93 }else{94 System.out.println(“不空”);95 }96 }97 }
1 //add.jsp
2
3 pageEncoding=”UTF-8″%>
4
5
6
7
8
Insert title here
9
10
11
12
13 Object message = request.getAttribute(“message”);14 if(message!=null && !””.equals(message)){15
16 %>
17
18 alert(“”);19
20
21
39
40 function check() {41 var name = document.getElementById(“name”);;42 var teacher = document.getElementById(“teacher”);43 var classroom = document.getElementById(“classroom”);44
45 //非空
46 if(name.value == ”) {47 alert(‘课程名称为空’);48 name.focus();49 return false;50 }51 if(teacher.value == ”) {52 alert(‘教师为空’);53 teacher.focus();54 return false;55 }56 if(classroom.value == ”) {57 alert(‘上课地点为空’);58 classroom.focus();59 return false;60 }61
62 //教师
63 if(teacher.value != ‘王建民’ && teacher.value != ‘王辉’ && teacher.value != ‘刘丹’ && teacher.value != ‘刘立嘉’ && teacher.value != ‘杨子光’){64 alert(‘教师名称错误’);65 return false;66 }67
68 //教室
69 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) {70 alert(‘上课地点错误’);71 return false;72 }73 }74
75
76
1 //del.jsp
2
3 pageEncoding=”UTF-8″%>
4
5
6
7
8
Insert title here
9
10
11
12
13 Object message = request.getAttribute(“message”);14 if(message!=null && !””.equals(message)){15
16 %>
17
18 alert(“”);19
20
21
33
34 function check() {35 var name = document.getElementById(“classname”);;36
37 //非空
38 if(name.value == ”) {39 alert(‘课程名称为空’);40 name.focus();41 return false;42 }43 }44
45
46
1 //detail.jap
2
3 pageEncoding=”UTF-8″%>
4
5
6
7
8
Insert title here
9
10 .a{11 margin-top: 20px;12 }13 .b{14 font-size: 20px;15 width: 160px;16 color: white;17 background-color: greenyellow;18 }19 .tb, td {20 border: 1px solid black;21 font-size: 22px;22 }23
24
25
26
27
课程信息删除
28
返回主页
29
30
31
课程名称
32
${list.classname}
33
34
35
任课教师
36
${list.teachnamr}
37
38
39
上课地点
40
${list.site}
41
42
43
44
删 除
45
46
47
48 function check() {49 if (confirm(“真的要删除吗?”)){50 return true;51 }else{52 return false;53 }54 }55
56
57
1 //detail2.jsp
2
3 pageEncoding=”UTF-8″%>
4
5
6
7
8
Insert title here
9
10
11
12
13 Object message = request.getAttribute(“message”);14 if(message!=null && !””.equals(message)){15
16 %>
17
18 alert(“”);19
20
21
40
41 function check() {42 var name = document.getElementById(“name”);;43 var teacher = document.getElementById(“teacher”);44 var classroom = document.getElementById(“classroom”);45
46 //非空
47 if(name.value == ”) {48 alert(‘课程名称为空’);49 name.focus();50 return false;51 }52 if(teacher.value == ”) {53 alert(‘教师为空’);54 teacher.focus();55 return false;56 }57 if(classroom.value == ”) {58 alert(‘上课地点为空’);59 classroom.focus();60 return false;61 }62
63 //教师
64 if(teacher.value != ‘王建民’ && teacher.value != ‘王辉’ && teacher.value != ‘刘丹’ && teacher.value != ‘刘立嘉’ && teacher.value != ‘杨子光’){65 alert(‘教师名称错误’);66 return false;67 }68
69 //教室
70 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) {71 alert(‘上课地点错误’);72 return false;73 }74 }75
76
77
1
2 pageEncoding=”UTF-8″%>
3
4
5
6
7
首页
8
9
10
11
26
27
1 //list.jsp
2
3 pageEncoding=”UTF-8″%>
4
5
6
7
8
9
Insert title here
10
11
12
13
14 Object message = request.getAttribute(“message”);15 if(message!=null && !””.equals(message)){16
17 %>
18
19 alert(“”);20
21
22
23
课程信息列表
24
返回主页
25
26
27
id
28
课程名称
29
任课教师
30
上课地点
31
操作
32
33
34
35
${item.id}
36
${item.name}
37
${item.teacher}
38
${item.classroom}
39
40
41
42
43
44
45
1 //search.jsp
2
3 pageEncoding=”UTF-8″%>
4
5
6
7
8
Insert title here
9
10
11
12
30
31 function check() {32 var name = document.getElementById(“name”);;33 var teacher = document.getElementById(“teacher”);34 var classroom = document.getElementById(“classroom”);35
36 //非空
37 if(name.value == ” && teacher.value == ” && classroom.value == ”) {38 alert(‘请填写一个条件’);39 return false;40 }41 }42
43
44
1 //searchlist.jsp
2
3 pageEncoding=”UTF-8″%>
4
5
6
7
8
9
Insert title here
10
11
12
13
14
课程信息列表
15
返回主页
16
17
18
id
19
课程名称
20
任课教师
21
上课地点
22
23
24
25
26
${item.id}
27
${item.name}
28
${item.teacher}
29
${item.classroom}
30
31
32
33
34
35
应该就可以运行了,中间没有写的就是tomcat的安装了,大家可以在网上找一下,比较简单。