-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseSession.java
More file actions
164 lines (149 loc) · 2.98 KB
/
DatabaseSession.java
File metadata and controls
164 lines (149 loc) · 2.98 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package darun.csvloader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
public class DatabaseSession {
DataSource dSource = null;
Connection conn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rs = null;
public DatabaseSession(){
try {
dSource = DataSourceFactory.getDataSource();
conn = dSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public void prepareStatement(String sql){
try{
if(sql != null){
pstmt = conn.prepareStatement(sql);
conn.setAutoCommit(false);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addBatch(String[] values){
try{
if(pstmt != null){
for(int i=0; i<values.length; i++){
pstmt.setString(i+1, values[i]);
}
pstmt.addBatch();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addParam(int pos, Object obj, int type){
try{
if(pstmt != null){
pstmt.setObject(pos, obj, type);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public int[] executeBatch(){
int[] count = null;
try{
if(pstmt != null){
count = pstmt.executeBatch();
conn.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public int executeUpdate(){
int count = 0;
try{
if(pstmt != null){
count = pstmt.executeUpdate();
conn.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public ResultSet executeQuery(){
try{
if(pstmt != null){
rs = pstmt.executeQuery();
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public ResultSet executeQuery(String sql){
try{
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(sql != null && conn != null){
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String sql){
int count = 0;
try{
if(rs != null){
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(sql != null && conn != null){
stmt = conn.createStatement();
count = stmt.executeUpdate(sql);
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public void close(){
try {
if(rs != null){
rs.close();
rs = null;
}
if(pstmt != null){
pstmt.close();
pstmt = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
//System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}