Advertisement
Zuhairy_Harry

Example

Jun 23rd, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import javax.swing.event.*;
  5. import java.sql.*;
  6.  
  7. public class Exercise1 extends JFrame implements ActionListener
  8. {
  9. JPanel panTopic, panMain, panNameAge, panTelAddress, panName, panAge, panTelephone, panAddress;
  10. JPanel panAction, panSubmit, panUpdate, panDelete, panView;
  11. JPanel panData;
  12. JLabel lblTopic, lblName, lblAge, lblTelephone, lblAddress;
  13. JTextField txtName, txtTelephone;
  14. JTextArea txtAddress, txtView;
  15. JComboBox cbAge;
  16. JButton btnView, btnDelete, btnSubmit, btnUpdate, btnSearch;
  17. JMenuBar mb;
  18. JMenu menuFile;
  19. JMenuItem menuDelete;
  20.  
  21. Connection con;
  22. Statement stmt;
  23. ResultSet rs;
  24. String output;
  25.  
  26. public void connectDB() throws Exception
  27. {
  28. Class.forName("com.mysql.cj.jdbc.Driver");
  29. String path = "jdbc:mysql://localhost:3306/register?useTimezone=true&serverTimezone=UTC";
  30. con = DriverManager.getConnection(path, "root", "abcDEF123");
  31. System.out.println("connected");
  32.  
  33. stmt = con.createStatement();
  34.  
  35. String sql = "CREATE TABLE REGISTER" + "(name VARCHAR(50), age VARCHAR(50), telephone VARCHAR(50), address VARCHAR(50))";
  36. stmt.executeUpdate(sql);
  37. System.out.print("Table created");
  38.  
  39. }
  40.  
  41. public Exercise1()
  42. {
  43. super("My Exercise 1");
  44.  
  45. try
  46. {
  47. connectDB();
  48. }
  49. catch(Exception e){
  50.  
  51. }
  52.  
  53.  
  54. menuGUI();
  55. setJMenuBar(mb);
  56.  
  57.  
  58.  
  59. lblTopic = new JLabel("REGISTRATION");
  60. lblTopic.setFont(new Font("Arial",Font.BOLD,24));
  61.  
  62. panTopic = new JPanel();
  63. panTopic.setLayout(new FlowLayout(FlowLayout.CENTER));
  64. panTopic.add(lblTopic);
  65.  
  66. lblName = new JLabel("Name :");
  67. lblName.setFont(new Font("Arial",Font.BOLD,12));
  68. add(lblName);
  69.  
  70. txtName = new JTextField(20);
  71. txtName.setFont(new Font("Arial",Font.BOLD,12));
  72. add(txtName);
  73.  
  74.  
  75. panName = new JPanel();
  76. panName.setLayout(new FlowLayout(FlowLayout.LEFT));
  77. panName.add(lblName);
  78. panName.add(txtName);
  79. add(panName);
  80.  
  81. lblAge = new JLabel("Age :");
  82. lblAge.setFont(new Font("Arial",Font.BOLD,12));
  83. add(lblAge);
  84.  
  85. String Age[] = {"","18-30", "31-40", "41-50", "51-60"};
  86.  
  87. cbAge = new JComboBox(Age);
  88. add(cbAge);
  89.  
  90. panAge = new JPanel();
  91. panAge.setLayout(new FlowLayout(FlowLayout.LEFT));
  92. panAge.add(lblAge);
  93. panAge.add(cbAge);
  94. add(panAge);
  95.  
  96.  
  97. panNameAge = new JPanel();
  98. panNameAge.add(panName);
  99. panNameAge.add(panAge);
  100. panNameAge.setLayout(new FlowLayout(FlowLayout.LEFT));
  101. add(panNameAge);
  102.  
  103. panTelephone = new JPanel();
  104. panTelephone.setLayout(new FlowLayout(FlowLayout.LEFT));
  105.  
  106. panAddress = new JPanel();
  107. panAddress.setLayout(new FlowLayout(FlowLayout.LEFT));
  108.  
  109. panTelAddress = new JPanel();
  110. panTelAddress.setLayout(new FlowLayout(FlowLayout.LEFT));
  111.  
  112. lblTelephone = new JLabel("Telephone:");
  113. lblTelephone.setFont(new Font("Arial", Font.BOLD, 12));
  114. panTelephone.add(lblTelephone);
  115.  
  116. txtTelephone = new JTextField(20);
  117. txtTelephone.setFont(new Font("Arial",Font.BOLD,12));
  118. panTelephone.add(txtTelephone);
  119.  
  120. lblAddress = new JLabel("Address:");
  121. lblAddress.setFont(new Font("Arial",Font.BOLD,12));
  122. panAddress.add(lblAddress);
  123.  
  124. txtAddress = new JTextArea(7,31);
  125. txtAddress.setFont(new Font("Arial",Font.BOLD,12));
  126. panAddress.add(txtAddress);
  127.  
  128.  
  129. panTelAddress.add(panTelephone);
  130. panTelAddress.add(panAddress);
  131.  
  132. //panel button Submit
  133.  
  134. btnSubmit = new JButton("SUBMIT");
  135. btnSubmit.setFont(new Font("Arial",Font.BOLD,12));
  136. btnSubmit.addActionListener(this);
  137. //btnSubmit.setColor(new Color("GREEN"))
  138.  
  139. panSubmit = new JPanel();
  140. panSubmit.setLayout(new FlowLayout(FlowLayout.RIGHT));
  141. panSubmit.add(btnSubmit);
  142.  
  143. btnView = new JButton("VIEW");
  144. btnView.setFont(new Font("Arial",Font.BOLD,12));
  145. btnView.addActionListener(this);
  146.  
  147. panView = new JPanel();
  148. panView.setLayout(new FlowLayout(FlowLayout.CENTER));
  149. panView.add(btnView);
  150.  
  151. btnUpdate = new JButton("UPDATE");
  152. btnUpdate.setFont(new Font("Arial",Font.BOLD,12));
  153. btnUpdate.addActionListener(this);
  154.  
  155. panUpdate = new JPanel();
  156. panUpdate.setLayout(new FlowLayout(FlowLayout.LEFT));
  157. panUpdate.add(btnUpdate);
  158.  
  159. panAction = new JPanel();
  160. panAction.setLayout(new BoxLayout(panAction,BoxLayout.X_AXIS));
  161. panAction.add(panSubmit);
  162. panAction.add(panView);
  163. panAction.add(panUpdate);
  164.  
  165. panData = new JPanel();
  166. panData.setLayout(new BoxLayout(panData,BoxLayout.X_AXIS));
  167.  
  168. txtView = new JTextArea(10,50);
  169. panData.add(txtView);
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. JLabel lblSpace = new JLabel("\t");
  177. lblSpace.setFont(new Font("Arial",Font.BOLD,10));
  178.  
  179. JPanel panSpace = new JPanel();
  180. panSpace.setLayout(new BoxLayout(panSpace, BoxLayout.Y_AXIS));
  181. panSpace.add(lblSpace);
  182.  
  183. JLabel lblSpace2 = new JLabel("\t");
  184. lblSpace2.setFont(new Font("Arial",Font.BOLD,10));
  185.  
  186. JPanel panSpace2 = new JPanel();
  187. panSpace2.setLayout(new BoxLayout(panSpace2, BoxLayout.Y_AXIS));
  188. panSpace2.add(lblSpace2);
  189.  
  190. JLabel lblSpace3 = new JLabel("\t");
  191. lblSpace3.setFont(new Font("Arial",Font.BOLD,10));
  192.  
  193. JPanel panSpace3 = new JPanel();
  194. panSpace3.setLayout(new BoxLayout(panSpace3, BoxLayout.Y_AXIS));
  195. panSpace3.add(lblSpace3);
  196.  
  197. JLabel lblSpace4 = new JLabel("\t");
  198. lblSpace4.setFont(new Font("Arial",Font.BOLD,10));
  199.  
  200. JPanel panSpace4 = new JPanel();
  201. panSpace4.setLayout(new BoxLayout(panSpace4, BoxLayout.Y_AXIS));
  202. panSpace4.add(lblSpace4);
  203.  
  204.  
  205. panMain = new JPanel();
  206. panMain.add(panTopic);
  207. panMain.add(panSpace);
  208. panMain.add(panNameAge);
  209. panMain.add(panSpace2);
  210. panMain.add(panTelAddress);
  211. panMain.add(panSpace3);
  212. panMain.add(panAction);
  213. panMain.add(panSpace4);
  214. panMain.add(panData);
  215. panMain.setLayout(new BoxLayout(panMain,BoxLayout.Y_AXIS));
  216.  
  217.  
  218. JPanel panAll = new JPanel();
  219. panAll.add(panMain);
  220. panAll.setLayout(new FlowLayout(FlowLayout.LEFT));
  221. add(panAll);
  222.  
  223. setLayout(new FlowLayout());
  224. setSize(800,800);
  225. setVisible(true);
  226. setLocationRelativeTo(null);
  227.  
  228. }
  229.  
  230. public void menuGUI()
  231. {
  232. mb = new JMenuBar();
  233. menuFile = new JMenu("File");
  234. menuDelete = new JMenuItem("Delete");
  235. menuDelete.addActionListener(this);
  236.  
  237. menuFile.add(menuDelete);
  238. mb.add(menuFile);
  239. }
  240.  
  241. public static void main(String[]args)
  242. {
  243. new Exercise1().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  244. }
  245.  
  246. public void actionPerformed(ActionEvent ae)
  247. {
  248. if (ae.getSource()== btnSubmit)
  249. {
  250. String nameSub = txtName.getText();
  251. Object ageSub = cbAge.getSelectedItem();
  252. String telSub = txtTelephone.getText();
  253. String addressSub = txtAddress.getText();
  254.  
  255. if (nameSub=="" || ageSub=="" || telSub=="" || addressSub=="")
  256. {
  257. JOptionPane.showMessageDialog(null,"Please complete the details!");
  258. }
  259.  
  260. else
  261. {
  262. try{
  263. insertData();
  264. JOptionPane.showMessageDialog(null,"Data has been saved");
  265.  
  266. }
  267. catch(Exception e){
  268. JOptionPane.showMessageDialog(null,"Data not saved");
  269. }
  270. }
  271.  
  272.  
  273. }
  274.  
  275. else if (ae.getSource() == btnView)
  276. {
  277. try{
  278. viewData();
  279. txtView.setText(output);
  280. }
  281. catch(Exception e) {
  282. JOptionPane.showMessageDialog(null,"Error!");
  283. }
  284. }
  285.  
  286. else if (ae.getSource() == btnUpdate)
  287. {
  288. try
  289. {
  290. updateData();
  291. JOptionPane.showMessageDialog(null, "Data has been updated");
  292. }catch(Exception e){}
  293.  
  294. }
  295.  
  296. else if (ae.getSource() == menuDelete)
  297. {
  298. try{
  299. deleteData();
  300. JOptionPane.showMessageDialog(null, "Data has been deleted");
  301. }
  302. catch(Exception e){
  303.  
  304. }
  305. }
  306. }
  307.  
  308. public void insertData() throws Exception
  309. {
  310. String name = txtName.getText();
  311. Object age = cbAge.getSelectedItem();
  312. String telephone = txtTelephone.getText();
  313. String address = txtAddress.getText();
  314. stmt = con.createStatement();
  315.  
  316.  
  317. String sql = "INSERT INTO REGISTER VALUES" + "('"+name+"', '"+age+"', '"+telephone+"', '"+address+"')";
  318.  
  319. stmt.executeUpdate(sql);
  320.  
  321. }
  322.  
  323. public void viewData() throws Exception
  324. {
  325. String name = txtName.getText();
  326. Object age = cbAge.getSelectedItem();
  327. String telephone = txtTelephone.getText();
  328. String address = txtAddress.getText();
  329. stmt = con.createStatement();
  330. String sql = "SELECT * FROM REGISTER";
  331. rs = stmt.executeQuery(sql);
  332. output = "Name\t Age\t Telephone\t Address\n";
  333. while(rs.next()){
  334. output = output + rs.getString("name") + "\t" + rs.getString("age") + "\t" + rs.getString("telephone") + "\t" + rs.getString("address") + "\n";
  335. }
  336. }
  337.  
  338. public void updateData() throws Exception
  339. {
  340. stmt = con.createStatement();
  341.  
  342. String name = JOptionPane.showInputDialog(null,"Insert name to update");
  343. String nameU = JOptionPane.showInputDialog(null,"Insert new name");
  344.  
  345. String sql = "UPDATE REGISTER set name = '" + nameU + "' WHERE name = '" + name + "'";
  346. stmt.executeUpdate(sql);
  347. }
  348.  
  349. public void deleteData() throws Exception
  350. {
  351. String name = JOptionPane.showInputDialog(null, "Enter name to delete");
  352. int a = JOptionPane.showConfirmDialog(null, "Are you confirm to delete?");
  353.  
  354. if(a==JOptionPane.YES_OPTION)
  355. {
  356. stmt = con.createStatement();
  357. String sql = "DELETE FROM REGISTER WHERE name = '"+name+"'";
  358. stmt.executeUpdate(sql);
  359. }
  360. else
  361. {
  362. JOptionPane.showMessageDialog(null, "Cancel operation");
  363. }
  364.  
  365. }
  366.  
  367. }
  368.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement