实习第一天-----配置环境idea+mysql+jdk以及简单的jdbc项目书写
第一周实习是基于ssm的用户管理系统的编写。
第一天是有关安装环境
第二天搭建ssm项目
1.JDBC是什么?(复习)
1)加载驱动;2)创建连接;3)写sql;4)statement对象;5)执行sql得到的结果;6)处理结果集;7)关闭资源
2.环境配置 jdk1.8+mysql+idea
1)win+r打开cmd,输入Java -version查看Java版本
如果版本不一样更新一下版本
2)mysql安装,网上教程很多,此处因为有老师发的免安装,我们可直接采用老师的;
安装SQLyog连接数据库
3)idea,**版
3.创建Java-webapp项目
1)创建文件结构如下:
2)打开SQLyog,配置如下
3)创建数据库:
4)注意此处需要导入包,不然不能正常运行
4具体实现代码如下
1)bean.User//实体类 与数据库对接 构造方法 属性 getset tostring
public class User {
public User() {
} public User(int id,String username,String password){ this.id = id; this.username = username; this.password = password; } private int id; private String username; private String password; //不能直接对私有对象进行操作 //法1 get set //法2 构造方法 public int getId() { return id; } public void setId(int id) { this.id = id; } 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 "User(" + "id="+id+ ", username='"+username+'\''+ ", password='"+password+'\''+ '}'; } }
2)dao-userDao //持久层 对数据库操作 crud
public class UserDao {//数据库的增删改查
//查询所有的用户 public List<User> findAll() throws SQLException, ClassNotFoundException { List<User> userList = new ArrayList<>();//新建集合 Connection connection = DBUtil.getConnection(); System.out.println("获得连接成功"); //查询 String sql = "select * from tb_user"; PreparedStatement statement = connection.prepareStatement(sql); ResultSet resultSet = statement.executeQuery(); while(resultSet.next()){ User user = new User(); int id = resultSet.getInt(1); String username = resultSet.getString(2); String password = resultSet.getString(3); user.setId(id); user.setUsername(username); user.setPassword(password); //System.out.println(id+"---"+username+"---"+password); userList.add(user);//将数据传入 //System.out.println(user); } DBUtil.close(resultSet,statement,connection); return userList; } //根据id删除 public void deleteById(int id) throws SQLException, ClassNotFoundException { //连接数据库 Connection connection = DBUtil.getConnection(); System.out.println(""); String sql = "delete from tb_user where id=?"; PreparedStatement statement =connection.prepareStatement(sql); statement.setInt(1,id); statement.executeUpdate(); DBUtil.close(null,statement,connection); } public int login(String username,String password) throws SQLException, ClassNotFoundException { Connection connection = DBUtil.getConnection(); Statement state =null; ResultSet rs = null; int flag=0; try { String sql = "select * from user where username = '"+username+"'"; state = connection.createStatement(); rs=state.executeQuery(sql); if(rs.next()) { if(rs.getString("password").equals(password)) { flag=1; } } } catch(Exception e) { e.printStackTrace(); } DBUtil.close(rs, state, connection); return flag; } public User findUserByName(String username) throws SQLException, ClassNotFoundException { Connection connection = DBUtil.getConnection(); System.out.println("获得连接成功"); //Connection connection = DBUtil.getConnection(); System.out.println("获得连接成功"); String sql = "select * from tb_user where username=?"; PreparedStatement statement =connection.prepareStatement(sql); statement.setString(1,username); ResultSet resultSet = statement.executeQuery(); User user=null; while(resultSet.next()){ user=new User(); int id=resultSet.getInt(1); String username1 = resultSet.getString(2); String password = resultSet.getString(3); user.setId(id); user.setUsername(username); user.setPassword(resultSet.getString(password)); } DBUtil.close(resultSet,statement,connection); return user; }
3)service-UserService //业务层 处理i请求
public class UserService { public boolean login(String username,String password) { UserDao userDao = new UserDao(); User user = null; try{ user = userDao.findUserByName(username); }catch(SQLException throwables){ throwables.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); } if(user!=null&&user.getPassword().equals(password)){ return true; }else{ return false; } } }
4)a..servlet-LoginServlet //控制层 不能直接调用dao 只能调用service
public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); UserService userService = new UserService(); Boolean flag = userService.login(username,password); if(flag){ resp.getWriter().write("ok"); //req.getRequestDispatcher("/index.jsp").forward(req,resp); }else { resp.getWriter().write("failure"); } } }
4)b..UserServlet
public class UserServlet extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException { req.setCharacterEncoding("utf-8");//设置字符集 String method = req.getParameter("method");//获取jsp界面的method参数来判断执行什么方法 if(method.equals("login")) { try { login(req,resp);//登录方法 } catch (SQLException throwables) { throwables.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } protected void login(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException, SQLException, ClassNotFoundException { req.setCharacterEncoding("utf-8"); String username = req.getParameter("username1"); String password = req.getParameter("password1");//获取用户名密码 UserDao userdao = new UserDao(); int flag = userdao.login(username, password);//执行Userdao里面的login方法判断登录的用户名密码是否正确 if(flag==1) { System.out.println("登录成功!"); resp.sendRedirect(req.getContextPath()+"/loginsuccessful.jsp");//登录成功跳转到主界面 } else { System.out.println("登录失败!"); resp.sendRedirect(req.getContextPath()+"/login.jsp"); } } }
5) util-DBUtil //数据库的连接与关闭
public class DBUtil { //连接数据库 public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/whlg","root","123456"); return connection; } //关闭数据库 public static void close(ResultSet resultSet, Statement statement, Connection connection) throws SQLException { if(resultSet!=null){ resultSet.close(); } if(statement!=null){ statement.close(); } if(connection!=null){ connection.close(); } } }
6)web.xml
<servlet> <servlet-name>hello</servlet-name> <servlet-class>com.servlet.UserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>com.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
7)index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆界面</title> </head> <body> <!-- --> <h1 style="color:#13b353">登录</h1> <from action="/login" method="post"> name:<input name="username" type ="text"> password:<input name="password" type ="password"> <input type="submit" value="login"> </from> </body> </html>
8)login.isp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/servlet/Userservlet?method=login" method="post"> <h1>登录 界面</h1> <div> <input type="text" placeholder="登录名" required="" id="username" name="username1"/> </div> <div> <input type="password" placeholder="密码" required="" id="password" name="password1" /> </div> <div> <input type="submit" value="登 录" /> </div> </form> </body> </html>
9)logincheck.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); UserDao userDao = new UserDao(); //取数值 String username1 = (String) request.getParameter("username"); String password1 = (String) request.getParameter("password"); // String sql= "select username from tb_user where username="+"'"+username1+"'";//定义一个查询语句 //String sql1="select password from tb_user where password="+"'"+password1+"'"; String sql= "select username from tb_user where username=?";//定义一个查询语句 String sql1="select password from tb_user where password=?"; Connection connection = DBUtil.getConnection(); PreparedStatement statement = connection.prepareStatement(sql); PreparedStatement statement2 = connection.prepareStatement(sql1); ResultSet resultSet = statement.executeQuery(); ResultSet resultSet1 = statement2.executeQuery(); if(resultSet.next()&&resultSet1.next()){ if(password1.equals(resultSet.getObject("password"))){ System.out.println(" 查询成功!"); response.sendRedirect("loginsuccessful.jsp"); } } else{ //out.print("<script language='javaScript'>alert('用户名或密码错误');</script>"); System.out.println("用户名或密码错误"); response.setHeader("refresh","url=login.jsp"); } %> </body> </html>
10)loginsuccessful.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 这是主页,你已成功登录! </body> </html>
因为今天之前很久没有接触过Java代码了,所以还是很生疏,有了老师的复习讲解与具体实现,自己也慢慢的熟悉了起来。