当前位置:网站首页>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.
边栏推荐
- Program analysis and Optimization - 9 appendix XLA buffer assignment
- LeedCode1480.一维数组的动态和
- 微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
- LeedCode1480. Dynamic sum of one-dimensional array
- Multiprocess programming (4): shared memory
- mm中的GAN模型架构
- Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调
- Introduction and use of ftrace tool
- Preview word documents online
- 线程的启动与优先级
猜你喜欢
![[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)](/img/73/19e2e0fc5ea6f05e34584ba40a452d.jpg)
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)

Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t

字符设备注册常用的两种方法和步骤

为什么网站打开速度慢?

Pageoffice - bug modification journey

Shell implements basic file operations (cutting, sorting, and de duplication)

Vulkan-实践第一弹

antv x6节点拖拽到画布上后的回调事件(踩大坑记录)

Introduction and use of ftrace tool

Rust string slicing, structs, and enumeration classes
随机推荐
使用jenkins之二Job
腾讯云免费SSL证书扩展文件含义
Use Jenkins II job
Cmake basic use
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
Shell 实现文件基本操作(sed-编辑、awk-匹配)
Pytorch 20 realizes corrosion expansion based on pytorch
Don't want teachers to see themselves with cameras in online classes? Virtual camera you deserve!
关于QByteArray存储十六进制 与十六进制互转
Pageoffice - bug modification journey
利亚德:Micro LED 产品消费端首先针对 100 英寸以上电视,现阶段进入更小尺寸还有难度
Hundreds of continuous innovation to create free low code office tools
An excellent orm in dotnet circle -- FreeSQL
多进程编程(五):信号量
Leetcode 294. Flip game II (game theory)
百数不断创新,打造自由的低代码办公工具
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
About qbytearray storage hexadecimal and hexadecimal conversion
node_modules删不掉
The most painful programming problem in 2021, adventure of code 2021 Day24