当前位置:网站首页>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.
边栏推荐
- Vulkan-实践第一弹
- The "2022 China Digital Office Market Research Report" can be downloaded to explain the 176.8 billion yuan market in detail
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
- 2022中国3D视觉企业(引导定位、分拣场景)厂商名单
- 简单聊聊运维监控的其他用途
- [Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
- [shutter] image component (image component introduction | image constructor | image.network constructor | image.asset constructor)
- Graduation summary
- LeedCode1480. Dynamic sum of one-dimensional array
- Implement the foreach method of array
猜你喜欢
UART、RS232、RS485、I2C和SPI的介绍
百度智能云牵头打造智能云综合标准化平台
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
2022中国3D视觉企业(引导定位、分拣场景)厂商名单
2022上半年值得被看见的10条文案,每一句都能带给你力量!
字符设备注册常用的两种方法和步骤
Feature Engineering: summary of common feature transformation methods
MySQL 23 classic interview hanging interviewer
Logback configuration file
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
随机推荐
cordova-plugin-device获取设备信息插件导致华为审核不通过
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Vulkan-实践第一弹
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
Shell implements basic file operations (cutting, sorting, and de duplication)
Automated defect analysis in electronic microscopic images
毕业总结
NC50965 Largest Rectangle in a Histogram
Use Jenkins II job
Array common operation methods sorting (including ES6) and detailed use
mm中的GAN模型架构
Extension of flutter
Callback event after the antv X6 node is dragged onto the canvas (stepping on a big hole record)
Multiprocess programming (4): shared memory
Vulkan-性能及精细化
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
Centos7 one click compilation to build MySQL script
NC24325 [USACO 2012 Mar S]Flowerpot
微信小程序获取某个元素的信息(高、宽等),并将px转换为rpx。