数据访问层 dao 接口
package com.teaching.jsp.dao;import com.teaching.jsp.entity.Brand;import java.util.List;//DAO 的全称是Data Access Objectpublic interface BrandDao {List
dao 操作
对数据库的连接不应该写在此层 , 应该写在层
package com.teaching.web.dao;import com.teaching.web.entity.User;import com.teaching.web.utils.DbUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class UserDaoImpl implements UserDao {@Overridepublic User selectUserByEntity(User user, Connection connection) throws SQLException {String sql="select id,username,password from user where username=? and password=?";User u = null;PreparedStatement ps = connection.prepareStatement(sql);ps.setString(1, user.getUsername());ps.setString(2, user.getPassword());ResultSet rs = ps.executeQuery();if (rs.next()){u =new User(rs.getInt(1),rs.getString(2),rs.getString(3));}return u;}@Overridepublic int insertUser(User user, Connection connection) throws SQLException {String sql="insert into user values (null,?,?)";PreparedStatement ps = connection.prepareStatement(sql);ps.setString(1, user.getUsername());ps.setString(2, user.getPassword());int i = ps.executeUpdate();DbUtils.close(ps,null,null);return i;}@Overridepublic int selectUserByUsername(String username, Connection connection) throws SQLException {PreparedStatement ps=null;ResultSet resultSet=null;int anInt=0;try {String sql="select count(*) from user where username=?";ps = connection.prepareStatement(sql);ps.setString(1,username);resultSet = ps.executeQuery();if (resultSet.next()){anInt = resultSet.getInt(1);}} finally {DbUtils.close(ps,resultSet,null);}return anInt;}}
package com.teaching.jsp.entity;public class Brand {// id 主键private Integer id;// 品牌名称private String brandName;// 企业名称private String companyName;// 排序字段private Integer ordered;// 描述信息private String description;// 状态:0:禁用1:启用private Integer status;public Brand() {}public Brand(Integer id, String brandName, String companyName, String description) {this.id = id;this.brandName = brandName;this.companyName = companyName;this.description = description;}public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {this.id = id;this.brandName = brandName;this.companyName = companyName;this.ordered = ordered;this.description = description;this.status = status;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBrandName() {return brandName;}public void setBrandName(String brandName) {this.brandName = brandName;}public String getCompanyName() {return companyName;}public void setCompanyName(String companyName) {this.companyName = companyName;}public Integer getOrdered() {return ordered;}public void setOrdered(Integer ordered) {this.ordered = ordered;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}@Overridepublic String toString() {return "Brand{" +"id=" + id +", brandName='" + brandName + '\'' +", companyName='" + companyName + '\'' +", ordered=" + ordered +", description='" + description + '\'' +", status=" + status +'}';}}
package com.teaching.jsp.utils;import com.alibaba.druid.pool.DruidDataSourceFactory;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import javax.sql.DataSource;import java.io.FileInputStream;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;public class DbUtils {private static DataSource dataSource;static {Properties prop=new Properties();try {prop.load(new FileInputStream("F:\\JAVA EE Preject\\课堂练习\\src\\com\\teaching\\jsp\\db.properties"));System.out.println("iiii");dataSource = DruidDataSourceFactory.createDataSource(prop);} catch (IOException e) {throw new RuntimeException(e);} catch (Exception e) {throw new RuntimeException(e);}}public static void test(){System.out.println(DbUtils.class.getClassLoader().getResource("db.properties"));}public static Connection getConnection() throws SQLException {System.out.println("iiii111");return dataSource.getConnection();}public static void close(Connection connection) throws SQLException {if (connection!=null){connection.close();}}public static void close(PreparedStatement ps, ResultSet rs,Connection connection) throws SQLException {if (rs!=null){rs.close();}if (ps!=null){ps.close();}if (connection!=null){connection.close();}}}
- 【JVM】Java虚拟机中的程序计数器
- Java 对象结构之 markword
- Java数据结构篇五:Hashtable
- Java开发工具Idea Revert操作
- JAVA的垃圾收集器与内存分配策略【一篇文章直接看懂】
- java实例属性_java类属性和实例属性区别 Java中类、属性、对象的概念及
- [架构之路-183]-《软考-系统分析师》-13-系统设计
- java实例内部类
- java hashtable 初始化_Java中HashTable和HashMa
- java 运行.jnlp文件,在Linux系统上执行/打开JNLP文件的方法