公文流转系统
这个要求的功能我只实现了了两部分
(1)首先是注册界面插入数据库的操作,首先是一个登陆界面,然后点击注册会跳转的相应的界面
没有经过css的美化,在后期会完善
写好数据之后点击注册就会提交的相应的servelet里面
package com.serve; import java.io.IOException; 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.dao.Connect; import com.dao.register; @WebServlet("/servlet") public class servlet extends HttpServlet { private static final long serialVersionUID = 1L; public servlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); request.setCharacterEncoding("utf-8"); String name=request.getParameter("uname"); String pwd=request.getParameter("upwd"); String zhiwei=request.getParameter("uzhiwei"); if (register.add(name,pwd,zhiwei)>0) { System.out.println("信息添加成功"); response.sendRedirect("dl.jsp"); }else { System.out.println("信息添加失败"); response.sendRedirect("del.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
这个register.add()这个方法是来执行添加操作的一个类
package com.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.mysql.fabric.Response; public class register { public static int add(String name,String pwd,String zhiwei) { Connection conn=Connect.getConn(); Statement st=null; int row=0; try { st=conn.createStatement(); String sql="insert into admin(name,admin,zhiwei) values('"+name+"','"+pwd+"','"+zhiwei+"')"; row=st.executeUpdate(sql); } catch (Exception e) { // TODO: handle exception }finally { Connect.close(conn, st, null); } return row; } public static ResultSet login(String name,String pwd,String zhiwei) { Connection conn=Connect.getConn(); Statement st=null; ResultSet rs=null; int row=0; try { st=conn.createStatement(); String sql="select * from admin where zhiwei like '%" + zhiwei + "%'"; row=st.executeUpdate(sql); if (row>0) { rs=st.executeQuery(sql); }else { rs=null; } } catch (Exception e) { // TODO: handle exception }finally { Connect.close(conn, st, rs); } return rs; } }
其中第二个函数login是在登陆的时候进行校验的的函数
(2)第二部分就是对应的登录时与数据库的内容匹配,在最后执行的时候遇到了问题,说是文件加密的问题。最后在连接数据库时添加了一个useSSL=false就可以了
当然连接数据库的代码我是单独写在一个函数之中的,定义为静态,关闭文件也是,这样就会在后面需要连接的时候直接用类名加方法的名称来调用即可,代码如下
package com.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Connect { public static Connection getConn() { //第一步:加载驱动类,如果有异常则抛出异常或者使用try..catch..语句处理 try { Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e){ e.printStackTrace(); System.out.println("驱动类加载失败"); //这里可能报错:ClassNotFoundException //错误原因:可能是驱动类的名称拼写错误,也可能是jar包没导入 } //第二步:获取连接对象 String url = "jdbc:mysql://localhost:3306/test?useSSL=false"; String username = "root"; String password = "ly0825"; Connection conn = null; try { conn = DriverManager.getConnection(url,username,password); }catch(SQLException e) { e.printStackTrace(); System.out.println("连接对象获取失败"); } //返回一个连接对象 return conn; } public static void close(Connection conn, Statement st,ResultSet rs) { if (rs!=null) { try { rs.close(); rs=null; } catch (Exception e) { e.printStackTrace(); } } if (st!=null) { try { st.close(); st=null; } catch (Exception e) { e.printStackTrace(); } } if (conn!=null) { try { conn.close(); conn=null; } catch (Exception e) { e.printStackTrace(); } } } }
在登陆匹配时是跳转到另外一个servlet里的,通过调用函数register.login来进行判断,如果不成功则会显示弹框失败的原因,这个东西我现在是出现了一点小问题,没有成功,后期会解决。
代码如下:
package com.serve; import java.io.IOException; import java.sql.ResultSet; 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 javax.swing.JFrame; import javax.swing.JOptionPane; import com.dao.register; @WebServlet("/findinfo") public class findinfo extends HttpServlet { private static final long serialVersionUID = 1L; public findinfo() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String name=request.getParameter("uname"); String pwd=request.getParameter("upwd"); String zhiwei=request.getParameter("uzhiwei"); ResultSet rs=register.login(name, pwd, zhiwei); try { if (rs==null) { JOptionPane.showMessageDialog(new JFrame().getContentPane(), "不存在此用户,请重新登录!", "系统信息", JOptionPane.INFORMATION_MESSAGE); response.sendRedirect("dl.jsp"); } } catch (Exception e) { // TODO: handle exception } try { while (rs.next()) { if (rs.getString("name").equals(name)&&rs.getString("admin").equals(pwd)) { JOptionPane.showMessageDialog(new JFrame().getContentPane(), "欢迎您:"+name+"登录成功!", "系统信息", JOptionPane.INFORMATION_MESSAGE); if (zhiwei.equals("部门")) { response.sendRedirect(""); } else if(zhiwei.equals("办公室")){ response.sendRedirect(""); } else if(zhiwei.equals("副厂长")){ response.sendRedirect("fuchangzhang.jsp"); } else if(zhiwei.equals("厂长")){ response.sendRedirect(""); } else if(zhiwei.equals("系统管理")){ response.sendRedirect(""); } } } JOptionPane.showMessageDialog(new JFrame().getContentPane(), "账号或者密码错误,请重新登录!", "系统信息", JOptionPane.INFORMATION_MESSAGE); response.sendRedirect("dl.jsp"); } catch (Exception e) { // TODO: handle exception } response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }