java中常用的工具类,三

继续分享java中常用的一些工具类。前两篇的文章中有人评论使用Apache 的lang包和IO包,或者Google的Guava库。后续的我会加上的!谢谢支持IT江湖

一、连接数据库的综合类

Java

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

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

/**

* 连接数据库的综合类。</br>

* 依赖jar包:commons.dbcp-1.4,commons.dbutils-1.3,commons.pool-1.5.4包。

*

* @author 宋立君

* @date 2014年07月03日

*/

{

;

;

;

;

// 连接池名称

// 连接池

// 对应的定时查询类

;

/**

* 功能:构造函数

*

* @author 宋立君

* @date 2014年07月03日

* @param dri

* 驱动全类名,例如:com.mysql.jdbc.Driver。

* @param url

* 数据库url连接,例如:

* @param userName

* 数据库用户名,例如:root

* @param password

* 数据库密码,例如:abc

* @param poolName

* 创建的数据库连接池的名称,例如mypool,注意一个web容器此名称不能重复。

*/

,

{

;

;

;

;

;

}

/**

* 执行sql。

*

* @param conn

* 连接

* @param pstm

* PreparedStatement

* @return int 执行sql对应的影响行。

* @throws SQLException

* @author 宋立君

* @date 2014年07月03日

*/

)

{

{

;

{

;

}

}

/**

* 查询sql。

*

* @param conn

* 连接

* @param pstm

* PreparedStatement

* @return List<Map<String,Object>> 查询的结果集

* @throws SQLException

* @author 宋立君

* @date 2014年07月03日

*/

,

{

{

;

{

;

}

}

/**

* 功能:ResultSet 转为List<Map<String,Object>>

*

*

* @param rs

* ResultSet 原始数据集

* @return List<Map<String,Object>>

* @throws java.sql.SQLException

* @author 宋立君

* @date 2014年07月03日

*/

)

{

)

;

// 得到结果集(rs)的结构信息,比如字段数、字段名等

// 返回此 ResultSet 对象中的列数

;

;

{

;

{

;

}

;

}

;

}

/**

* 查询sql语句。

*

* @param sql

* 被执行的sql语句

* @return List<Map<String,Object>>

* @throws SQLException

* @author 宋立君

* @date 2014年07月03日

*/

{

;

;

{

;

;

;

{

;

}

;

}

/**

* 根据参数查询sql语句

*

* @param sql

* sql语句

* @param param

* 参数

* @return List<Map<String,Object>>

* @throws SQLException

* @author 宋立君

* @date 2014年07月03日

*/

)

