当前位置:网站首页>[explanation of JDBC and internal classes]
[explanation of JDBC and internal classes]
2022-07-07 07:05:00 【DJL_ new_ life】
( Click jump )
List of articles
JDBC And internal classes
JDBC
JDBC
Routine code , modify excuteUpdate()
, Inquire about excuteQuery()
all Java Operating relational databases , No matter what kind of database , The routine is the same .JPA、MyBatis All are JDBC Encapsulation .
Java.sql
The next interface
- Acquisition data source
DataSource
, Configure the connection address , user name 、 Password etc. - Get the connection object , Is to send a network request , Establish a connection to the database Connection
- Access to perform SQL The object of PreparedStatement object , Packaged SQL sentence
Connection connection = dataSource.getConnection();
String sql = “select password from user where username = ?”;
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,“ full name ”);
- Perform an update operation
executeUpdate
=> int The number of rows affected by this update
Execute the query executeQuery
=> Result Result set
ResultSet resultSet = statement.executeQuery();
- Traversal result set ResultSet object , Every time you call next Method , Just take a row of data from the result set resule.getXX(“ Name ”) => Get the specific attribute value of the data in this line
while(resultSet.next())
- After the operation close resource ,resultSet,Connection object
Acquisition data source Yes Two ways :
One is DataSource The way ( What is actually used )
The other is DriverManager( It is not recommended to use )
DataSource
:
Create... Internally Connection Object's connection pool , “ pool ”: Resource reuse
When one Connection Object call close After method He's not really closed , This object will be DataSource Recycling , Enter the connection pool .
If there are other users who need to establish a connection , Instead of creating a new connection , If there are idle connections in the connection pool , Use this connection directly .
The establishment and destruction of database connections are still time-consuming and resource consuming
DriverManager
Class to get Connection Connect , Can't be reused , Every time you release resources after use , adopt connection.close() Is to close the physical connection
Specific implementation SQL There are also two ways for the object of ,
One is PreparedStatement object ( Recommended )
The other is Statement object
Statement : Simple to perform without parameters SQL sentence
PreparedStatement:
Used to execute with or without parameters SQL sentence
SQL Statements are precompiled in the database system
Execution faster than Statement object
Inner class
The so-called inner class , It is the operation of nesting one class into another class
It's just a way of Inner class A property in the current class
Inner class It is divided into :
- Member inner class
- Static inner class
- Method inner class
- Anonymous inner class (Lambda The predecessor of expression )
1 and 2 Are defined in the class , 3 and 4 Is defined in the method area
Reason for existence of inner class :
- Internal and external classes can easily access each other's private domains ( Properties and methods )
- Inner classes can be used for The exterior of the external class is completely hidden ( Treat internal classes as attributes of external classes ) Access modifier private < default < protected < public Inner classes can use private keyword
- Inner classes can implement multiple inheritance in a disguised form ( Understanding can )
Member inner class
1、 Member inner class : Do not use... Inside an external class static The inner class defined by keyword is the inner class of member . Analogy between member properties and member methods - The inner class of a member needs to depend on the outer class object ( First there are external class objects , There are inner class objects )
There are two ways to generate objects within a member class :
- Generated inside the external class Member inner class object : It's no different from ordinary classes . Inner class name Internal class reference = new Inner class ();
- If the internal class pair Externally visible ( No private Decorated inner class ). Create an inner class object outside the outer class , External class name . Inner class name Internal class reference = new External class ().new Inner class (); eg: Outter.Inner inner = new Outter().new Inner();
2、 The inner class of a member depends on the object of the outer class , First generate the external class object , Then the inner class object is generated - Analogy to member properties or member methods .
Member methods can access instance variables and static variables in classes , But you can't define static variables
Static methods can access static variables in classes , Member variables are not accessible
3、 The inner class of a member can directly access the private properties of the outer class
problem 1:
Member inner class Access to static fields in external classes Can I define a static field in the current inner class ?
answer : You can access static fields in external classes , Because the inner class of the member already has the object of the outer class , There are external class objects , Of course, you can access static properties
You cannot define static fields in the current inner class . If possible , The static properties of the inner class of a member can be called without generating an object , This sum The inner class of a member must depend on the outer class object to generate It's contradictory .
Static inner class
Use static keyword A class defined inside another class —— Static methods or properties
The biggest difference between static inner classes and member inner classes is : Static inner classes do not require objects of outer classes , And external classes are relatively independent , It's just an internal definition of an external class .
The static inner class can only access the static domain of the outer class , No external class objects , Cannot directly access the member domain of an external class
problem : Can a static inner class define its own member domain ?
Sure , A static inner class is a normal class , It's just inside a class
Summary :
The inner class of a member can access all domains of the outer class ( member , static state ), Because the inner class of a member depends on the object of the outer class , You cannot have your own static domain .
Static inner class You can have your own member domain , But you can't directly access the member domain of an external class ( No external class objects )
Method inner class
Is a class defined inside a method
public class Main{
public void fun(){
// Method inner class
class test{
}
}
}
Be careful :
- Method inner classes cannot use any permission modifiers ( This class has no methods )
- Completely hidden from the outside ( External to external classes )
- The inner class should use the formal parameters or local variables of the method , The variable must be implicit Of final Statement ( Method internal classes should use formal parameters or local variables in external methods , Only read right , Can't modify )
- If the local variables in the method are read in the internal class of the method ( Formal parameters or local variables ) This local variable cannot be modified ( Cannot be modified not only in Inner Classes , Nor in the method modify )
- Method inner class cannot define static Static domain ( Because static variables cannot be defined in methods )
Anonymous inner class
Defined in method ( Formal or actual parameters in methods )—— Arguments are used more
No permission modifiers , Not even the class name
Anonymous inner classes are also a kind of method inner classes , Most commonly used in method parameters
interface IMessage{
void getMag(String msg);
}
public class Main{
public static void main(String[] args){
fun(new IMessage(){
@Override
public void getMag(String msg){
}
});
}
}
The interface cannot directly instantiate the , So here we are new Yes. IMessage Subclass of interface , But this subclass is only used in fun Method
Lambda expression
2008 year JDK8 Release , An epoch-making version
Hadoop Map Reduce There are all kinds of Lambda Use of expressions
Functional programming ideas —》 Scala
java This object-oriented definition is too cumbersome , When dealing with various mathematical operations , Frequent need to define classes , object , Methods and so on
JDK8 after , In the interface default Common method .JDK8 Before , There are only global constants and abstract methods in the interface
default Keyword the method defined in the interface is a common method ( Don't omit ), Subclasses do not need to override .
Don't write default Think of it as an abstract method
Not commonly used , Generally, it is used to expand methods in the interface of the modified ancient version
Functional interface : An interface has and has only one abstract method ( Ordinary methods do not affect ), This kind of interface is called functional interface
Use @FunctionaInterface
To check whether the current interface is a functional interface
Lambda The predecessor of expression —— Anonymous inner class
fun(()->{
...
});
Be able to use Lambda The premise of the expression is Interfaces must be functional , There is only one abstract method
Lambda Four cases of expressions
1 No return value no parameter
interface NoParaNoReturn{
void test();
}
public static void main(String[] args){
NoParaNoReturn d = ()->{
// Inside the braces is the method body code
System.out.println(" No return value , No parameter ");
};
d.test();// What is called is after overwriting Of test Method
}
The rules 1: If the method body has only one line of code , It can be omitted {};
NoParaNoReturn d = ()->System.out.println(" No return value , No parameter ");
2 No return value, parameter
interface NoParaNoReturn{
void test(int a);
}
public static void main(String[] args){
NoParaNoReturn d = (int x)->{
// Inside the braces is the method body code
x += 10;
System.out.println(" No return value , No parameter ");
};
d.test();// What is called is after overwriting Of test Method
}
The rules 2 : If there is only one parameter of the method body , You can omit parentheses , Type should also be omitted
The rules 3 : It can be omitted Lambda The type of parameter in the expression , If type is omitted , Need to omit
NoParaNoReturn d = x->{
x += 10;
System.out.println(" No return value , No parameter ");
};
3 There is a return value No parameter
interface NoParaNoReturn{
int test();
}
public static void main(String[] args){
NoParaNoReturn d = ()->{
// Inside the braces is the method body code
int a = 10;
int b = 10;
return a+b;
};
System.out.println(d.test());// What is called is after overwriting Of test Method
}
The rules 4: If the abstract method has a return value And the overridden method body has only one line , At this point, the braces of the method body ,return All can be omitted
NoParaNoReturn d = ()-> 10+10;
4 There are return values and parameters
interface NoParaNoReturn{
int test(int a,int b);
}
public static void main(String[] args){
NoParaNoReturn d = (x,y)-> x+=y;
System.out.println(d.test(10,20));// What is called is after overwriting Of test Method
}
If it helps you , Please give me a compliment .
边栏推荐
- Jetpack Compose 远不止是一个UI框架这么简单~
- Basic introduction of JWT
- 场馆怎么做体育培训?
- 化工园区危化品企业安全风险智能化管控平台建设四大目标
- How DHCP router works
- Installing redis and windows extension method under win system
- MYSQL----导入导出&视图&索引&执行计划
- How can flinksql calculate the difference between a field before and after update when docking with CDC?
- 2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
- Advantages of using net core / why
猜你喜欢
How to model and simulate the target robot [mathematical / control significance]
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第一阶段答案
Sword finger offer high quality code
Bus message bus
MATLAB小技巧(29)多项式拟合 plotfit
2022年全国所有A级景区数据(13604条)
Prime partner of Huawei machine test questions
化工园区危化品企业安全风险智能化管控平台建设四大目标
ANR 原理及实践
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第二阶段答案
随机推荐
How can gyms improve their competitiveness?
Basic introduction of JWT
Config分布式配置中心
Multithreading and high concurrency (9) -- other synchronization components of AQS (semaphore, reentrantreadwritelock, exchanger)
Data of all class a scenic spots in China in 2022 (13604)
根据IP获取地市
Stack and queue-p79-10 [2014 unified examination real question]
Complete process of MySQL SQL
How to share the same storage among multiple kubernetes clusters
main函数在import语句中的特殊行为
ANR 原理及实践
Anr principle and Practice
Networkx绘图和常用库函数坐标绘图
Big coffee gathering | nextarch foundation cloud development meetup is coming
Unable to debug screen program with serial port
常用函数detect_image/predict
Exception of DB2 getting table information: caused by: com ibm. db2.jcc. am. SqlException: [jcc][t4][1065][12306][4.25.13]
Take you to brush (niuke.com) C language hundred questions (the first day)
【NOI模拟赛】区域划分(结论,构造)
Distributed ID solution