I made a lot of notes when I was learning Java before, but I kept it all the time without sorting it out... (actually lazy), since I set up a Flag, I have to sort out these precious notes, but it will take some time (#funny)
1. Create a new Java project (if not already)
2. Add the dependency of the driver package (jar package) (mysql, Oracle...) to the current project , the package is obtained from the database manufacturer,
This package implements the JDBC interface, and combines these implemented classes into a jar package.
3. Add package and class to the project
a. Create entity (entity) package or vo package (value object)
The classes in the entity package and the classes in the vo package are both used for data.
The attributes of the class are private, through the public The getter and setter access a certain class in the data entity package, corresponding to the class in the corresponding data table vo package in the database, and place the data temporarily used in the program com.xxx.entity entity package com .xxx.vo vo package eg. com.xxx.entity.User This user class corresponds to the t_user table com.xxx.vo.Page in the database This class is used to access paging information b. Create a dao package (data access object data access Object ) com.xxx.dao This package and its subpackages are all java classes or interfaces related to database operations com.xxx.dao.common This package is the common java class for database operations of all modules
com.xxx.dao.common.CommonDao The functions in this class are public functions of database operations, such as opening a database connection, closing a database connection, etc.
1. Open a connection
//Load the driver class
Class.f o rName("Entry of the database ");
eg.
MySql:com.mysql.jdbc.Driver
Oracle:oracle.jdbc.OracleDriver
//Through the driver manager class, load the sub-implementation class of the interface in the corresponding driver package
Connection con=DriverManager.getConnection("url","usrname" ,"userpassword");
mysql:
url: jdbc:mysql://localhost:3306/testdb
username:root
userpassword:root
oracle:
url: jdbc:oracle:thin:@localhost:1521:orcl
username:scott
userpassword:tiger
2.关连接
//分别判断ResultSet对象,PreparedStatement对象
Connection对象,如果不空就调用close() 方法
eg.
com.xxx.dao.user 此包中专门操作user相关的数据库操作
com.xxxdao.user.IUserDao 此接口规范规范了用户的数据库操作
com.xxx.dao.user.impl.UserDaoMysql 此类是IUserDao的实现
4.访问数据库:
Oracle:oracle.jdbc.OracleDriver
//Through the driver manager class, load the sub-implementation class of the interface in the corresponding driver package
Connection con=DriverManager.getConnection("url","usrname" ,"userpassword");
mysql:
url: jdbc:mysql://localhost:3306/testdb
username:root
userpassword:root
oracle:
url: jdbc:oracle:thin:@localhost:1521:orcl
username:scott
userpassword:tiger
2.关连接
//分别判断ResultSet对象,PreparedStatement对象
Connection对象,如果不空就调用close() 方法
eg.
com.xxx.dao.user 此包中专门操作user相关的数据库操作
com.xxxdao.user.IUserDao 此接口规范规范了用户的数据库操作
com.xxx.dao.user.impl.UserDaoMysql 此类是IUserDao的实现
4.访问数据库:
数据库的dml操作:增删改
1. 获取连接
2.构建dml的sql语句
3.基于连接和sql语句运送到数据库,构建命令对象
在命令对象中给sql语句的"?"占位符的位置填入具体数据
4.基于连接和完整的sql语句,调用executeUpdate( ) 方法执行sql语句,此方法的返回值是整数,返回值为0,dml失败,返回值>0,dml成功
5.关闭对应对象的资源
数据库的dql操作:查
1.获取连接
2.构建dql的sql语句
3.基于连接,把sql语句运送到数据库,有"?"占位符,给占位符补充数据
4.基于连接和完整的sql语句,调用executeQuery方法,executeQuery方法的返回值ResultSet(结果集)也就是查询出来的结果放在了ResultSet对象中
5.循环从ResultSet结果集中读取数据,并把数据转换成集合存储在内存中,在关闭连接后使用集合对象中的数据即可
6.关闭对应对象的资源
com.xxx.dao.user.impl.UserDaoMysql 此类是IUserDao的实现
数据库的dml操作
步骤同上
数据库的dql操作
步骤同上
5.如果有必要的话要进行单元测试
1. 获取连接
2.构建dml的sql语句
3.基于连接和sql语句运送到数据库,构建命令对象
在命令对象中给sql语句的"?"占位符的位置填入具体数据
4.基于连接和完整的sql语句,调用executeUpdate( ) 方法执行sql语句,此方法的返回值是整数,返回值为0,dml失败,返回值>0,dml成功
5.关闭对应对象的资源
数据库的dql操作:查
1.获取连接
2.构建dql的sql语句
3.基于连接,把sql语句运送到数据库,有"?"占位符,给占位符补充数据
4.基于连接和完整的sql语句,调用executeQuery方法,executeQuery方法的返回值ResultSet(结果集)也就是查询出来的结果放在了ResultSet对象中
5.循环从ResultSet结果集中读取数据,并把数据转换成集合存储在内存中,在关闭连接后使用集合对象中的数据即可
6.关闭对应对象的资源
com.xxx.dao.user.impl.UserDaoMysql 此类是IUserDao的实现
数据库的dml操作
步骤同上
数据库的dql操作
步骤同上
5.如果有必要的话要进行单元测试
附:有关网络的若干协议
http协议 http://www.163.com
https协议 https://www.xxx.com
ftp协议 ftp://192.168.1.121
smtp协议 smtp://ip
jdbc:mysql协议 jdbc:mysql://ip:3306/数据库
jdbc:oracle协议 jdbc:thin协议:@ip地址 jdbc:oracle:thin:@localhost:1521:orcl
http协议 http://www.163.com
https协议 https://www.xxx.com
ftp协议 ftp://192.168.1.121
smtp协议 smtp://ip
jdbc:mysql协议 jdbc:mysql://ip:3306/数据库
jdbc:oracle协议 jdbc:thin协议:@ip地址 jdbc:oracle:thin:@localhost:1521:orcl