View difference between Paste ID: 91WrwCk8 and HrP1kDmQ
SHOW: | | - or go back to the newest paste.
1
package com.example.javaqui;
2
3-
import java.awt.GridBagConstraints;
3+
import java.io.BufferedReader;
4-
import java.awt.GridBagLayout;
4+
import java.io.BufferedWriter;
5-
import java.awt.event.ActionEvent;
5+
import java.io.File;
6-
import java.awt.event.ActionListener;
6+
import java.io.FileInputStream;
7
import java.io.FileOutputStream;
8-
import javax.swing.JButton;
8+
import java.io.FileReader;
9-
import javax.swing.JFrame;
9+
import java.io.FileWriter;
10-
import javax.swing.JLabel;
10+
import java.io.IOException;
11-
import javax.swing.JOptionPane;
11+
import java.io.InputStreamReader;
12-
import javax.swing.JPanel;
12+
import java.io.ObjectInputStream;
13-
import javax.swing.JPasswordField;
13+
import java.io.ObjectOutputStream;
14-
import javax.swing.JTextField;
14+
import java.sql.Connection;
15
import java.sql.DriverManager;
16-
public class login implements ActionListener
16+
import java.sql.PreparedStatement;
17
import java.sql.ResultSet;
18-
	JFrame myframe;
18+
import java.sql.SQLException;
19-
	JPanel mypanel;
19+
import java.sql.Statement;
20-
	JLabel lblusername,lblpassword;
20+
import java.util.ArrayList;
21-
	JTextField tusername,tpassword;
21+
22-
	JButton btnlogin;
22+
public class studentslist 
23-
	GridBagLayout g;
23+
24-
	GridBagConstraints gbc;
24+
	String datasource;
25-
	public login()
25+
	ArrayList<student> students;
26
	BufferedReader reader;
27-
		//initializations
27+
	int choice;
28-
		myframe=new JFrame("Login");
28+
	int filereadtoken;
29-
		mypanel=new JPanel();
29+
	//1 is for file,2 is for serialized data,3 is for database
30-
		lblusername=new JLabel("User Name");
30+
	//for database only
31-
		lblpassword=new JLabel("Password");
31+
	private Connection con=null;
32-
		tusername=new JTextField(15);
32+
	private Statement st=null;
33-
		tpassword=new JPasswordField(15);
33+
	ResultSet rs=null;
34-
		btnlogin=new JButton("L O G I N");
34+
	String query=null;
35-
		g=new GridBagLayout();
35+
	String constring="jdbc:mysql://localhost/test?user=root&password=";
36-
		gbc=new GridBagConstraints();
36+
37
	public studentslist()
38-
		//placements
38+
39-
		mypanel.setLayout(g);
39+
		datasource="Not Choosen";
40
		filereadtoken=0;
41-
		gbc.anchor=GridBagConstraints.NORTHWEST;
41+
		reader=new BufferedReader(new InputStreamReader(System.in));
42-
		gbc.gridx=1;
42+
		showmenu();
43-
		gbc.gridy=5;
43+
44-
		g.setConstraints(lblusername,gbc);
44+
	public void showmenu()
45-
		mypanel.add(lblusername);
