目录
五、创建PrepareStatement , 编写sql语句
一、创建resource 跟文件



二、创建BaseDao类。设置属性位静态属性
public class BaseDao {
private static String driver;
private static String url;
private static String user;
private static String pwd;
三、创建static代码块:
创建实例化会直接加载,得到连接方式,用户名,密码
static {
Properties properties = new Properties();
InputStream inputStream = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
try {
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
driver = properties.getProperty("mysqldriver");
url = properties.getProperty("mysqlurl");
user = properties.getProperty("mysqluser");
pwd = properties.getProperty("mysqlpwd");
System.out.println(driver);
System.out.println(url);
System.out.println(user);
System.out.println(pwd);
}
四、加载驱动,创建Connnection连接
public Connection getConnection() {
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
五、创建PrepareStatement , 编写sql语句
/*将新增,修改,删除统一抽象到一个方法中
* params... 用法:表示n个参数[0,+无穷]
* */
public int executeUpdate(String sqlStr, Object... params){
Connection connection = this.getConnection();
PreparedStatement preparedStatement = null;
String sql = sqlStr;
int num = -1;
try {
preparedStatement = connection.prepareStatement(sql);
if(null!=params){
for(int i = 0; i < params.length;i++){
preparedStatement.setObject(i+1,params[i]);
}
}
num = preparedStatement.executeUpdate();
} catch (SQLException e){
e.printStackTrace();
} finally {
this.close(preparedStatement,connection);
}
return num;
}
六、释放资源(方法重载,关闭不同连接)
public void close(PreparedStatement preparedStatement, Connection connection) {
try {
if (connection != null) {
connection.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
七、创建实例,测试是否报错
public static void main(String[] args) {
BaseDao baseDao = new BaseDao();
Connection connection = baseDao.getConnection();
System.out.println(connection);
}
八、创建接口方法
/*
* 对数据库中Dog表的操作定义,比如新增宠物,
* 根据id删除宠物,
* 根据健康值删除宠物,
* 根据id修改宠物信息,
* 根据id查询宠物信息,
* 根据多条件查询宠物信息
* */
public interface DogDao {
Integer saveDog(Dog dog);
Integer updateDog(Dog dog);
Integer delById(Integer id);
Integer delByhealth(Integer health);
}
九、创建测试类测试
public class DogDaoImpl extends BaseDao implements DogDao{
@Override
public Integer saveDog(Dog dog) {
String sql="insert into dog(name,health,love,strain,lytm) value(?,?,?,?,now())";
int num = super.executeUpdate(sql, dog.getName(), dog.getHealth(), dog.getLove(), dog.getStrain());
if (num>0){
System.out.println("添加成功");
}
return num;
@Override
public Integer updateDog(Dog dog) {
String sql = "update dog set name=?, health=?, love=?,strain=? where id=?";
int num = super.executeUpdate(sql, dog.getName(), dog.getHealth(), dog.getLove(), dog.getStrain(), dog.getId());
if(num>0){
System.out.println("修改成功");
}
return num;
}
}
9143

被折叠的 条评论
为什么被折叠?



