Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. import java.awt.event.*;
  2. import javax.swing.*;
  3. import javax.swing.table.DefaultTableModel;
  4. import java.awt.*;
  5. import java.sql.*;
  6. import java.util.*;
  7. // final project
  8. class EmpolyeeAll extends JFrame implements ActionListener{
  9.  
  10. //driver name and database name
  11. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  12. static final String DB_URL = "jdbc:mysql://localhost/emp";
  13.  
  14. //database user and pass
  15. static final String USER = "root";
  16. static final String PASS = "1234";
  17. Connection conn = null;
  18. Statement stmt = null;
  19.  
  20.  
  21. JLabel empTitleLb;
  22. JLabel idLb, nameLb, salaryLb, postLb,showLb,deleteLb;;
  23. JTextField idField, nameField, salaryField,deleteField;
  24. JComboBox postJcb;
  25. JButton regJb,showJb,deleteJb,updateJb, CencleJb;
  26.  
  27. JTable table = new JTable();
  28.  
  29. //font
  30. Font font = new Font("Serif", Font.BOLD, 30);
  31. Font fontlabel = new Font("Serif", Font.BOLD, 20);
  32. //frame color
  33. Color foreColor =new Color(255, 255, 255);
  34. Color backColor = new Color(120, 20, 102);
  35. //label color
  36. Color foreColorlabel =new Color(255, 255, 255);
  37. Color backColorlabel = new Color(120, 150, 102);
  38.  
  39. //field font
  40. Font fieldFont = new Font("Serif", Font.BOLD, 25);
  41. Color backColorField = new Color(198, 185, 185);
  42. Color foreColorField =new Color(0, 0, 0);
  43. //button
  44. Color backColorButton = new Color(0, 153, 51);
  45.  
  46.  
  47. EmpolyeeAll(){
  48.  
  49. //title
  50. empTitleLb = new JLabel("Employee Registration Form",JLabel.CENTER);
  51. empTitleLb.setBounds(170,20,400,40);
  52. empTitleLb.setFont(font);
  53. empTitleLb.setOpaque(true);
  54. empTitleLb.setBackground(backColor);
  55. empTitleLb.setForeground(foreColor);
  56. add(empTitleLb);
  57.  
  58. //employee id
  59. idLb = new JLabel("Emp Id : ",JLabel.CENTER);
  60. idLb.setBounds(150,100,150,30);
  61. idLb.setFont(fontlabel);
  62. idLb.setOpaque(true);
  63. idLb.setBackground(backColorlabel);
  64. idLb.setForeground(foreColorlabel);
  65. add(idLb);
  66.  
  67. idField = new JTextField();
  68. idField.setBounds(350,100,200,30);
  69. idField.setToolTipText("<html><b><font color=gray size=6>"
  70. + "Enter Emp.ID here" + "</font></b></html>");
  71. idField.setFont(fieldFont);
  72. idField.setBackground(backColorField);
  73. idField.setForeground(foreColorField);
  74. add(idField);
  75.  
  76. //employee name
  77. nameLb = new JLabel("Name: ",JLabel.CENTER);
  78. nameLb.setBounds(150,150,150,30);
  79. nameLb.setFont(fontlabel);
  80. nameLb.setOpaque(true);
  81. nameLb.setBackground(backColorlabel);
  82. nameLb.setForeground(foreColorlabel);
  83. add(nameLb);
  84.  
  85. nameField = new JTextField();
  86. nameField.setBounds(350,150,200,30);
  87. nameField.setToolTipText("<html><b><font color=gray size=6>"
  88. + "Enter Name here" + "</font></b></html>");
  89. nameField.setFont(fieldFont);
  90. nameField.setBackground(backColorField);
  91. nameField.setForeground(foreColorField);
  92. add(nameField);
  93.  
  94. //employee slary
  95. salaryLb = new JLabel("Salary : ",JLabel.CENTER);
  96. salaryLb.setBounds(150,200,150,30);
  97. salaryLb.setFont(fontlabel);
  98. salaryLb.setOpaque(true);
  99. salaryLb.setBackground(backColorlabel);
  100. salaryLb.setForeground(foreColorlabel);
  101. add(salaryLb);
  102.  
  103. salaryField= new JTextField();
  104. salaryField.setBounds(350,200,200,30);
  105. salaryField.setToolTipText("<html><b><font color=gray size=6>"
  106. + "Enter Salary here" + "</font></b></html>");
  107. salaryField.setFont(fieldFont);
  108. salaryField.setBackground(backColorField);
  109. salaryField.setForeground(foreColorField);
  110. add(salaryField);
  111. //post combobox
  112. postLb = new JLabel("Post : ",JLabel.CENTER);
  113. postLb.setBounds(150,250,150,30);
  114. postLb.setFont(fontlabel);
  115. postLb.setOpaque(true);
  116. postLb.setBackground(backColorlabel);
  117. postLb.setForeground(foreColorlabel);
  118. add(postLb);
  119.  
  120. String post[] = {"select","Programmer","developer","Designer"};
  121. postJcb = new JComboBox(post);
  122. postJcb.setFont(fieldFont);
  123. postJcb.setBackground(backColorField);
  124. postJcb.setForeground(foreColorField);
  125. postJcb.setBounds(350,250,200,30);
  126. add(postJcb);
  127. //reg and cencle button
  128. regJb = new JButton("Registration");
  129. regJb.setBounds(70,360,160,40);
  130. regJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
  131. regJb.setBackground(backColorButton);
  132. regJb.setForeground(foreColorlabel);
  133. regJb.addActionListener(this);
  134. add(regJb);
  135.  
  136. //update
  137. updateJb = new JButton("Update");
  138. updateJb.setBounds(370,360,120,40);
  139. updateJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
  140. updateJb.setBackground(backColorButton);
  141. updateJb.setForeground(foreColorlabel);
  142. updateJb.addActionListener(this);
  143. add(updateJb);
  144.  
  145. //delete
  146. deleteLb = new JLabel("Enter Id ",JLabel.CENTER);
  147. deleteLb.setBounds(70,420,150,30);
  148. deleteLb.setFont(fontlabel);
  149. deleteLb.setOpaque(true);
  150. deleteLb.setBackground(backColorlabel);
  151. deleteLb.setForeground(foreColorlabel);
  152. add(deleteLb);
  153.  
  154. deleteField = new JTextField();
  155. deleteField.setBounds(250,420,70,30);
  156. deleteField.setToolTipText("<html><b><font color=gray size=6>"
  157. + "Enter delete ID here" + "</font></b></html>");
  158. deleteField.setFont(fieldFont);
  159. deleteField.setBackground(backColorField);
  160. deleteField.setForeground(foreColorField);
  161. add(deleteField);
  162.  
  163. deleteJb = new JButton("Delete");
  164. deleteJb.setBounds(350,420,110,30);
  165. deleteJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
  166. deleteJb.setBackground(new Color(255, 64, 0));
  167. deleteJb.setForeground(foreColorlabel);
  168. deleteJb.addActionListener(this);
  169. add(deleteJb);
  170.  
  171. //show
  172. showJb= new JButton("Show");
  173. showJb.setBounds(250,360,100,40);
  174. showJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
  175. showJb.setBackground(backColorButton);
  176. showJb.setForeground(foreColorlabel);
  177. showJb.addActionListener(this);
  178. add(showJb);
  179. //cancle
  180. CencleJb= new JButton("Cancle");
  181. CencleJb.setBounds(505,360,120,40);
  182. CencleJb.setFont(new java.awt.Font("Arial", Font.BOLD, 20));
  183. CencleJb.setBackground(Color.RED);
  184. CencleJb.setForeground(foreColorlabel);
  185. CencleJb.addActionListener(this);
  186. add(CencleJb);
  187.  
  188. //table panel
  189. JScrollPane pane = new JScrollPane(table);
  190. pane.setBounds(650,90,400,400);
  191. add(pane);
  192.  
  193.  
  194. // show label
  195. showLb = new JLabel("Show All Employee Info ",JLabel.CENTER);
  196. showLb.setBounds(650,30,400,40);
  197. showLb.setFont(fontlabel);
  198. showLb.setOpaque(true);
  199. showLb.setBackground(new Color(102, 51, 0));
  200. showLb.setForeground(Color.GREEN);
  201. add(showLb);
  202.  
  203.  
  204. //frame
  205. setTitle("DEMO Project : Employee ");
  206. setSize(1100,600);
  207. getContentPane().setBackground(new Color(255, 165, 0));
  208. setLocationRelativeTo(null);
  209. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  210. setLayout(null);
  211. setVisible(true);
  212. }
  213.  
  214.  
  215. // ************************action performed ****************************
  216. public void actionPerformed(ActionEvent ae){
  217.  
  218. int id = 0,salary=0;
  219. String name="",post="";
  220. if(ae.getSource()==regJb){
  221.  
  222. //geting value from text field
  223.  
  224. if(postJcb.getSelectedItem().equals("select")|| idField.getText().isEmpty() || nameField.getText().isEmpty()||salaryField.getText().isEmpty() ){
  225.  
  226. JOptionPane.showMessageDialog(null,"Please check all field");
  227.  
  228. }else if(salaryField.getText().equals("0")){
  229.  
  230. JOptionPane.showMessageDialog(null," please check salary ");
  231. }
  232.  
  233. else{
  234. //idField, nameField, salaryField;
  235. try{
  236. id =Integer.parseInt(idField.getText());
  237. name = nameField.getText();
  238. salary = Integer.parseInt(salaryField.getText());
  239. post = (String) postJcb.getSelectedItem();
  240. }
  241. catch(Exception e){
  242. JOptionPane.showMessageDialog(null," data submit error "+e+"");
  243. }
  244.  
  245. //database handle here
  246. try{
  247. Class.forName(JDBC_DRIVER);
  248. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  249. stmt = conn.createStatement();
  250.  
  251. //check salary and id 0;
  252. if(0 >=id || salary <=0){
  253. JOptionPane.showMessageDialog(null," please check id and Salary Field ");
  254.  
  255. //
  256. }else{
  257.  
  258.  
  259. //String sql = "select * from empinfo where id = '"+id+"'";
  260. String sql = "insert into empinfo values('"+id+"','"+name+"','"+salary+"','"+post+"')";
  261. //ResultSet rs = stmt.executeQuery(sql);
  262. stmt.executeUpdate(sql);
  263. JOptionPane.showMessageDialog(null," Successfully data submit");
  264. }
  265.  
  266. }catch(Exception e){
  267. JOptionPane.showMessageDialog(null," Duplicate Employee ID "+id+"\nPlease Enter Unique ID ");
  268. System.out.println(e);
  269.  
  270. }
  271. if (conn != null||stmt != null ) {
  272. try {
  273. conn.close();
  274. stmt.close();
  275. } catch (SQLException e) {
  276.  
  277. }
  278. }
  279.  
  280. }
  281.  
  282. }
  283.  
  284. //Show button
  285. if(ae.getSource()==showJb){
  286. //String show = "SELECT * FROM empinfo";
  287. try{
  288. Class.forName(JDBC_DRIVER);
  289. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  290. stmt = conn.createStatement();
  291. ResultSet rs = stmt.executeQuery("SELECT * FROM empinfo");
  292.  
  293. Object []columns = {"Emp Id","Name ","Salary "," Post"};
  294. DefaultTableModel model = new DefaultTableModel();
  295. model.setColumnIdentifiers(columns);
  296. table.setModel(model);
  297. table.setRowHeight(30);
  298. table.setBackground(new Color(204, 204, 204));
  299. table.setForeground(new Color(0, 0, 0));
  300. Object[] row = new Object[4];
  301. while(rs.next()){
  302.  
  303. row[0] = rs.getInt("id");
  304. row[1] = rs.getString("name");
  305. row[2] = rs.getInt("salary");
  306. row[3] = rs.getString("post");
  307.  
  308. //showLb.setText(Integer.toString(rs.getInt("id")));
  309. model.addRow(row);
  310.  
  311. }
  312. }catch(Exception e){
  313. System.out.println("show error"+e);
  314.  
  315. }
  316.  
  317.  
  318.  
  319. }
  320.  
  321. //delete button
  322. if(ae.getSource()==deleteJb){
  323.  
  324. try{
  325. stmt = conn.createStatement();
  326. ResultSet rs = stmt.executeQuery("SELECT * FROM empinfo");
  327. int dele = Integer.parseInt(deleteField.getText());
  328. while(rs.next()){
  329. //checking id
  330. if(dele==rs.getInt("id")){
  331. String del = "DELETE FROM empinfo WHERE id = '"+dele+"'";
  332. stmt.executeUpdate(del);
  333. JOptionPane.showMessageDialog(null," Deleted Successfully");
  334. }
  335. }
  336.  
  337. JOptionPane.showMessageDialog(null," Plese Enter Correct ID ");
  338.  
  339. // deleteField.setText("delete");
  340. }catch(Exception ee){
  341. JOptionPane.showMessageDialog(null," Please Enter Integer Number:\n"+ee);
  342.  
  343. }
  344.  
  345. }
  346.  
  347.  
  348. //update button
  349. if(ae.getSource()==updateJb){
  350. JOptionPane.showMessageDialog(null," Update button will work soon :( ");
  351.  
  352. }
  353.  
  354. //cancle button
  355. if(ae.getSource()==CencleJb){
  356. //idField, nameField, salaryField;
  357. idField.setText(null);
  358. nameField.setText(null);
  359. salaryField.setText(null);
  360. deleteField.setText(null);
  361. postJcb.setSelectedItem("select");
  362. }
  363. }
  364.  
  365. public static void main(String argd[]){
  366.  
  367. EmpolyeeAll em = new EmpolyeeAll();
  368. }
  369.  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement