Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 KB | None | 0 0
  1. package userauth;
  2. import java.awt.Cursor;
  3. import java.awt.SystemColor;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8. import java.io.IOException;
  9. import java.net.InetAddress;
  10. import java.net.URISyntaxException;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.PreparedStatement;
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JButton;
  16. import javax.swing.JComboBox;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTextField;
  22. import javax.swing.SwingConstants;
  23. import javax.swing.text.AbstractDocument;
  24. import javax.swing.text.AttributeSet;
  25. import javax.swing.text.BadLocationException;
  26. import javax.swing.text.DocumentFilter;
  27.  
  28. class Home
  29. {
  30. static JFrame frame,frame1;
  31. static String facname,stdid;
  32. Home() throws IOException,URISyntaxException
  33. {
  34.  
  35. frame = new JFrame("Application");
  36. frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  37. frame.setUndecorated(true);
  38.  
  39. frame.setAlwaysOnTop(true);
  40. JPanel contentPane = new JPanel();
  41. contentPane.setOpaque(true);
  42. contentPane.setBackground(SystemColor.activeCaption);
  43. contentPane.setLayout(null);
  44. java.net.URL url = Home.class.getResource("/fac.png");
  45.  
  46. ImageIcon facimage = new ImageIcon(url);
  47. JLabel faclab = new JLabel(facimage);
  48.  
  49. faclab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  50. faclab.addMouseListener(new MouseAdapter() {
  51. public void mouseClicked(MouseEvent e) {
  52. if (e.getClickCount() > 0) {
  53. facframe();
  54. }
  55.  
  56. }
  57. });
  58.  
  59.  
  60.  
  61. java.net.URL url1 = Home.class.getResource("/user.png");
  62. ImageIcon stuimage = new ImageIcon(url1);
  63. JLabel stulab = new JLabel(stuimage);
  64.  
  65. stulab.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  66. stulab.addMouseListener(new MouseAdapter() {
  67.  
  68.  
  69. public void mouseClicked(MouseEvent e) {
  70. if (e.getClickCount() > 0) {
  71. studentframe();
  72. }
  73.  
  74. }
  75. });
  76.  
  77.  
  78. JLabel facl=new JLabel("Faculty");
  79. facl.setHorizontalAlignment(SwingConstants.CENTER);
  80. JLabel stdl=new JLabel("Student");
  81. stdl.setHorizontalAlignment(SwingConstants.CENTER);
  82. faclab.setBounds(450,250, 200, 200);
  83. stulab.setBounds(800,250, 200, 200);
  84. facl.setBounds(450, 475, 200, 20);
  85. stdl.setBounds(800,475,200,20);
  86. contentPane.add(faclab);
  87. contentPane.add(stulab);
  88. contentPane.add(facl);
  89. contentPane.add(stdl);
  90. frame.setContentPane(contentPane);
  91. frame.setSize(1500, 1000);
  92. frame.setLocationRelativeTo(null);
  93. frame.setVisible(true);
  94.  
  95. }
  96. protected static void facframe()
  97. {
  98. frame1 = new JFrame("Faculty");
  99. frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  100. frame1.setUndecorated(true);
  101. frame1.setAlwaysOnTop(true);
  102. JPanel contentPane = new JPanel();
  103. contentPane.setOpaque(true);
  104. contentPane.setBackground(SystemColor.activeCaption);
  105. contentPane.setLayout(null);
  106. JLabel facnamefieldl=new JLabel("Faculty Name");
  107. JLabel deptlabel=new JLabel("Department");
  108. deptlabel.setBounds(400, 380, 140, 25);
  109. contentPane.add(deptlabel);
  110. facnamefieldl.setBounds(400, 350, 140, 25);
  111. contentPane.add(facnamefieldl);
  112. JTextField facname = new JTextField("");
  113.  
  114. facname.setBounds(560, 350,200, 25);
  115. String depts[]={"Select Dept","Automobile","Mechanical(Btech)","Civil(Btech)","IT","CSE","ECE","EEE","MBA","Training","Mechanical(Poly tech.)","Civil(Poly tech.)","EEE(Poly tech.)"};
  116. JComboBox deptnames=new JComboBox(depts);
  117. deptnames.setBounds(560,380,150,25);
  118. JButton submit=new JButton("Submit");
  119. JButton back=new JButton("Back");
  120. submit.setLayout(null);
  121. back.setBounds(560,445 , 80, 25);
  122. submit.setBounds(560,415 , 80, 25);
  123.  
  124. contentPane.add(deptnames);
  125. contentPane.add(back);
  126. contentPane.add(facname);
  127. contentPane.add(submit);
  128.  
  129.  
  130. frame1.setSize(1500, 1000);
  131. frame1.setContentPane(contentPane);
  132. frame1.setLocationRelativeTo(null);
  133. frame1.setVisible(true);
  134. frame.setVisible(false);
  135. back.addActionListener(new ActionListener()
  136. {
  137. public void actionPerformed(ActionEvent e)
  138. {
  139.  
  140. frame1.setVisible(false);
  141. frame.setVisible(true);
  142. }
  143. });
  144. submit.addActionListener(new ActionListener()
  145. {
  146. public void actionPerformed(ActionEvent e)
  147. {
  148.  
  149. facins(facname.getText(),deptnames.getSelectedItem().toString());
  150.  
  151.  
  152. }
  153. });
  154.  
  155. }
  156. protected static void facins(String fname,String dept)
  157. {
  158. try
  159. {
  160. InetAddress ip=InetAddress.getLocalHost();
  161. String hostname=ip.getHostName();
  162. String str=ip.toString();
  163. facname=fname;
  164.  
  165. String ipaddress=str.substring(str.indexOf("/")+1);
  166. if (fname.equals("")||dept.equals("Select Dept")){
  167. frame1.setAlwaysOnTop(false);
  168. JOptionPane.showMessageDialog(null, "please fill all the fields");
  169. frame1.setAlwaysOnTop(true);
  170.  
  171. }
  172. else
  173. {
  174.  
  175. Class.forName("com.mysql.jdbc.Driver");
  176. Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/userauth","root","");
  177.  
  178. PreparedStatement stmt=conn.prepareStatement("insert into faculty(facname,dept,ipaddress,hostname,date)values(?,?,?,?,NOW())");
  179. stmt.setString(1, fname);
  180. stmt.setString(2, dept);
  181. stmt.setString(3, ipaddress);
  182. stmt.setString(4,hostname);
  183.  
  184. int i=stmt.executeUpdate();
  185. if(i>0)
  186. {
  187. frame1.setAlwaysOnTop(false);
  188. JOptionPane.showMessageDialog(null, "Sucessfully updated");
  189. frame1.setVisible(false);
  190. frame.setVisible(false);
  191.  
  192.  
  193.  
  194.  
  195. }
  196. }
  197.  
  198. }
  199. catch(Exception my)
  200. {
  201. System.out.println(my);
  202. }
  203.  
  204. }
  205. protected static void studentframe()
  206. {
  207.  
  208. frame1 = new JFrame("Student");
  209. frame1.setUndecorated(true);
  210. frame1.setAlwaysOnTop(true);
  211. frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  212. JPanel contentPane = new JPanel();
  213. contentPane.setOpaque(true);
  214. contentPane.setBackground(SystemColor.activeCaption);
  215. contentPane.setLayout(null);
  216. JTextField stname=new JTextField("");
  217. stname.setBounds(560, 350,200, 25);
  218. JTextField stuid = new JTextField("");
  219. stuid.setBounds(560,380,150,25);
  220. JLabel namel=new JLabel("Student Name");
  221. JLabel idl=new JLabel("Student ID");
  222. namel.setBounds(400, 350, 140, 25);
  223. idl.setBounds(400, 380, 140, 25);
  224. JButton submit=new JButton("Submit");
  225. JButton back=new JButton("Back");
  226. submit.setLayout(null);
  227. back.setBounds(560,445 , 80, 25);
  228. submit.setBounds(560,415 , 80, 25);
  229. contentPane.add(namel);
  230. contentPane.add(idl);
  231.  
  232. contentPane.add(stname);
  233. contentPane.add(stuid);
  234. contentPane.add(submit);
  235. contentPane.add(back);
  236.  
  237. frame1.setSize(1500,700 );
  238. frame1.setContentPane(contentPane);
  239. frame1.setLocationRelativeTo(null);
  240. frame1.setVisible(true);
  241. frame.setVisible(false);
  242. DocumentFilter filter = new UppercaseDocumentFilter();
  243. AbstractDocument firstNameDoc = (AbstractDocument) stuid.getDocument();
  244. firstNameDoc.setDocumentFilter(filter);
  245.  
  246. back.addActionListener(new ActionListener()
  247. {
  248. public void actionPerformed(ActionEvent e)
  249. {
  250. // display/center the jdialog when the button is pressed
  251. frame1.setVisible(false);
  252. frame.setVisible(true);
  253. }
  254. });
  255.  
  256.  
  257. submit.addActionListener(new ActionListener()
  258. {
  259. public void actionPerformed(ActionEvent e)
  260. {
  261. try
  262. {
  263. Home.studentins(stname.getText(),stuid.getText());
  264.  
  265. }
  266. catch (Exception e2)
  267. {
  268.  
  269. }
  270. }
  271.  
  272.  
  273. });
  274.  
  275. }
  276.  
  277. protected static void studentins(String stuname,String id) throws IOException
  278. {
  279. InetAddress ip=InetAddress.getLocalHost();
  280.  
  281. String hostname=ip.getHostName();
  282. String str=ip.toString();
  283. stdid=id;
  284. String ipaddress=str.substring(str.indexOf("/")+1);
  285. if(stuname.equals("")||id.equals(""))
  286. {
  287. frame1.setAlwaysOnTop(false);
  288. JOptionPane.showMessageDialog(null, "please fill all the fields");
  289. frame1.setAlwaysOnTop(true);
  290.  
  291. }
  292. else
  293. {
  294. try{
  295.  
  296.  
  297. Class.forName("com.mysql.jdbc.Driver");
  298. Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/userauth","root","");
  299. PreparedStatement stmt=conn.prepareStatement("insert into students(stname,stid,hostname,ipaddress,date)values(?,?,?,?,NOW())");
  300. stmt.setString(1, stuname);
  301. stmt.setString(2, id);
  302. stmt.setString(3,hostname);
  303. stmt.setString(4,ipaddress);
  304.  
  305. int i=stmt.executeUpdate();
  306. if(i>0)
  307. {
  308. frame1.setAlwaysOnTop(false);
  309. JOptionPane.showMessageDialog(null, "Sucessfully updated");
  310. frame1.setVisible(false);
  311. frame.setVisible(false);
  312.  
  313.  
  314. }
  315. }
  316. catch(Exception a)
  317. {
  318. System.out.println(a);
  319. }
  320. }
  321.  
  322.  
  323. }
  324.  
  325.  
  326.  
  327. public static void main(String args[]) throws IOException, URISyntaxException
  328. {
  329. new Home();
  330. Runtime.getRuntime().addShutdownHook(new Thread()
  331. {
  332. @Override
  333. public void run()
  334. {
  335. try {
  336. Class.forName("com.mysql.jdbc.Driver");
  337. Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/userauth","root","");
  338. if(!facname.equals(""))
  339. {
  340. PreparedStatement stmt=conn.prepareStatement("update faculty set status=? where facname=?");
  341. stmt.setString(1, "loggedout");
  342. stmt.setString(2,stdid);
  343. }
  344. else if(!stdid.equals(""))
  345. {
  346. PreparedStatement stmt=conn.prepareStatement("update students set status=? where stid=?");
  347. stmt.setString(1, "loggedout");
  348. stmt.setString(2,stdid);
  349.  
  350. }
  351.  
  352.  
  353.  
  354. } catch (Exception e) {
  355.  
  356. e.printStackTrace();
  357. }
  358.  
  359.  
  360.  
  361.  
  362. }
  363. });
  364.  
  365.  
  366. }
  367. }
  368.  
  369.  
  370. class UppercaseDocumentFilter extends DocumentFilter {
  371.  
  372. @Override
  373. public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
  374. fb.insertString(offset, text.toUpperCase(), attr);
  375. }
  376.  
  377. @Override
  378. public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
  379. fb.replace(offset, length, text.toUpperCase(), attrs);
  380. }
  381.  
  382. }
  383.  
  384. Runtime.getRuntime().addShutdownHook(new Thread() {
  385. public void run() { database.close(); }
  386. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement