Repository navigation
Expand file tree
/
Copy pathCommonDAO.java
More file actions
executable file
·75 lines (67 loc) · 1.76 KB
/
Copy pathCommonDAO.java
File metadata and controls
executable file
·75 lines (67 loc) · 1.76 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
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CommonDAO {
// 根据用户id获得用户名字
public static String getUserNameById(Integer uId) {
BaseDAO dao = new BaseDAO();
ResultSet rs = null;
String sql = "select * from userInfo where uId = ?";
try {
rs = dao.executeQuery(sql, new Object[] { uId });
if (rs != null && rs.next()) {
return rs.getString("uName");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return null;
}
// 格式化时间
public static String getDateFormat(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (date != null) {
return sdf.format(date);
}
return "";
}
/**
* 把一个字符串替换成另一个字符串
*
* @param input
* 原字符串
* @param needReplaceStr
* 需要替换的参数
* @param replaceStr
* 替换的参数
* @return String 返回替换后的结果
*/
public static String replace(String input, String needReplaceStr,
String replaceStr) {
if (input == null || input.length() == 0
|| input.indexOf(needReplaceStr, 0) < 0) {
return input;
} else {
int i = 0;
int j = 0;
int needReplaceLength = needReplaceStr.length();
StringBuffer buffer = new StringBuffer();
while ((i = input.indexOf(needReplaceStr, i)) >= 0) {
buffer.append(input.substring(j, i)).append(replaceStr);
j = (i += needReplaceLength);
}
input = buffer.append(input.substring(j)).toString();
buffer = null;
return input;
}
}
// 测试
public static void main(String[] args) {
}
}