当前位置:网站首页>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.
边栏推荐
- 关于Unity屏幕相关Screen的练习题目,Unity内部环绕某点做运动
- [shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
- University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
- Bypass AV with golang
- 免费自媒体必备工具分享
- Leetcode 294. Flip game II (game theory)
- Shell implements basic file operations (SED edit, awk match)
- Two common methods and steps of character device registration
- MySQL 23 classic interview hanging interviewer
- Understanding and application of least square method
猜你喜欢

Shell脚本基本使用

pageoffice-之bug修改之旅

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

【雅思阅读】王希伟阅读P1(阅读判断题)

Solution to the problem of abnormal display of PDF exported Chinese documents of confluence

An excellent orm in dotnet circle -- FreeSQL

Nacos+openfeign error reporting solution

Two common methods and steps of character device registration

文件操作IO-Part2

Install docker and use docker to install MySQL
随机推荐
腾讯云免费SSL证书扩展文件含义
Don't want teachers to see themselves with cameras in online classes? Virtual camera you deserve!
Free we media essential tools sharing
MySQL 23 classic interview hanging interviewer
[IELTS reading] Wang Xiwei reading P1 (reading judgment question)
详解用OpenCV的轮廓检测函数findContours()得到的轮廓拓扑结构(hiararchy)矩阵的意义、以及怎样用轮廓拓扑结构矩阵绘制轮廓拓扑结构图
Logback configuration file
About the practice topic of screen related to unity screen, unity moves around a certain point inside
【雅思阅读】王希伟阅读P1(阅读判断题)
University of Toronto: Anthony coach | the conditions of deep reinforcement learning can induce dynamic risk measurement
v8
NC20806 区区区间间间
Nacos+openfeign error reporting solution
NC50528 滑动窗口
Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time
Kubernetes simple introduction to writing YML
【Pulsar文档】概念和架构/Concepts and Architecture
AEM: Nanlin fan Ben et al. - plant rhizosphere growth promoting bacteria control soybean blight
Some introduction and precautions about XML
NC24325 [USACO 2012 Mar S]Flowerpot