{

;

;

{

;

;

,

;

{

;

{

;

}

;

}

/**

* 执行sql语句

*

* @param sql

* 被执行的sql语句

* @return 受影响的行

* @throws Exception

* @author 宋立君

* @date 2014年07月03日

*/

{

;

;

{

;

;

{

;

}

;

}

/**

* 执行含参数的sql语句

*

* @param sql

* 被执行的sql语句

* @param params

* 参数

* @return 返回受影响的行

* @throws Exception

* @author 宋立君

* @date 2014年07月03日

*/

{

;

;

{

;

;

{

;

}

;

}

/**

* 关闭连接

*

* @param conn

* @throws SQLException

* @author 宋立君

* @date 2014年07月03日

*/

{

{

;

}

;

}

/**

* 启动连接池

*

* @author 宋立君

* @date 2014年07月03日

*/

{

{

;

{

;

}

{

;

}

{

;

(

;

(

,

;

;

DriverManager

;

;

{

;

}

// 开启查询程序

;

;

}

/**

* 关闭连接池

*

* @author 宋立君

* @date 2014年07月03日

*/

{

{

DriverManager

;

;

// 关闭定时查询

;

{

;

}

}

/**

* 得到一个连接

*

* @return

* @author 宋立君

* @date 2014年07月03日

*/

{

;

{

)

;

;

{

;

}

;

}

}

/**

* 当连接池启动后会自动定时查询数据库,防止数据库连接超时。

*

* @author 宋立君

* @date 2014年07月03日

*/

{

;

// 是否开启查询

;

/**

* 功能:对应的数据库连接。

*

* @author 宋立君

* @date 2014年07月03日

* @param dbUtil

* 数据库连接

*/

{

;

}

{

{

{

{

;

}

{

;

{

{

;

{

;

}

}

}

}

{

;

}

}

依赖的jar

commons.dbcp-1.4,commons.dbutils-1.3,commons.pool-1.5.4包

二、DES加密和解密

DESUtil.java

Java

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

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

;

;

;

;

;

;

;

;

;

;

;

;

;

;

/**

* DES加密和解密。

*

* @author 宋立君

* @date 2014年07月03日

*/

{

/** 安全密钥 */

;

/**

* 功能:构造

*

* @author 宋立君

* @date 2014年07月03日

*/

{

}

/**

* 功能:构造

*

* @author 宋立君

* @date 2014年07月03日

* @param keyData

* key

*/

{

;

}

/**

* 功能:加密 (UTF-8)

*

* @author 宋立君

* @date 2014年07月03日

* @param source

* 源字符串

* @param charSet

* 编码

* @return String

* @throws UnsupportedEncodingException

* 编码异常

*/

{

;

}

/**

*

* 功能:解密 (UTF-8)

*

* @author 宋立君

* @date 2014年07月03日

* @param encryptedData

* 被加密后的字符串

* @return String

* @throws UnsupportedEncodingException

* 编码异常

*/

)

{

;

}

/**

* 功能:加密

*

* @author 宋立君

* @date 2014年07月03日

* @param source

* 源字符串

* @param charSet

* 编码

* @return String

* @throws UnsupportedEncodingException

* 编码异常

*/

)

{

;

;

;

;

}

/**

*

* 功能:解密

*

* @author 宋立君

* @date 2014年07月03日

* @param encryptedData

* 被加密后的字符串

* @param charSet

* 编码

* @return String

* @throws UnsupportedEncodingException

* 编码异常

*/

)

{

;

;

;

;

}

/**

* 加密数据 用生成的密钥加密原始数据

*

* @param primaryData

* 原始数据

* @return byte[]

* @author 宋立君

* @date 2014年07月03日

*/

{

/** 取得安全密钥 */

;

/** DES算法要求有一个可信任的随机数源 */

;

/** 使用原始密钥数据创建DESKeySpec对象 */

;

{

;

{

;

}

/** 创建一个密钥工厂 */

;

{

;

{

;

}

/** 用密钥工厂把DESKeySpec转换成一个SecretKey对象 */

;

{

;

{

;

}

/** Cipher对象实际完成加密操作 */

;

{

;

{

;

{

;

}

/** 用密钥初始化Cipher对象 */

{

;

{

;

}

/** 正式执行加密操作 */

;

{

;

{

;

{

;

{

;

}

/** 返回加密数据 */

;

}

/**

* 用密钥解密数据

*

* @param encryptedData

* 加密后的数据

* @return byte[]

* @author 宋立君

* @date 2014年07月03日

*/

{

/** DES算法要求有一个可信任的随机数源 */

;

/** 取得安全密钥 */

;

/** 使用原始密钥数据创建DESKeySpec对象 */

;

{

;

{

;

}

/** 创建一个密钥工厂 */

;

{

;

{

;

}

/** 用密钥工厂把DESKeySpec转换成一个SecretKey对象 */

;

{

;

{

;

}

/** Cipher对象实际完成加密操作 */

;

{

;

{

;

{

;

}

/** 用密钥初始化Cipher对象 */

{

;

{

;

}

/** 正式执行解密操作 */

;

{

;

{

;

{

;

{

;

}

;

}

/**

* 取得安全密钥 此方法作废,因为每次key生成都不一样导致解密加密用的密钥都不一样, 从而导致Given final block not

* properly padded错误.

*

* @return byte数组

* @author 宋立君

* @date 2014年07月03日

*/

{

/** DES算法要求有一个可信任的随机数源 */

;

/** 为我们选择的DES算法生成一个密钥生成器对象 */

;

{

;

{

;

}

;

/** 生成密钥工具类 */

;

/** 生成密钥byte数组 */

;

;

}

}

Base64.java

Java

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

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

;

;

/**

* Base64 编码和解码。

*

* @author 宋立君

* @date 2014年07月03日

*/

{

{

}

/**

* 功能:编码字符串

*

* @author 宋立君

* @date 2014年07月03日

* @param data

* 源字符串

* @return String

*/

{

;

}

/**

* 功能:解码字符串

*

* @author 宋立君

* @date 2014年07月03日

* @param data

* 源字符串

* @return String

*/

{

;

}

/**

* 功能:编码byte[]

*

* @author 宋立君

* @date 2014年07月03日

* @param data

* 源

* @return char[]

*/

{

;

{

;

;

;

;

{

;

;

}

;

{

;

;

}

;

;

;

;

;

;

;

}

;

}

/**

* 功能:解码

*

* @author 宋立君

* @date 2014年07月03日

* @param data

* 编码后的字符数组

* @return byte[]

*/

{

;

{

{

// ignore non-valid chars and padding

}

}

// calculate required length:

// -- 3 bytes for every 4 valid base64 chars

// -- plus 2 bytes if there are 3 extra base64 chars,

// or plus 1 byte if there are 2 extra.

;

{

;

}

{

;

}

;

// # of excess bits stored in accum

// excess bits

;

// we now go through the entire array (NOT using the 'tempLen' value)

{

;

// skip over non-code

// bits shift up by 6 each time thru

// loop, with new bits being put in

// at the bottom.

// whenever there are 8 or more shifted in,

// write them out (from the top, leaving any

// excess at the bottom for next iteration.

;

}

}

}

// if there is STILL something wrong we just have to throw up now!

{

index

;

}

;

}

/**

* 功能:编码文件

*

* @author 宋立君

* @date 2014年07月03日

* @param file

* 源文件

*/

{

{

;

}

{

;

;

;

}

;

}

/**

* 功能:解码文件。

*

* @author 宋立君

* @date 2014年07月03日

* @param file

* 源文件

* @throws IOException

*/

{

{

;

{

;

;

;

}

;

}

//

// code characters for values 0..63

//

;

//

// lookup table for converting base64 characters to value in range 0..63

//

;

{

{

;

}

{

;

}

{

;

}

{

;

}

;

;

}

{

;

;

;

;

{

;

;

;

;

{

{

;

}

}

;

{

{

)

;

)

;

)

;

{

;

}

}

;

}

{

;

;

;

{

;

;

;

;

{

{

;

}

}

{

{

)

;

)

;

)

;

{

;

}

}

;

}

{

;

;

{

;

;

;

{

{

)

;

)

;

{

;

}

}

}

{

;

;

{

;

;

;

{

{

)

;

)

;

{

;

}

}

}

// /////////////////////////////////////////////////

// end of test code.

// /////////////////////////////////////////////////

}

三、ExcelUtil工具类

Java

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

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

;

/**

* 封装对excel的操作,包括本地读写excel和流中输出excel,支持office 2007。<br/>

* 依赖于poi-3.9-20121203.jar,poi-ooxml-3.9-20121203.jar,poi-ooxml-schemas-3.9-

* 20121203.jar,dom4j-1.6.1.jar<br/>

* 有参构造函数参数为excel的全路径<br/>

*

* @author 宋立君

* @date 2014年07月03日

*/

{

// excel文件路径

;

// 写入excel时,是否自动扩展列宽度来符合内容。

;

/**

* 无参构造函数 默认

*/

{

}

/**

* 有参构造函数

*

* @param path

* excel路径

*/

{

;

}

/**

* 读取某个工作簿上的所有单元格的值。

*

* @param sheetOrder

* 工作簿序号,从0开始。

* @return List<Object[]> 所有单元格的值。

* @throws IOException

* 加载excel文件IO异常。

* @throws FileNotFoundException

* excel文件没有找到异常。

* @throws InvalidFormatException

* @author 宋立君

* @date 2014年07月03日

*/

,

{

;

;

{

;

}

;

// 用来记录excel值

;

// 循环遍历每一行、每一列。

{

// 每一行

;

{

// cell.getCellType是获得cell里面保存的值的type

{

:

// 得到Boolean对象的方法

,

;

;

:

// 先看是否是日期格式

{

// 读取日期格式

,

;

{

;

// 单元格的值,替换掉,

)

;

// 读取数字

,

;

}

;

:

// 读取公式

,

;

;

:

// 读取String

cell

;

;

}

}

// 将这行添加到list。

;

}

;

}

/**

* 读取某个工作簿上的某个单元格的值。

*

* @param sheetOrder

* 工作簿序号,从0开始。

* @param colum

* 列数 从1开始

* @param row

* 行数 从1开始

* @return 单元格的值。

* @throws Exception

* 加载excel异常。

* @author 宋立君

* @date 2014年07月03日

*/

{

;

;

{

;

}

;

;

;

;

;

}

/**

* 在指定的工作簿、行、列书写值。

*

* @param sheetOrder

* 工作簿序号,基于0.

* @param colum

* 列 基于1

* @param row

* 行 基于1

* @param content

* 将要被书写的内容。

* @throws Exception

* 书写后保存异常。

* @author 宋立君

* @date 2014年07月03日

*/

)

{

;

;

{

;

}

;

;

;

;

;

;

;

}

/**

* 得到一个工作区最后一条记录的序号,相当于这个工作簿共多少行数据。

*

* @param sheetOrder

* 工作区序号

* @return int 序号。

* @throws IOException

* 根据excel路径加载excel异常。

* @throws InvalidFormatException

* @author 宋立君

* @date 2014年07月03日

*/

,

{

;

;

{

;

}

;

;

}

/**

* 在磁盘生成一个含有内容的excel,路径为path属性

*

* @param sheetName

* 导出的sheet名称

* @param fieldName

* 列名数组

* @param data

* 数据组

* @throws IOException

* @author 宋立君

* @date 2014年07月03日

*/

,

{

// 在内存中生成工作薄

;

// 截取文件夹路径

;

// 如果路径不存在,创建路径

;

)

;

;

;

;

}

/**

* 在输出流中导出excel。

*

* @param excelName

* 导出的excel名称 包括扩展名

* @param sheetName

* 导出的sheet名称

* @param fieldName

* 列名数组

* @param data

* 数据组

* @param response

* response

* @throws IOException

* 转换流时IO错误

* @author 宋立君

* @date 2014年07月03日

*/

,

,

{

;

// 清空输出流

// 取得输出流

// 设定输出文件头

// 定义输出类型

// 在内存中生成工作薄

;

;

;

}

/**

* 根据条件,生成工作薄对象到内存。

*

* @param sheetName

* 工作表对象名称

* @param fieldName

* 首列列名称

* @param data

* 数据

* @return HSSFWorkbook

* @author 宋立君

* @date 2014年07月03日

*/

,

{

// 用来记录最大列宽,自动调整列宽。

;

// 产生工作薄对象

;

// 产生工作表对象

;

// 为了工作表能支持中文,设置字符集为UTF_16

;

// 产生一行

;

// 产生单元格

;

// 写入各个字段的名称

{

// 创建第一行各个字段名称的单元格

;

// 设置单元格内容为字符串型

;

// 为了能在单元格中输入中文,设置字符集为UTF_16

// cell.setEncoding(HSSFCell.ENCODING_UTF_16);

// 给单元格内容赋值

;

// 初始化列宽

;

}

// 临时单元格内容

;

// 写入各条记录,每条记录对应excel表中的一行

{

;

// 生成一行

;

{

;

// 设置单元格字符类型为String

;

;

;

// 如果自动调整列宽度。

{

// 标题列数小于数据列数时。

,

;

{

// 如果这个内容的宽度大于之前最大的,就按照这个设置宽度。

{

;

}

}

}

}

}

// 自动调整列宽度。

{

// 调整列为这列文字对应的最大宽度。

{

;

}

}

;

}

/**

* 功能:设置写入excel时,是否自动扩展列宽度来符合内容,默认为false。

*

* @author 宋立君

* @date 2014年07月03日

* @param autoColumnWidth

* true或者false

*/

{

;

}

}

依赖包下载地址:链接:http://pan.baidu.com/s/1ntof9pv 密码:6j5s