forked from shreayan98c/Udaan-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRUDFunctions.java
226 lines (196 loc) · 7.56 KB
/
CRUDFunctions.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.srm.udaanapp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author shrea
*/
public class CRUDFunctions {
public void display() {
Connection con = null;
Statement statement1 = null;
ResultSet result = null;
try {
//step 1 - connect to driver(load driver)
Class.forName("com.mysql.cj.jdbc.Driver");
String user = "root"; //root@localhost
String pass = "mysql";
//step 2- form a connection
con = DriverManager.getConnection("jdbc:mysql://localhost/udaan", user, pass);
System.out.println("Successfullly connected to db!");
String sql1 = "select * from udaan.asset;";
statement1 = con.createStatement();
result = statement1.executeQuery(sql1);
int count = 0;
while (result.next()) {
int assetid = result.getInt(1);
String assetname = result.getString(2);
int assetcount = result.getInt(3);
String output = "Employee #%d: %s - %d";
System.out.println(String.format(output, assetid, assetname, assetcount));
}
} catch (SQLException err) {
System.out.println(err.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
// finally {
// try {
// result.close();
// } catch (SQLException err) {
// err.getMessage();
// }
// try {
// statement1.close();
// } catch (SQLException err) {
// err.getMessage();
// }
// try {
// con.close();
// } catch (SQLException err) {
// err.getMessage();
// }
// }
}
public int insert(String assetname, int assetcount) {
Connection con = null;
PreparedStatement statement = null;
try {
//step 1 - connect to driver(load driver)
Class.forName("com.mysql.cj.jdbc.Driver");
String user = "root"; //root@localhost
String pass = "mysql";
//step 2- form a connection
con = DriverManager.getConnection("jdbc:mysql://localhost/udaan", user, pass);
System.out.println("Successfullly connected to db!");
//step 3 - inserting to table
String sql = "INSERT INTO udaan.asset(assetname,assetcount) VALUES(?,?); ";
statement = con.prepareStatement(sql);
statement.setString(1, assetname);
statement.setInt(2, assetcount);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
return 1;
}
} catch (SQLException err) {
System.out.println(err.getMessage());
err.printStackTrace();
return 0;
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return 0;
}// finally {
/* try {
statement.close();
} catch (SQLException err) {
err.getMessage();
}
try {
con.close();
} catch (SQLException err) {
err.getMessage();
}
}*/ return 0;
}
public int insert_task(String taskname, String taskcreate, int taskfreq, String taskdeadline, int taskworkerid) {
Connection con = null;
PreparedStatement statement = null;
try {
//step 1 - connect to driver(load driver)
Class.forName("com.mysql.cj.jdbc.Driver");
String user = "root"; //root@localhost
String pass = "mysql";
//step 2- form a connection
con = DriverManager.getConnection("jdbc:mysql://localhost/udaan", user, pass);
System.out.println("Successfullly connected to db!");
//step 3 - inserting to table
String sql = "INSERT INTO udaan.task(taskname,taskcreatedat,taskfreq,taskdeadline,taskworkerid) VALUES(?,?,?,?,?); ";
statement = con.prepareStatement(sql);
statement.setString(1, taskname);
statement.setString(2, taskcreate);
statement.setInt(3, taskfreq);
statement.setString(4, taskdeadline);
statement.setInt(5, taskworkerid);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
return 1;
}
} catch (SQLException err) {
System.out.println(err.getMessage());
err.printStackTrace();
return 0;
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return 0;
}// finally {
/* try {
statement.close();
} catch (SQLException err) {
err.getMessage();
}
try {
con.close();
} catch (SQLException err) {
err.getMessage();
}
}*/ return 0;
}
public int insert_worker(String workername, String workercreate, String workeremail, String workerphone, String workergender) {
Connection con = null;
PreparedStatement statement = null;
try {
//step 1 - connect to driver(load driver)
Class.forName("com.mysql.cj.jdbc.Driver");
String user = "root"; //root@localhost
String pass = "mysql";
//step 2- form a connection
con = DriverManager.getConnection("jdbc:mysql://localhost/udaan", user, pass);
System.out.println("Successfullly connected to db!");
//step 3 - inserting to table
String sql = "INSERT INTO udaan.worker(workername,workercreatedat,workeremail,workerphone,workergender) VALUES(?,?,?,?,?); ";
statement = con.prepareStatement(sql);
statement.setString(1, workername);
statement.setString(2, workercreate);
statement.setString(3, workeremail);
statement.setString(4, workerphone);
statement.setString(5, workergender);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
return 1;
}
} catch (SQLException err) {
System.out.println(err.getMessage());
err.printStackTrace();
return 0;
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return 0;
}// finally {
/* try {
statement.close();
} catch (SQLException err) {
err.getMessage();
}
try {
con.close();
} catch (SQLException err) {
err.getMessage();
}
}*/ return 0;
}
}