45+
46
		while(true){
47-
		gbc.anchor=GridBagConstraints.NORTHWEST;
47+
		clearscreen();
48-
		gbc.gridx=5;
48+
		System.out.println("Data Source Choosen : "+ datasource);
49-
		gbc.gridy=5;
49+
		System.out.println("1. Choose plain text as data source");
50-
		g.setConstraints(tusername,gbc);
50+
		System.out.println("2. Choose serialized text as data source");
51-
		mypanel.add(tusername);
51+
		System.out.println("3. Choose db as data source");
52
		System.out.println("4. Add Student");
53-
		gbc.anchor=GridBagConstraints.NORTHWEST;
53+
		System.out.println("5. List Students");
54-
		gbc.gridx=1;
54+
		System.out.println("Enter your choice [1,2,3,4,5]");
55-
		gbc.gridy=10;
55+
		try 
56-
		g.setConstraints(lblpassword,gbc);
56+
57-
		mypanel.add(lblpassword);
57+
			choice=Integer.parseInt(reader.readLine());
58
			switch(choice)
59-
		gbc.anchor=GridBagConstraints.NORTHWEST;
59+
60-
		gbc.gridx=5;
60+
				case 1:
61-
		gbc.gridy=10;
61+
				{
62-
		g.setConstraints(tpassword,gbc);
62+
					datasource="PlainText";
63-
		mypanel.add(tpassword);
63+
					filereadtoken=1;
64
					break;
65-
		gbc.anchor=GridBagConstraints.NORTHWEST;
65+
				}
66-
		gbc.gridx=5;
66+
				case 2:
67-
		gbc.gridy=15;
67+
				{
68-
		g.setConstraints(btnlogin,gbc);
68+
					datasource="Serialization";
69-
		mypanel.add(btnlogin);
69+
					filereadtoken=2;
70
					if(deserializedata()==null)
71-
		btnlogin.addActionListener(this);		
71+
						students=new ArrayList<student>();
72-
		tpassword.addActionListener(this);		
72+
					else
73
						students=deserializedata();
74-
		myframe.getContentPane().add(mypanel);
74+
					break;
75-
		myframe.setSize(400, 400);
75+
				}
76-
		myframe.setVisible(true);
76+
				case 3:
77
				{
78
					datasource="Database";
79
					filereadtoken=3;
80-
		new login();
80+
					break;
81
				}
82-
	@Override
82+
				case 4:
83-
	public void actionPerformed(ActionEvent e) 
83+
				{
84
					if(checkdatasource())
85-
		Object obj=e.getSource();
85+
						getstudentdetails();
86-
		String username=tusername.getText().toString();
86+
					break;
87-
		String password=tpassword.getText().toString();
87+
				}
88-
		if(obj==btnlogin)
88+
				case 5:
89-
		{			
89+
				{
90-
			if(username.length()==0 || password.length() ==0)
90+
					if(checkdatasource())
91
					{
92-
				JOptionPane.showMessageDialog(myframe, "Incomplete Data");
92+
						if(filereadtoken==1)
93
							readfromfile();
94-
			else
94+
						else if(filereadtoken==2)
95
							printstudentdetails();
96-
				checklogin(username,password);				
96+
						else if(filereadtoken==3)
97
							printstudentsdb();
98
					}
99
					break;
100-
		if(obj==tpassword)
100+
				}
101
				default:
102-
			checklogin(username,password);
102+
				{
103
					System.out.println("Invalid Choice");
104
					break;
105-
	private void checklogin(String username, String password)
105+
				}
106
			}
107-
		dboperations operation1=new dboperations();
107+
		} 
108-
		operation1.makecon();
108+
		catch (IOException e) 
109-
		if(operation1.validateuser(username, password))
109+
110
			e.printStackTrace();
111-
			new menu();
111+
112-
			myframe.setVisible(false);
112+
113
	}
114
	public ArrayList<student> deserializedata()
115
	{
116-
			JOptionPane.showMessageDialog(myframe, "Not Validated !");
116+
		FileInputStream fileIn=null;
117-
		}		
117+
		ObjectInputStream inStream=null;
118
		try
119
		{
120
			fileIn=new FileInputStream("data");
121
			inStream=new ObjectInputStream(fileIn);
122
			students=(ArrayList<student>) inStream.readObject();
123
			return students;
124
		}
125
		catch(IOException e)
126
		{
127
			//e.printStackTrace();
128
			System.out.println(e.getMessage());
129
			return null;
130
		}
131
		catch(ClassNotFoundException c)
132
		{c.printStackTrace();return null;}
133
		finally
134
		{
135
			
136
			try
137
			{
138
				fileIn.close();
139
				inStream.close();
140
			}
141
			catch(Exception e)
142
			{
143
				return null;
144
			}
145
		}
146
	}
147
	public void serializedata()
148
	{
149
		FileOutputStream fileOut=null;
150
		ObjectOutputStream outStream=null;
151
		try
152
		{
153
			fileOut=new FileOutputStream("data");
154
			outStream=new ObjectOutputStream(fileOut);
155
			outStream.writeObject(students);
156
		}
157
		catch(IOException e)
158
		{
159
			e.printStackTrace();
160
		}
161
		finally
162
		{
163
			try {
164
				fileOut.close();
165
				outStream.close();
166
			} catch (IOException e) {
167
				// TODO Auto-generated catch block
168
				e.printStackTrace();
169
			}
170
171
		}
172
	}
173
	public void readfromfile()
174
	{
175
		File file;
176
		FileReader fileReader=null;
177
		BufferedReader bufferedReader=null;
178
		try
179
		{
180
			file=new File("file.txt");
181
			fileReader=new FileReader(file);
182
			bufferedReader=new BufferedReader(fileReader);
183
			String line;
184
			while((line=bufferedReader.readLine())!=null)
185
			{
186
				System.out.println(line);
187
			}
188
			String dummy=reader.readLine();
189
		}
190
		catch(IOException e)
191
		{e.printStackTrace();}
192
		finally
193
		{
194
			try{
195
				if(fileReader!=null) 
196
					fileReader.close();
197
				if(bufferedReader!=null) 
198
					bufferedReader.close();
199
			}catch(IOException e){;}			
200
		}
201
	}
202
	public void writetofile(ArrayList<String> data)
