1.
下面哪个不是标准Statement类?
正确答案: D 你的答案: A (错误)
Statement
PreparedStatement
CallableStatement
BatchedStatement
Statement 对象用于将 SQL 语句发送到数据库中。实际上有三种 Statement 对象,它们都作为在给定连接上执行 SQL 语句的包容器:Statement、PreparedStatement(它从 Statement 继承而来)和 CallableStatement(它从 PreparedStatement 继承而来)。它们都专用于发送特定类型的 SQL 语句: Statement 对象用于执行不带参数的简单 SQL 语句;PreparedStatement 对象用于执行带或不带 IN 参数的预编译 SQL 语句;CallableStatement 对象用于执行对数据库已存在的存储过程的调用。
Statement是sql语句的载体
Statement是标准的Statement类,通过字符串对sql语句进行拼接,但是它存在sql注入的危险
PreparedStatement对sql语句进行了预编译,可以防止SQL注入
CallableStatement用来调用存储过程的
BatchedStatement用于批量操作数据库,BatchedStatement不是标准的Statement类
2.what is the result of the following code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum
AccountType
{
SAVING, FIXED, CURRENT;
private
AccountType()
{
System.out.println(“It is a account type”);
}
}
class
EnumOne
{
public
static
void
main(String[]args)
{
System.out.println(AccountType.FIXED);
}
}
正确答案: C 你的答案: B (错误)
Compiles fine and output is prints”It is a account type”once followed by”FIXED”
Compiles fine and output is prints”It is a account type”twice followed by”FIXED”
Compiles fine and output is prints”It is a account type”thrice followed by”FIXED”
Compiles fine and output is prints”It is a account type”four times followed by”FIXED”
Compilation fails
枚举类在后台实现时,实际上是转化为一个继承了java.lang.Enum类的实体类,原先的枚举类型变成对应的实体类型,
上例中AccountType变成了个class AccountType,并且会生成一个新的构造函数,若原来有构造函数,则在此基础上
添加两个参数,生成新的构造函数,如上例子中:
1
private
AccountType(){ System.out.println(“It is a account type”); }
会变成:
1
2
private
AccountType(String s,
int
i){
super
(s,i); System.out.println(“It is a account type”); }
而在这个类中,会添加若干字段来代表具体的枚举类型:
1
2
3
public
static
final
AccountType SAVING;
public
static
final
AccountType FIXED;
public
static
final
AccountType CURRENT;
而且还会添加一段static代码段:
1
2
3
4
5
6
static
{
SAVING =
new
AccountType(
"SAVING"
,
0
);
... CURRENT =
new
AccountType(
"CURRENT"
,
0
);
$VALUES =
new
AccountType[]{
SAVING, FIXED, CURRENT
} }
以此来初始化枚举中的每个具体类型。(并将所有具体类型放到一个$VALUE数组中,以便用序号访问具体类型)
在初始化过程中new AccountType构造函数被调用了三次,所以Enum中定义的构造函数中的打印代码被执行了3遍。
怎么通过执行AccountType.FIXED这句到调用静态代码块的?这个其实是与java类中静态代码的初始化有关:类中的静态类型,
仅在程序中第一次被使用时才会被初始化,且只初始化一次。初始化过程中还会自动调用类中用static{ }括起来的代码。
因此static final的字段的初始化过程才可以写在static{ }中。
转载请注明原文地址: https://ju.6miu.com/read-677294.html