-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectJava.java
More file actions
147 lines (120 loc) · 4.56 KB
/
ConnectJava.java
File metadata and controls
147 lines (120 loc) · 4.56 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.NumberColumn;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.Table;
public class ConnectJava {
static final private String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final private String DB_URL = "jdbc:mysql://localhost:3306/Simon?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
static final String USER = "root";
static final String PASS = "admin";
public static void main(String[] args) throws IOException {
ConnectJava cj = new ConnectJava();
cj.gainFromSQL();
}
public void gainFromSQL() {
Connection conn = null;
Statement stmt = null;
ArrayList<String> idList =new ArrayList<>();
ArrayList<String> nameList =new ArrayList<>();
ArrayList<String> urlList =new ArrayList<>();
try {
Class.forName(JDBC_DRIVER);
System.out.println("Connect to database:");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("实例化Statement对象");
stmt = (Statement) conn.createStatement();
String sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
int row = 0;
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
idList.add(Integer.toString(id));
nameList.add(name);
urlList.add(url);
ArrayList<String> appendList =new ArrayList<>();
appendList.add(Integer.toString(id));
appendList.add(name);
appendList.add(url);
appendFile(row++, appendList);
}
tableSawTest(idList, nameList, urlList);//Display the list of database.
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
public void appendFile(int rowIndex, ArrayList<String> appendList) throws IOException{
String path = "D:\\Hello.xls";
File file = new File(path);
int columnIndex = 0;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
if(file.exists()){
FileInputStream fis = new FileInputStream(file);
workbook = new HSSFWorkbook(fis);
sheet = workbook.getSheet("Sheet0");
}else {
workbook = new HSSFWorkbook();
sheet = workbook.createSheet();
}
HSSFRow row = sheet.getRow(rowIndex);
for(String value : appendList){
if(row == null){
row = sheet.createRow(rowIndex);
}
HSSFCell cell = row.createCell(columnIndex++);
cell.setCellValue(value);
}
workbook.write(file);
}
public void tableSawTest(ArrayList<String> idList, ArrayList<String> nameList, ArrayList<String> urlList) {
String[] id = new String[idList.size()];
String[] name = new String[nameList.size()];
String[] url = new String[urlList.size()];
idList.toArray(id);
nameList.toArray(name);
urlList.toArray(url);
Table table = Table.create("Complete Collection of Web sites").addColumns(
StringColumn.create("ID", id),
StringColumn.create("站点名称", name),
StringColumn.create("站点地址", url));
System.out.println(table.print());
}
}