DAO意思是数据库、访问、对象。有了前后端思想。
需要下载mysql,并下载链接自己设置好密码。可以在navicat等软件链接打开。
材料是jc0122.sql,里面的admin_info是要操作的表。不像上一节需要把数据库文件放到java目录里。数据库只要在本地即可。
一、简单测试链接读取
要读取admin_info所有的数据,弄一个对象。保证后续输出。
AdminInfo
package S4.pojo;
import java.util.Date;
public class AdminInfo {
private Integer adminId;
private String adminName;
private String adminPwd;
private Integer roleId;
private Integer adminState;
private Date adminCtime;
private Date adminMtime;
public AdminInfo() {
}
public AdminInfo(Integer adminId, String adminName, String adminPwd, Integer roleId, Integer adminState, Date adminCtime, Date adminMtime) {
this.adminId = adminId;
this.adminName = adminName;
this.adminPwd = adminPwd;
this.roleId = roleId;
this.adminState = adminState;
this.adminCtime = adminCtime;
this.adminMtime = adminMtime;
}
public Integer getAdminId() {
return adminId;
}
public void setAdminId(Integer adminId) {
this.adminId = adminId;
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminPwd() {
return adminPwd;
}
public void setAdminPwd(String adminPwd) {
this.adminPwd = adminPwd;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public Integer getAdminState() {
return adminState;
}
public void setAdminState(Integer adminState) {
this.adminState = adminState;
}
public Date getAdminCtime() {
return adminCtime;
}
public void setAdminCtime(Date adminCtime) {
this.adminCtime = adminCtime;
}
public Date getAdminMtime() {
return adminMtime;
}
public void setAdminMtime(Date adminMtime) {
this.adminMtime = adminMtime;
}
@Override
public String toString() {
return "AdminInfo{" +
"adminId=" + adminId +
", adminName='" + adminName + '\'' +
", adminPwd='" + adminPwd + '\'' +
", roleId=" + roleId +
", adminState=" + adminState +
", adminCtime=" + adminCtime +
", adminMtime=" + adminMtime +
'}';
}
}
Test:注意localhost、pwd密码,密码是自己修改的。uri后面的jc0122是.sql文件,可能有许多表格。
package S4.test;
import S4.pojo.AdminInfo;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");// jdbc驱动
String uri = "jdbc:mysql://localhost:3306/jc0122?useUnicode=ture&serverTimezone=UTC";
String userid = "root";
String pwd = "123456";
Connection connection = DriverManager.getConnection(uri,userid,pwd);
System.out.println("connection:"+connection);
//开始查询
String sql = "select * from admin_info ";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet set = ps.executeQuery();
List<AdminInfo> list = new ArrayList<>();
while (set.next()){
AdminInfo adminInfo = new AdminInfo();
//匹配java对象的属性和数据库对象的字段一一对应起来
adminInfo.setAdminId(set.getInt("admin_id"));
adminInfo.setAdminName(set.getString("admin_name"));
adminInfo.setAdminPwd(set.getString("admin_pwd"));
adminInfo.setAdminCtime(set.getDate("admin_ctime"));
adminInfo.setAdminMtime(set.getDate("admin_mtime"));
adminInfo.setAdminState(set.getInt("admin_state"));
adminInfo.setRoleId(set.getInt("role_id"));
System.out.println(adminInfo);
list.add(adminInfo);
}
//后面就是展示到UI上面去
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
如果链接成功,会提示connection,并根据需求读出数据。
二、根据id、名字读取所有数据;分页查询
AdminInfo还是上面那个
创建Interface类,AdminDao。按要求声明一下。
package S4.dao;
import S4.pojo.AdminInfo;
import java.util.List;
public interface AdminDao {
/**
* 通过账号密码查询admin对象
* @param name 账号
* @param pwd 密码
* @return admin对象
*/
AdminInfo selectByNameAndPwd(String name,String pwd);
/**
* 分页查询用户
* @param start 起始数
* @param limit 条数
* @return admin对象集合
*/
List<AdminInfo> selectByPage(Integer start ,Integer limit);
}
AdminDaolmpl中把函数写完整,注意要链接mysql
package S4.dao;
import S4.pojo.AdminInfo;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class AdminDaoImpl implements AdminDao {
@Override
public AdminInfo selectByNameAndPwd(String name, String pwd) {
AdminInfo result = null;
try {
Class.forName("com.mysql.jdbc.Driver");// jdbc驱动
String uri = "jdbc:mysql://localhost:3306/jc0122?useUnicode=ture&serverTimezone=UTC";
String userid = "root";
String upwd = "123456";
Connection connection = DriverManager.getConnection(uri, userid, upwd);
System.out.println("connection:" + connection);
//开始查询
String sql = "SELECT * FROM admin_info WHERE admin_name = ? AND admin_pwd = ?"; //使用?对传入的参数进行占位
PreparedStatement ps = connection.prepareStatement(sql);
//参数中 1 表示第一个? name 表示?对应的值
ps.setObject(1, name);
ps.setObject(2, pwd);
ResultSet set = ps.executeQuery();
while (set.next()) {
AdminInfo adminInfo = new AdminInfo();
//匹配java对象的属性和数据库对象的字段一一对应起来
adminInfo.setAdminId(set.getInt("admin_id"));
adminInfo.setAdminName(set.getString("admin_name"));
adminInfo.setAdminPwd(set.getString("admin_pwd"));
adminInfo.setAdminCtime(set.getDate("admin_ctime"));
adminInfo.setAdminMtime(set.getDate("admin_mtime"));
adminInfo.setAdminState(set.getInt("admin_state"));
adminInfo.setRoleId(set.getInt("role_id"));
result = adminInfo;
break;
}
//后面就是展示到UI上面去
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
@Override
public List<AdminInfo> selectByPage(Integer start, Integer limit) {
try {
Class.forName("com.mysql.jdbc.Driver");// jdbc驱动
String uri = "jdbc:mysql://localhost:3306/jc0122?useUnicode=ture&serverTimezone=UTC";
String userid = "root";
String pwd = "123456";
Connection connection = DriverManager.getConnection(uri,userid,pwd);
//开始查询
String sql = "select * from admin_info LIMIT ? , ? ";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setObject(1,start);
ps.setObject(2,limit);
ResultSet set = ps.executeQuery();
List<AdminInfo> list = new ArrayList<>();
while (set.next()){
AdminInfo adminInfo = new AdminInfo();
//匹配java对象的属性和数据库对象的字段一一对应起来
adminInfo.setAdminId(set.getInt("admin_id"));
adminInfo.setAdminName(set.getString("admin_name"));
adminInfo.setAdminPwd(set.getString("admin_pwd"));
adminInfo.setAdminCtime(set.getDate("admin_ctime"));
adminInfo.setAdminMtime(set.getDate("admin_mtime"));
adminInfo.setAdminState(set.getInt("admin_state"));
adminInfo.setRoleId(set.getInt("role_id"));
list.add(adminInfo);
}
return list;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
最后main函数DaoTest
package S4.test;
import S4.dao.AdminDao;
import S4.dao.AdminDaoImpl;
import S4.pojo.AdminInfo;
import java.util.List;
public class DaoTest {
public static void main(String[] args) {
AdminDao adminDao = new AdminDaoImpl(); // 接口类new出实现类
AdminInfo adminInfo = adminDao.selectByNameAndPwd("zhangsan","123456");
System.out.println(adminInfo);
List<AdminInfo> list = adminDao.selectByPage(1,10);
System.out.println(list);
}
}
结果是按账号密码输出了张三,按分页查找(注意起始是0,这里是1),输出了李四。