2020.10.28收获
课程管理系统的增删改查
1、连接数据库
要连接数据库,首先得建立一个数据库表,建立表头信息。然后在eclipse里进行操作。
在util 层下建立 DBUtil.java 文件,将数据库连接。主要分为四步:
- 加载驱动程序
- 数据库连接字符串
- 数据库登录名和密码
- 关闭数据库
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { e.printStackTrace(); } String user = "root"; String password = "liutianwen0613"; String url = "jdbc:mysql://localhost:3306/login? ?characterEncoding=utf-8&useSSL=true"; Connection connection = null; try { connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { e.printStackTrace(); } return connection; } public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
2、在 bean 层下建 infor.java 文件,里面创建对象,属性为 private。然后分别建立 set 和 get 函数,属性为 public(可通过右击--源码快速生成)
package bean; public class infor{ private String name; private String teacher; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public infor() {} public infor(String name,String teacher,String address) { this.name = name; this.teacher = teacher; this.address = address; } }
注意:必须要有一个空的构造函数
3、在dao层下建立studentdao.java文件,专门写对数据库的操作,在里面写增删改查的函数,根据功能来写不同的函数,每个函数都得调用数据库的连接语句:
Connection connection = DBUtil.getConnection();
用到对数据库操作的语句:
String sql = "select count(*) from class1 where name = ?";//选择语句
sql = "insert into class1(name,teacher,address) value (?,?,?)";//插入语句
语句有很多种,基本用法也不尽相同。
同时还得新定义这两个语句:
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
然后就是写函数体。根据不同的要求写不同的函数。
package dao; import javax.swing.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import util.DBUtil; import bean.infor; public class studentdao { public void add1(String name, String teacher,String address) { Connection connection = DBUtil.getConnection(); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { String sql = "insert into class1(name,teacher,addess) value (?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, name); preparedStatement.setString(2, teacher); preparedStatement.setString(3, address); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } } public void update(String name, String newname,String newteacher,String newaddress) { Connection connection = DBUtil.getConnection(); String sql = "select count(*) from class1 where name = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, name); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { if (resultSet.getInt(1) > 0) { System.out.println("开始修改"); sql = "update class1 set name=?,teacher=?,address=? where name=?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, newname); preparedStatement.setString(2, newteacher); preparedStatement.setString(3, newaddress); preparedStatement.setString(4, name); } preparedStatement.executeUpdate(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } } public void delete(String username) { Connection connection = DBUtil.getConnection(); String sql = "delete from class1 where name = ?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, username); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(preparedStatement); DBUtil.close(connection); } } public int add(String name,String teacher,String address) { Connection connection = DBUtil.getConnection(); String sql = "select count(*) from class1 where name = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, name); resultSet = preparedStatement.executeQuery(); System.out.println("hello"); while(resultSet.next()) { if (resultSet.getInt(1) > 0) { return 0; } else { sql = "insert into class1(name,teacher,address) value (?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, name); preparedStatement.setString(2, teacher); preparedStatement.setString(3, address); preparedStatement.executeUpdate(); return 1; } } } catch (SQLException e) { // TODO Auto-generated catch block //e.printStackTrace(); e.getMessage(); }finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return 0; } public int load(String name) { // TODO Auto-generated method stub Connection connection = DBUtil.getConnection(); String sql = "select * from class1 where name = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, name); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.println("课程名称:" + resultSet.getString("name")); System.out.println("任课教师:" + resultSet.getString("teacher")); System.out.println("上课地点:" + resultSet.getString("address")); return 1; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return 0; } public String load_del(String name) { // TODO Auto-generated method stub Connection connection = DBUtil.getConnection(); String sql = "select * from class1 where name = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, name); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.println("课程名称:" + resultSet.getString("name")); System.out.println("任课教师:" + resultSet.getString("teacher")); System.out.println("上课地点:" + resultSet.getString("address")); return ("课程名称:" + resultSet.getString("name")+" 任课教师:" + resultSet.getString("teacher")+" 上课地点:" + resultSet.getString("address")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return "课程不存在!"; } public String load_delete(String name) { // TODO Auto-generated method stub Connection connection = DBUtil.getConnection(); //System.out.println(111); PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { String sql = "select * from class1 where name like '%"+name+"%'"; preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.println("课程名称:" + resultSet.getString("name")); System.out.println("任课教师:" + resultSet.getString("teacher")); System.out.println("上课地点:" + resultSet.getString("address")); return ("课程名称:" + resultSet.getString("name")+" 任课教师:" + resultSet.getString("teacher")+" 上课地点:" + resultSet.getString("address")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return "课程不存在!"; } public List<infor> loadlist(String name,String teacher,String address) { // TODO Auto-generated method stub String sql = "select * from class1 where "; if (name != "") { sql += "name like '%" + name + "%'"; } if (teacher != "") { sql += "teacher like '%" + teacher + "%'"; } if (address != "") { sql += "classroom like '%" + address + "%'"; } List<infor> list = new ArrayList<>(); Connection connection = DBUtil.getConnection(); //System.out.println(111); infor bean = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { //String sql = "select * from class1 where name like '%"+name+"%'"; preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { String name2 = resultSet.getString("name"); String teacher2 = resultSet.getString("teacher"); String address2 = resultSet.getString("address"); bean = new infor(name2,teacher2,address2); list.add(bean); System.out.println("课程名称:" + resultSet.getString("name")); System.out.println("任课教师:" + resultSet.getString("teacher")); System.out.println("上课地点:" + resultSet.getString("address")); //return ("课程名称:" + resultSet.getString("name")+" 任课教师:" + resultSet.getString("teacher")+" 上课地点:" + resultSet.getString("address")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return list; } }
4、搭建界面
主要用的是jsp,先建立一个主页,展示操作,再分别建立增删改查的jsp:
然后在jsp里面进行传值,调用Java文件的函数,进而操作数据库。
这些jsp文件里并不是都是显示页面,有一些只是进行判断的页面,调用函数进行操作数据库。
这是主页面,分成了上左右三块,左边菜单栏,上边是名称,右边是各种操作界面及结果显示。
代码:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>主页</title> 8 <link rel="stylesheet" href="css/page.css" /> 9 <script type="text/javascript" src="js/jquery.min.js"></script> 10 <script type="text/javascript" src="js/index.js"></script> 11 </head> 12 13 <frameset rows="20%,*"> 14 <frame class="top" src="top.jsp"> 15 <frameset cols="20%,*"> 16 <frame src="main_left.jsp"> 17 <frame src="main_right.jsp" name="main_right"> 18 </frameset> 19 </frameset> 20 <body> 21 22 </body> 23 </html>
其他分界面以添加为例
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="com.jaovo.msg.dao.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <form method="post" action="inputstuinfo_result.jsp"> 12 13 <div align="center"> 14 <h5>课程名称:<input name="input_name" type="text" placeholder="请输课程名称"></h5> 15 <h5>任课教师:<input name="input_teacher" type="text" placeholder="请输任课教师"></h5> 16 <h5>上课地点:<input name="input_address" type="text" placeholder="请输上课地点"></h5> 17 <input name="submit" type="submit" > 18 </div> 19 </form> 20 </body> 21 </html>