Guest User

Untitled

a guest
Aug 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. import java.awt.Font;
  2. import java.awt.HeadlessException;
  3. import java.awt.TextField;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import javax.swing.Timer;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import static javax.swing.JFrame.EXIT_ON_CLOSE;
  12. import javax.swing.JLabel;
  13. import javax.swing.JOptionPane;
  14.  
  15. public class UserInput extends Thread
  16. {
  17. Timer t;
  18. JLabel idL,fnameL,lnameL,deptL,pinL;
  19. TextField idT,fnameT,lnameT,deptT,pinT;
  20. JButton submit,cancel;
  21. static int ID,Pin;
  22. static String Fname,Lname,Dept;
  23. public static void store() throws SQLException
  24. {
  25. String url = "jdbc:mysql://localhost/javadb";
  26. String user = "root";
  27. String pass = "Akshay";
  28. String insrt = "insert into student value("+ID+",\""+Fname+"\",\""+Lname+"\",\""+Dept+"\","+Pin+");";
  29. Connection conn = DriverManager.getConnection(url,user,pass);
  30. Statement st = conn.createStatement();
  31. int v = st.executeUpdate(insrt);
  32. }
  33. public UserInput() throws HeadlessException,SQLException
  34. {
  35. JFrame j = new JFrame("Input Field");
  36. j.setLayout(null);
  37. j.setBounds(400, 100, 500, 600);
  38. Font f = new Font("Times New Roman", Font.LAYOUT_LEFT_TO_RIGHT, 40);
  39. JLabel wel = new JLabel("Welcome...!");
  40. wel.setFont(f);
  41. wel.setBounds(160, 30, 200, 30);
  42. j.add(wel);
  43. idL = new JLabel("Employee ID : ");
  44. idL.setBounds(100, 100, 100, 30);
  45. j.add(idL);
  46. idT = new TextField();
  47. idT.setBounds(200, 100, 70, 25);
  48. j.add(idT);
  49.  
  50. fnameL = new JLabel("First Name : ");
  51. fnameL.setBounds(100,150,100,30);
  52. j.add(fnameL);
  53. fnameT = new TextField();
  54. fnameT.setBounds(200, 152, 100, 25);
  55. j.add(fnameT);
  56.  
  57. lnameL = new JLabel("Last Name : ");
  58. lnameL.setBounds(100, 200, 100, 30);
  59. j.add(lnameL);
  60. lnameT = new TextField();
  61. lnameT.setBounds(200, 200, 100, 30);
  62. j.add(lnameT);
  63.  
  64. deptL = new JLabel("Department : ");
  65. deptL.setBounds(100, 250, 100, 30);
  66. j.add(deptL);
  67. deptT = new TextField();
  68. deptT.setBounds(200, 250, 150, 30);
  69. j.add(deptT);
  70.  
  71. pinL = new JLabel("Pincode : ");
  72. pinL.setBounds(100, 300, 100, 30);
  73. j.add(pinL);
  74. pinT = new TextField();
  75. pinT.setBounds(200, 300, 100, 30);
  76. j.add(pinT);
  77.  
  78. submit = new JButton("Submit");
  79. submit.setBounds(200, 350, 100, 30);
  80. j.add(submit);
  81.  
  82. submit.addActionListener((e) ->
  83. {
  84. submit.setEnabled(false);
  85.  
  86. ID = Integer.parseInt(idT.getText());
  87. Fname = fnameT.getText();
  88. Lname = lnameT.getText();
  89. Dept = deptT.getText();
  90. Pin = Integer.parseInt(pinT.getText());
  91. try{store();}catch(SQLException sql){sql.printStackTrace();}
  92. JOptionPane.showMessageDialog(j, "Data Inserted Successfully");
  93. j.dispose();
  94. });
  95. j.setDefaultCloseOperation(EXIT_ON_CLOSE);
  96. j.setVisible(true);
  97. }
  98. public static void main(String[] args) throws SQLException
  99. {
  100. UserInput u = new UserInput();
  101. }
  102. }
Add Comment
Please, Sign In to add comment