第一步,创建Maven工程,
第二步,在resoureces下面创建driver.properties文件。
第三步编写代码
public class BaseDAO { private static String URL; private static String USERNAME; private static String PASSWORD; private static String DRIVER; private static Connection con; private PreparedStatement pstat; private ResultSet re; static { loadDriver(); } private static void loadDriver(){ //加载文件位置 try { //防止中文乱码 String path=URLDecoder.decode(BaseDAO.class.getResource("/driver.properties").getPath(),"utf-8"); //根据文件位置加载配置文件内容 Properties prop = new Properties(); prop.load(new FileInputStream(path)); //跟据文件中Key的名称获取数据并填充被雷的四个属性 DRIVER= prop.getProperty("driver"); URL= prop.getProperty("url"); USERNAME= prop.getProperty("username"); PASSWORD= prop.getProperty("password"); } catch (IOException e) { e.printStackTrace(); } } protected BaseDAO(){} /* * 获取数据库链接 * @return * */ public static Connection getConnection(){ if (con==null){ try { Class.forName(DRIVER); con= DriverManager.getConnection(URL,USERNAME,PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } return con; } //针对增删改的方法 public void update(String sql,Object [] params){ try { pstat = getConnection().prepareStatement(sql); for (int i = 0; i < params.length; i++) { pstat.setObject(i+1,params[i]); } } catch (SQLException e) { e.printStackTrace(); } } /* * 针对用户查询的方法 * * */ public ResultSet query(String sql, Object []params){ try { pstat= getConnection().prepareStatement(sql); for (int i = 0; i < params.length; i++) { pstat.setObject(i+1,params[i]); } re=pstat.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return re; } }
第四步可以创建一个数据库中表的实体类
package com.zbitedu.mymysql.entity; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data //get/set方法 @NoArgsConstructor //无参构造器 @AllArgsConstructor //全参构造器 @Builder //语法糖格式构建实体对象 public class Userinfos { private Long userid; private String username; private Date birthday; }
第五步 新建一个类继承BaseDAO,封装刚才的实体类对象,封装成集合保存数据
package com.zbitedu.mymysql.dao; import com.zbitedu.mymysql.entity.Userinfos; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class UserinfosDAO extends BaseDAO { //将 resultset 转为list集合 public List<Userinfos> change(String sql,Object[]params){ ResultSet rs=query(sql,params); List<Userinfos> users=new ArrayList<>(); try { while (rs.next()){ Userinfos us =Userinfos.builder() .userid(rs.getLong("userid")) .username(rs.getString("username")) .birthday(rs.getDate("birthday")).build(); users.add(us); } } catch (SQLException e) { e.printStackTrace(); } return users; } public List<Userinfos> findAll(){ String sql="select * from userinfos"; return change(sql,new Object[]{}); } public Userinfos findById(Long id){ String sql ="select * from userinfos where userid=?"; List<Userinfos> uss=change(sql,new Object[]{id}); return uss.isEmpty()?null:uss.get(0); } public static void main(String[] args) { System.out.println(new UserinfosDAO().findAll()); } }