“商苑面馆:Java 网页版之 Dao层”的版本间的差异
来自CloudWiki
(创建页面,内容为“==Model层的作用== *M即model模型是指模型表示业务规则。在MVC的三个部件中,模型拥有最多的处理任务。被模型返回的数据是中…”) |
|||
第1行: | 第1行: | ||
− | == | + | ==Dao层== |
− | |||
− | |||
[[文件:Java8-1.png]] | [[文件:Java8-1.png]] | ||
− | |||
− | |||
+ | ==前期准备== | ||
+ | 在entity包下建立User类: | ||
− | + | <nowiki>package entity; | |
− | |||
− | |||
− | + | public class User { | |
− | + | //成员变量 | |
− | + | public String name; | |
− | // | + | public String password; |
− | public | + | |
− | + | //构造方法 | |
− | / | + | public User(){ |
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | public | ||
− | |||
} | } | ||
− | + | public User(String name,String password ){ | |
− | + | ||
− | = | + | this.name=name; |
− | + | ||
− | + | this.password=password; | |
− | |||
− | |||
− | |||
− | + | } | |
− | + | //读方法 | |
− | + | public String getname(){ | |
− | + | ||
− | + | return this.name; | |
− | + | } | |
− | + | ||
− | + | ||
− | + | //写方法 | |
− | + | public boolean setName(String s){ | |
− | + | this.name = s; | |
− | + | return true; | |
− | + | } | |
− | + | //获取用户密码 | |
− | + | public String getPwd(){ | |
− | + | ||
− | + | return this.password; | |
− | + | } | |
− | + | ||
− | + | ||
− | + | //写方法 | |
− | + | public boolean setPwd(String p){ | |
− | + | this.password = p; | |
− | + | return true; | |
− | + | } | |
− | + | }</nowiki> | |
− | + | ==实现步骤== | |
− | |||
− | |||
− | |||
− | |||
− | === | + | ===建立接口UserDao=== |
− | + | 在包dao下建立抽象接口UserDao | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <nowiki>package dao; | |
− | |||
− | |||
− | + | import entity.User; | |
+ | public interface UserDao { | ||
+ | public boolean verify(String name, String pwd)throws Exception ;//验证用户名和密码是否一致 | ||
+ | |||
+ | |||
+ | }</nowiki> | ||
+ | ===建立接口的实现类=== | ||
+ | 接口实现类可以用多种方式实现,ArrayList,LinkedList, MySQL等 | ||
− | + | 这里我们用查询MySQL实现 | |
− | + | ====接口实现类UserDaoSQL==== | |
+ | <nowiki>package dao; | ||
− | + | import java.sql.Connection; | |
− | + | import java.sql.ResultSet; | |
− | + | import java.sql.SQLException; | |
− | import | + | import java.sql.Statement; |
− | import | + | |
− | + | // 调用数据库,进行查询用户名和密码 | |
− | + | public class UserDaoSQL implements UserDao{ | |
− | + | public boolean verify(String username, String password) throws Exception { | |
− | + | boolean returnValue = false; | |
− | + | String sql = "SELECT * FROM user"; | |
− | + | Connection conn = null; | |
− | + | Statement stmt = null; | |
− | + | ResultSet rs = null; | |
− | + | ||
− | public | + | try { |
− | + | conn = DBConnection.getConnection(); | |
− | + | stmt = conn.createStatement(); | |
− | + | rs = stmt.executeQuery(sql); | |
− | + | while (rs.next()) { | |
− | + | String userName = rs.getString("username"); | |
− | + | String passWord = rs.getString("password"); | |
− | + | if (userName.equals(username) && passWord.equals(password)) { | |
− | + | // 如果用户名和密码都和数据库的一样,就返回true | |
− | + | returnValue = true; | |
− | + | break; | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
− | + | } catch (ClassNotFoundException e) { | |
− | + | e.printStackTrace(); | |
− | + | } catch (SQLException e) { | |
− | + | e.printStackTrace(); | |
− | + | } | |
− | return | + | return returnValue; |
+ | |||
} | } | ||
− | + | } | |
− | + | ||
− | + | </nowiki> | |
− | + | ===编写测试代码=== | |
− | + | 在UserDaoSQL类的main方法中编写测试代码: | |
− | + | ||
− | + | <nowiki>public static void main(String[] args) throws Exception{ | |
− | + | //本类的测试代码 | |
− | }</nowiki> | + | // TODO Auto-generated method stub |
+ | UserDao userSet= new UserDaoSQL(); | ||
+ | |||
+ | |||
+ | if(userSet.verify("maxin", "000000")){ | ||
+ | System.out.println("密码正确"); | ||
+ | }else{ | ||
+ | System.out.println("密码错误"); | ||
+ | } | ||
+ | }</nowiki> | ||
+ | |||
+ | [[文件:java7-35.png]] |
2019年5月2日 (四) 04:06的版本
Dao层
前期准备
在entity包下建立User类:
package entity; public class User { //成员变量 public String name; public String password; //构造方法 public User(){ } public User(String name,String password ){ this.name=name; this.password=password; } //读方法 public String getname(){ return this.name; } //写方法 public boolean setName(String s){ this.name = s; return true; } //获取用户密码 public String getPwd(){ return this.password; } //写方法 public boolean setPwd(String p){ this.password = p; return true; } }
实现步骤
建立接口UserDao
在包dao下建立抽象接口UserDao
package dao; import entity.User; public interface UserDao { public boolean verify(String name, String pwd)throws Exception ;//验证用户名和密码是否一致 }
建立接口的实现类
接口实现类可以用多种方式实现,ArrayList,LinkedList, MySQL等
这里我们用查询MySQL实现
接口实现类UserDaoSQL
package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; // 调用数据库,进行查询用户名和密码 public class UserDaoSQL implements UserDao{ public boolean verify(String username, String password) throws Exception { boolean returnValue = false; String sql = "SELECT * FROM user"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = DBConnection.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { String userName = rs.getString("username"); String passWord = rs.getString("password"); if (userName.equals(username) && passWord.equals(password)) { // 如果用户名和密码都和数据库的一样,就返回true returnValue = true; break; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return returnValue; } }
编写测试代码
在UserDaoSQL类的main方法中编写测试代码:
public static void main(String[] args) throws Exception{ //本类的测试代码 // TODO Auto-generated method stub UserDao userSet= new UserDaoSQL(); if(userSet.verify("maxin", "000000")){ System.out.println("密码正确"); }else{ System.out.println("密码错误"); } }