203
	{
204
		String student=data.get(0)+","+data.get(1)+","+data.get(2)+","+data.get(3);
205
		try
206
		{
207
			File file=new File("file.txt");
208
			FileWriter filewriter=new FileWriter(file.getName(),true);
209
			//BufferedWriter bufferedwriter=new BufferedWriter(filewriter);
210
			filewriter.write("\n");
211
			filewriter.write(student);
212
			filewriter.close();
213
		}
214
		catch(IOException e)
215
		{
216
			e.printStackTrace();
217
		}
218
	}
219
	public Boolean checkdatasource()
220
	{
221
		if(filereadtoken==0)
222
		{
223
			System.out.println("Please choose data source to proceed !");
224
			try
225
			{
226
				String dummy=reader.readLine();
227
			}
228
			catch(IOException e)
229
			{e.printStackTrace();}
230
			return false;
231
		}
232
		else
233
			return true;
234
	}
235
	public void clearscreen()
236
	{
237
		for(int clear=0;clear<1000;clear++)
238
		{
239
			System.out.println("");
240
		}
241
	}
242
	public void getstudentdetails()
243
	{
244
		try{
245
		clearscreen();
246
		System.out.println("Enter Student Number");
247
		String sno=reader.readLine();
248
		System.out.println("Enter Student Name");
249
		String sname=reader.readLine();
250
		System.out.println("Enter Subject Code");
251
		String sbcode=reader.readLine();
252
		System.out.println("Enter Subject Name");
253
		String sbname=reader.readLine();
254
		if(filereadtoken==1)
255
		{
256
			ArrayList<String> data=new ArrayList<String>();
257
			data.add(sno);
258
			data.add(sname);
259
			data.add(sbcode);
260
			data.add(sbname);
261
			writetofile(data);
262
		}
263
		else if(filereadtoken==2)
264
		{
265
			student s=new student(sno,sname,sbcode,sbname);
266
			students.add(s);
267
			serializedata();
268
			
269
		}
270
		else if(filereadtoken==3)
271
		{
272
			student s=new student(sno,sname,sbcode,sbname);
273
			writetodb(s);
274
			
275
		}
276
		else
277
			System.out.println("Data source not choosen");		
278
		}
279
		catch(IOException e)
280
		{
281
			System.out.println(e.getMessage());
282
		}
283
	}
284
	public void printstudentdetails()
285
	{
286
		clearscreen();
287
		for(student s:students)
288
		{
289
			System.out.println(s.snumber);
290
			System.out.println(s.sname);
291
			System.out.println(s.sbcode);
292
			System.out.println(s.sbname);
293
		}
294
		try
295
		{
296
			reader.readLine();
297
		}
298
		catch(IOException e)
299
		{
300
			e.printStackTrace();
301
		}
302
	}
303
	public void makecon()
304
	{
305
		try 
306
		{
307
			Class.forName("com.mysql.jdbc.Driver");
308
			con=DriverManager.getConnection(constring);
309
			//st=con.createStatement();
310
		} 
311
		catch (Exception e) 
312
		{
313
			// TODO Auto-generated catch block
314
			e.printStackTrace();			
315
		}
316
		
317
	}
318
	public Boolean writetodb(student s)
319
	{
320
		try
321
		{
322
			makecon();
323
			query="insert into students(studentno,studentname,subjectcode,subjectname) values(?,?,?,?)";
324
			PreparedStatement ps=con.prepareStatement(query);
325
			ps.setString(1,s.snumber);
326
			ps.setString(2,s.sname);
327
			ps.setString(3,s.sbcode);
328
			ps.setString(4,s.sbname);
329
			//System.out.println(query);
330
			ps.executeUpdate();
331
			System.out.println("student inserted !");
332
				return true;
333
		}
334
		catch(Exception e)
335
		{
336
			System.out.println(e);
337
			return false;
338
		}
339
		finally
340
		{
341
			try {
342
				con.close();
343
			} catch (SQLException e) {
344
				// TODO Auto-generated catch block
345
				System.out.print(e.getMessage());
346
			}
347
		}
348
	}
349
	public void printstudentsdb()
350
	{
351
		try
352
		{
353
			makecon();
354
			query="select * from students";
355
			PreparedStatement ps=con.prepareStatement(query);
356
			ResultSet rs=ps.executeQuery();
357
			while(rs.next())
358
			{
359
				System.out.print(rs.getString(1));
360
				System.out.print(",");
361
				System.out.print(rs.getString(2));
362
				System.out.print(",");
363
				System.out.print(rs.getString(3));
364
				System.out.print(",");
365
				System.out.print(rs.getString(4));
366
				System.out.println();
367
			}
368
			reader.readLine();
369
		}
370
		catch(Exception e)
371
		{
372
			System.out.println(e.getMessage());
373
		}
374
	}
375
	public static void main(String args[])
376
	{
377
		new studentslist();
378
	}
379
}