当前位置:网站首页>Teach you JDBC hand in hand -- structure separation
Teach you JDBC hand in hand -- structure separation
2022-07-03 00:39:00 【Gorit】
JDBC Learning notes —— Structural separation
- 1.2.1 establish jdbc.properties file
- 1.2.2 establish ConfigProperty.java file
- 1.2.3 establish Contranct.java file
- 1.3.1 UserDao Interface implementation
- 1.3.2 UserDaoImpl class ,UserDao Method implementation of
- Looking back
Preparation
Basic configuration see me
One 、 Realize your second JDBC Program
1.1 Basic structure of the project

Explanation of three packages :
- dao Package created under UserDao class , Realize the most basic method of adding, deleting, modifying and checking
- entity Package created under User class , Add basic User Basic information of as well as getter and setter Method
- util It's a bag , Use BaseDao Class to JDBC Perform packaging work
1.2 BaseDao Structural separation
1.2.1 establish jdbc.properties file
stay util In bag , We will BaseDao The part of the registration driver in is further subdivided , Store the four element file of registration drive in In a file , It's packaged twice (jdbc.properties), And add the following
#mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bank?userUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
- 1.
- 2.
- 3.
- 4.
- 5.
1.2.2 establish ConfigProperty.java file
Used below Properties class , Then use this class to read jdbc.properties Properties file
package cn.jdbc.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ConfigProperty {
//Properties(Java.util.Properties), Mainly used to read Java Configuration file for
Properties properties = null;
private static ConfigProperty instance = null;
private ConfigProperty() {
properties = new Properties();// Create a property file object
// Get the input stream -- The reflex mechanism
InputStream is = ConfigProperty.class.getClassLoader()
.getResourceAsStream("jdbc.properties");
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static ConfigProperty getInstance() {
if (instance == null) {
instance = new ConfigProperty();
}
return instance;
}
public String getValueByKey(String key) {// according to key, Find value
return properties.getProperty(key);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
1.2.3 establish Contranct.java file
Create a Contranct Interface . And then
package cn.jdbc.util;
public interface Contranct {
public static final String DRIVER=ConfigProperty.getInstance().getValueByKey("jdbc.driver");
public static final String URL=ConfigProperty.getInstance().getValueByKey("jdbc.url");
public static final String NAME=ConfigProperty.getInstance().getValueByKey("jdbc.username");
public static final String PWD=ConfigProperty.getInstance().getValueByKey("jdbc.password");
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
Why do you do this ?
- Because it did , Only modify the configuration in the future jdbc.properties Can be configured , The code can basically be changed
- Then add the corresponding database driver
1.3 Dao Layer separation
take UserDao Separate into the following two parts
- UserDao Class to UserDaoT class
- Add a new one UserDao Interface
- Add a new one UserDaoImpl class , Inherit BaseDao class as well as UserDao Interface , Final realization Data addition, deletion, modification and query
1.3.1 UserDao Interface implementation
package cn.jdbc.dao;
import cn.jdbc.entity.User;
public interface UserDao {
User selectUserByuserNameAndPwd(String userName, String pwd);
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
1.3.2 UserDaoImpl class ,UserDao Method implementation of
package cn.jdbc.dao.Impl;
import java.sql.SQLException;
import cn.jdbc.dao.UserDao;
import cn.jdbc.entity.User;
import cn.jdbc.util.BaseDao;
public class UserDaoImpl extends BaseDao implements UserDao{
@Override
public User selectUserByuserNameAndPwd(String userName, String pwd) {
// TODO Auto-generated method stub
User user = null;
String sql = "SELECT *FROM USER WHERE username=? AND pwd=?";
Object[] params = { userName, pwd };
rs = this.executeQuery(sql, params);
try {
if (rs.next()) {
user = new User(rs.getString("username"), rs.getString("pwd"),
rs.getString("status"), rs.getString("qq"),
rs.getTimestamp("registerTime"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
边栏推荐
- 【Pulsar文档】概念和架构/Concepts and Architecture
- 利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
- Basic 10 of C language: array and pointer
- Linux软件:如何安装Redis服务
- 百度智能云牵头打造智能云综合标准化平台
- Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
- [MCU project training] eight way answering machine
- [Luogu p4320] road meets (round square tree)
- [shutter] image component (image component introduction | image constructor | image.network constructor | image.asset constructor)
- Some introduction and precautions about XML
猜你喜欢

ftrace工具的介绍及使用

University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement

Rust所有权(非常重要)

Install docker and use docker to install MySQL

Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs

UART、RS232、RS485、I2C和SPI的介绍

图解网络:什么是虚拟路由器冗余协议 VRRP?

MySQL 23道经典面试吊打面试官

Nacos+openfeign error reporting solution

How SQLSEVER removes data with duplicate IDS
随机推荐
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(中)-- 搜索建议
百数不断创新,打造自由的低代码办公工具
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
Rust string slicing, structs, and enumeration classes
Free we media essential tools sharing
关于XML一些介绍和注意事项
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
Pytorch 20 realizes corrosion expansion based on pytorch
Vulkan-实践第一弹
Hundreds of continuous innovation to create free low code office tools
Rust字符串切片、结构体和枚举类
Maya fishing house modeling
An excellent orm in dotnet circle -- FreeSQL
可下载《2022年中国数字化办公市场研究报告》详解1768亿元市场
机器学习:numpy版本线性回归预测波士顿房价
Implement the foreach method of array
Solution to the problem of abnormal display of PDF exported Chinese documents of confluence
【JetCache】JetCache的配置说明和注解属性说明
Andorid gets the system title bar height