-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJAVA.txt
More file actions
305 lines (242 loc) · 9.46 KB
/
JAVA.txt
File metadata and controls
305 lines (242 loc) · 9.46 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
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
insert
---------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String userid=request.getParameter("userid");
String username=request.getParameter("username");
String password=request.getParameter("password");
String phonenumber=request.getParameter("phonenumber");
String emailid=request.getParameter("emailid");
String city=request.getParameter("city");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/food";
String usr="root";
String pwd="";
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(url,usr,pwd);
Statement st=con.createStatement();
String query="insert into login values('"+Integer.parseInt(userid)+"','"+username+"','"+password+"','"+Integer.parseInt(phonenumber)+"','"+emailid+"','"+city+"')";
int i=st.executeUpdate(query);
out.print(i+"record inserted");
out.print("<br><a href='display'>display</a>");
}
catch (Exception e) {
e.printStackTrace();
}
------------------------------------------------------------------
Display
----------------------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/food";
String usr="root";
String pwd="";
out.print("<a href='productlist'>productlist<a>");
HttpSession session=request.getSession(false);
if(session!=null)
{
out.print("welcome, "+session.getAttribute("username"));
out.print("<a href='logout'>logout</a>");
}
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(url,usr,pwd);
Statement st=con.createStatement();
String query="select * from login";
ResultSet rs=st.executeQuery(query);
out.print("<table>");
while(rs.next()) {
out.print("<tr><td>"+rs.getString(1)+"</td>"
+ "<td>"+rs.getString(2)+"</td>"
+ "<td>"+rs.getString(3)+"</td>"
+ "<td>"+rs.getString(4)+"</td>"
+ "<td>"+rs.getString(5)+"</td>"
+ "<td>"+rs.getString(6)+"</td>"
+ "<td><a href='delete?id="+rs.getString(1)+"'>delete</a></td>"
+ "<td><a href='updateform?id="+ rs.getString(1)+"'>update</a></td></tr>");
}
out.print("</table>");
}
catch (Exception e) {
e.printStackTrace();
}
------------------------------------------------------------------
Delete
-------------------------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/food";
String usr="root";
String pswd="";
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(url,usr,pswd);
Statement st=con.createStatement();
String query="delete from login where userid='"+request.getParameter("id")+"'";
st.executeUpdate(query);
RequestDispatcher rd=request.getRequestDispatcher("display");
rd.forward(request,response);
}
catch (Exception e) {
e.printStackTrace();
}
------------------------------------------------------------------
updateform
------------------------------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String id=request.getParameter("id");
out.print("<form action='update'><table>"
+"<tr><td>userid:</td><td><input type='number'name='userid'></td></tr>"
+"<tr><td>username:</td><td><input type='text'name='username'></td></tr>"
+"<tr><td>password:</td><td><input type='text'name='password'></td></tr>"
+"<tr><td>phonenumber:</td><td><input type='text'name='phonenumber'></td></tr>"
+"<tr><td>emailid:</td><td><input type='text'name='emailid'></td></tr>"
+"<tr><td>city:</td><td><input type='text'name='city'></td></tr>"
+"<tr><td><input type='submit'value='update'></td></tr>"
+"<tr><td><input type='hidden' name='id' value='"+id+"'></td></tr></form></table>");
}
------------------------------------------------------------------
update
---------------------------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/food";
String usr="root";
String pwd="";
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(url,usr,pwd);
Statement st=con.createStatement();
String query="update login set userid=?,username=?,password=?,phonenumber=?,emailid=?,city=? where userid=?";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1, request.getParameter("userid"));
ps.setString(2, request.getParameter("username"));
ps.setString(3, request.getParameter("password"));
ps.setString(4, request.getParameter("phonenumber"));
ps.setString(5, request.getParameter("emailid"));
ps.setString(6, request.getParameter("city"));
ps.setString(7, request.getParameter("id"));
ps.executeUpdate();
RequestDispatcher rd=request.getRequestDispatcher("display");
rd.forward(request,response);
}
catch (Exception e) {
e.printStackTrace();
}
------------------------------------------------------------------
login
--------------------------------------------------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/food";
String usr="root";
String pswd="";
String username=request.getParameter("username");
String password=request.getParameter("password");
try
{
Class.forName(driver);
Connection con=DriverManager.getConnection(url,usr,pswd);
String query="select * from login where username=? and password=?";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs=(ResultSet) ps.executeQuery();
if(rs.next())
{
HttpSession session=request.getSession(true);
session.setAttribute("username", username);
ArrayList<productbeans> list=new ArrayList<productbeans>();
session.setAttribute("list", list);
RequestDispatcher rd=request.getRequestDispatcher("display");
rd.forward(request, response);
}
else
{
out.print("<br>username password invalid<br>");
RequestDispatcher rd=request.getRequestDispatcher("login.html");
rd.forward(request, response);
}
}
catch (Exception e) {
e.printStackTrace();
}
------------------------------------------------------------------
logout
-----------------------------------------------------------------------
HttpSession session=request.getSession(false);
session.invalidate();
RequestDispatcher rd=request.getRequestDispatcher("login.html");
rd.forward(request, response);
------------------------------------------------------------------
productlist
---------------------------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/product";
String usr="root";
String pwd="";
out.print("<a href='displaycart'>viewcart<a>");
try {
Class.forName(driver);
Connection con=DriverManager.getConnection(url,usr,pwd);
Statement st=con.createStatement();
String query="select * from productdetails";
ResultSet rs=st.executeQuery(query);
out.print("<table>");
while(rs.next()) {
out.print("<tr><td>"+rs.getInt(1)+"</td>"
+ "<td>"+rs.getString(2)+"</td>"
+ "<td>"+rs.getString(3)+"</td>"
+ "<td>"+rs.getInt(4)+"</td>"
+ "<td><a href='addprodservlet?prodid="+ rs.getInt(1)+"'>addtocart</a></td></tr>");
}
out.print("</table>");
}
catch (Exception e) {
e.printStackTrace();
}
------------------------------------------------------------------
productbeans
---------------------------------------------------------
package FOOD;
public class productbeans
{
public int prodid;
public int getProdid() {
return prodid;
}
public void setProdid(int prodid) {
this.prodid = prodid;
}
}
------------------------------------------------------------------
addprodservlet
-----------------------------------------------------------------------
HttpSession session=request.getSession();
String prodid=request.getParameter("prodid");
productbeans p=new productbeans();
p.setProdid(Integer.parseInt(prodid));
ArrayList<productbeans> list=(ArrayList<productbeans>) session.getAttribute("list");
list.add(p);
response.sendRedirect("productlist");
------------------------------------------------------------------
displaycart
-------------------------------------------------------------------------
PrintWriter out=response.getWriter();
response.setContentType("text/html");
HttpSession session=request.getSession(false);
ArrayList<productbeans> list=(ArrayList<productbeans>) session.getAttribute("list");
productbeans p=new productbeans();
for(int i=0;i<list.size();i++)
{
p=list.get(i);
out.print(p.getProdid()+"<br>");
}