当前位置:网站首页>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.
边栏推荐
- Thinkadmin V6 arbitrary file read vulnerability (cve-2020-25540)
- 机器学习:numpy版本线性回归预测波士顿房价
- Multiprocess programming (V): semaphores
- Free we media essential tools sharing
- Implement the foreach method of array
- 微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。
- Nc20806 District interval
- Form form instantiation
- redis21道经典面试题,极限拉扯面试官
- Andorid gets the system title bar height
猜你喜欢
随机推荐
NC24840 [USACO 2009 Mar S]Look Up
Briefly talk about other uses of operation and maintenance monitoring
University of Toronto:Anthony Coache | 深度强化学习的条件可诱导动态风险度量
Vulkan-实践第一弹
[shutter] image component (the placeholder | transparent_image transparent image plug-in is loaded into the memory)
node_modules删不掉
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
【雅思阅读】王希伟阅读P1(阅读判断题)
Nc17059 queue Q
[shutter] image component (image component introduction | image constructor | image.network constructor | image.asset constructor)
关于XML一些介绍和注意事项
Pytorch 20 realizes corrosion expansion based on pytorch
Automated defect analysis in electronic microscopic images
布隆过滤器
kubernetes编写yml简单入门
Program analysis and Optimization - 9 appendix XLA buffer assignment
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
JSON转换工具类
Linux软件:如何安装Redis服务








