forked from jbloch/effective-java-3e-source-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticFactoryMethods.java
More file actions
54 lines (47 loc) · 1.68 KB
/
Copy pathStaticFactoryMethods.java
File metadata and controls
54 lines (47 loc) · 1.68 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package effectivejava.chapter2.item1;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Random;
import java.util.Set;
/**
* @Description: 静态工厂方法
* @Author: Baixin
* @Create: 2019-08-25 19:44:07
**/
public class StaticFactoryMethods {
public static void main(String[] args) {
BigInteger bigInt = BigInteger.probablePrime(2, new Random());
Set<SampleEnum> s = Collections.synchronizedSet(EnumSet.noneOf(SampleEnum.class));
try {
// 首先要导入数据库驱动jar包、直接复制到文件那里,然后加入到路径
// 1. 注册驱动,java反射机制
Class.forName("com.mysql.jdbc.Driver");
// 2. 创建一个连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
// 3. 创建一个sql语句的发送命令对象
Statement stmt = conn.createStatement();
// 4. 执行sql,拿到查询的结果集对象
ResultSet rs = stmt.executeQuery("select * from stu");
// 5. 输出结果集的数据
while (rs.next()) {
System.out.println(rs.getInt("id") + ":" + rs.getString("name"));
}
// 6. 关闭连接,命令对象以及结果集。
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
enum SampleEnum {
ENUM1,
ENUM2,
ENUM3;
}
}