-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectToSqlDB.java
More file actions
246 lines (218 loc) · 8.74 KB
/
ConnectToSqlDB.java
File metadata and controls
246 lines (218 loc) · 8.74 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
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
package databases;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Created by mrahman on 04/02/18.
*/
public class ConnectToSqlDB {
public static Connection connect = null;
public static Statement statement = null;
public static PreparedStatement ps = null;
public static ResultSet resultSet = null;
public static Properties loadProperties() throws IOException{
Properties prop = new Properties();
InputStream ism = new FileInputStream("src/secret.properties");
prop.load(ism);
ism.close();
return prop;
}
public static Connection connectToSqlDatabase() throws IOException, SQLException, ClassNotFoundException {
Properties prop = loadProperties();
String driverClass = prop.getProperty("MYSQLJDBC.driver");
String url = prop.getProperty("MYSQLJDBC.url");
String userName = prop.getProperty("MYSQLJDBC.userName");
String password = prop.getProperty("MYSQLJDBC.password");
Class.forName(driverClass);
connect = DriverManager.getConnection(url,userName,password);
System.out.println("Database is connected");
return connect;
}
public List<String> readDataBase(String tableName, String columnName)throws Exception{
List<String> data = new ArrayList<String>();
try {
connectToSqlDatabase();
statement = connect.createStatement();
resultSet = statement.executeQuery("select * from " + tableName);
data = getResultSetData(resultSet, columnName);
} catch (ClassNotFoundException e) {
throw e;
}finally{
close();
}
return data;
}
private void close() {
try{
if(resultSet != null){
resultSet.close();
}
if(statement != null){
statement.close();
}
if(connect != null){
connect.close();
}
}catch(Exception e){
}
}
private List<String> getResultSetData(ResultSet resultSet2, String columnName) throws SQLException {
List<String> dataList = new ArrayList<String>();
while(resultSet.next()){
String itemName = resultSet.getString(columnName);
dataList.add(itemName);
}
return dataList;
}
public void insertDataFromArrayToSqlTable(int [] ArrayData, String tableName, String columnName)
{
try {
connectToSqlDatabase();
ps = connect.prepareStatement("DROP TABLE IF EXISTS `"+tableName+"`;");
ps.executeUpdate();
ps = connect.prepareStatement("CREATE TABLE `"+tableName+"` (`ID` int(11) NOT NULL AUTO_INCREMENT,`SortingNumbers` bigint(20) DEFAULT NULL, PRIMARY KEY (`ID`) );");
ps.executeUpdate();
for(int n=0; n<ArrayData.length; n++){
ps = connect.prepareStatement("INSERT INTO "+tableName+" ( "+columnName+" ) VALUES(?)");
ps.setInt(1,ArrayData[n]);
ps.executeUpdate();
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void insertDataFromStringToSqlTable(String ArrayData, String tableName, String columnName)
{
try {
connectToSqlDatabase();
ps = connect.prepareStatement("INSERT INTO "+tableName+" ( "+columnName+" ) VALUES(?)");
ps.setString(1,ArrayData);
ps.executeUpdate();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public List<String> directDatabaseQueryExecute(String passQuery,String dataColumn)throws Exception{
List<String> data = new ArrayList<String>();
try {
connectToSqlDatabase();
statement = connect.createStatement();
resultSet = statement.executeQuery(passQuery);
data = getResultSetData(resultSet, dataColumn);
} catch (ClassNotFoundException e) {
throw e;
}finally{
close();
}
return data;
}
public void insertDataFromArrayListToSqlTable(List<Student> list, String tableName, String columnName)
{
try {
connectToSqlDatabase();
ps = connect.prepareStatement("DROP TABLE IF EXISTS `"+tableName+"`;");
ps.executeUpdate();
ps = connect.prepareStatement("CREATE TABLE `"+tableName+"` (`ID` int(11) NOT NULL AUTO_INCREMENT,`SortingNumbers` bigint(20) DEFAULT NULL, PRIMARY KEY (`ID`) );");
ps.executeUpdate();
for(Student st:list){
ps = connect.prepareStatement("INSERT INTO "+tableName+" ( "+columnName+" ) VALUES(?)");
ps.setObject(1,st);
ps.executeUpdate();
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void insertUserToSqlTable(List<Employee> list, String tableName, String columnName1,String columnName2,String columnName3)
{
try {
connectToSqlDatabase();
ps = connect.prepareStatement("DROP TABLE IF EXISTS `"+tableName+"`;");
ps.executeUpdate();
ps = connect.prepareStatement("CREATE TABLE "+tableName+"("+columnName1+" VARCHAR(255),"+columnName2+" VARCHAR(255),"+columnName3+" VARCHAR(255));");
ps.executeUpdate();
for(Employee user:list){
ps = connect.prepareStatement("INSERT INTO "+tableName+" ( "+columnName1+","+columnName2+","+columnName3+" ) VALUES(?,?,?)");
ps.setObject(1,user.getEmpName());
ps.setObject(2,user.getEmpID());
ps.setObject(3,user.getEmpDOB());
ps.executeUpdate();
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void insertProfileToSqlTable(String tableName, String columnName1, String columnName2)
{
try {
connectToSqlDatabase();
ps = connect.prepareStatement("INSERT INTO "+tableName+" ( " + columnName1 + "," + columnName2 + " ) VALUES(?,?)");
ps.setString(1,"Ankita Sing");
ps.setInt(2,3590);
ps.executeUpdate();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static List<User> readUserProfileFromSqlTable()throws IOException, SQLException, ClassNotFoundException{
List<User> list = new ArrayList<>();
User user = null;
try{
Connection conn = connectToSqlDatabase();
String query = "SELECT * FROM Students";
// create the java statement
Statement st = conn.createStatement();
// execute the query, and get a java resultset
ResultSet rs = st.executeQuery(query);
// iterate through the java resultset
while (rs.next())
{
String name = rs.getString("stName");
String id = rs.getString("stID");
String dob = rs.getString("stDOB");
//System.out.format("%s, %s\n", name, id);
user = new User(name,id, dob);
list.add(user);
}
st.close();
}catch (Exception e){
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
return list;
}
public static void main(String[] args)throws IOException, SQLException, ClassNotFoundException {
List<User> list = readUserProfileFromSqlTable();
for(User user:list){
System.out.println(user.getStName() + " " + user.getStID()+ " " + user.getStDOB());
}
List<Employee> userList = new ArrayList<Employee>();
userList.add(new Employee("Ahsan","8923","11-15-1990"));
userList.add(new Employee("Valerie","2134","07-18-1993"));
insertUserToSqlTable(userList,"Employee","empName","empID","empDOB");
}
}