Guest User

Untitled

a guest
Dec 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. package gui;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JTextField;
  12. import javax.swing.JButton;
  13. import java.awt.Font;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.Statement;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19.  
  20. public class TaoTK extends JFrame {
  21.  
  22. private JPanel contentPane;
  23. private JTextField tfmatk;
  24. private JTextField tftentk;
  25. private JTextField tfsodu;
  26. private JTextField tfdiachi;
  27. Connection conn;
  28. Statement stmt;
  29. //tao ham connect DB
  30. public void connectDB() {
  31. try {
  32. Class.forName("com.mysql.jdbc.Driver");
  33. conn = DriverManager.getConnection("jdbc:mysql://localhost/bankdb", "root", "");
  34. System.out.println("Connect Sucess");
  35. } catch (Exception e) {
  36. // TODO: handle exception
  37. e.printStackTrace();
  38. }
  39. }
  40. /**
  41. * Launch the application.
  42. */
  43. public static void main(String[] args) {
  44. EventQueue.invokeLater(new Runnable() {
  45. public void run() {
  46. try {
  47. TaoTK frame = new TaoTK();
  48. frame.setVisible(true);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. });
  54. }
  55.  
  56. /**
  57. * Create the frame.
  58. */
  59. public TaoTK() {
  60. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  61. setBounds(100, 100, 545, 372);
  62. contentPane = new JPanel();
  63. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  64. setContentPane(contentPane);
  65. contentPane.setLayout(null);
  66.  
  67. JLabel lblToMiTi = new JLabel("T\u1EA1o m\u1EDBi t\u00E0i kho\u1EA3n");
  68. lblToMiTi.setBounds(212, 38, 102, 24);
  69. contentPane.add(lblToMiTi);
  70.  
  71. JLabel lblMTK = new JLabel("M\u00E3 t\u00E0i kho\u1EA3n");
  72. lblMTK.setFont(new Font("Times New Roman", Font.PLAIN, 14));
  73. lblMTK.setBounds(44, 86, 75, 14);
  74. contentPane.add(lblMTK);
  75.  
  76. tfmatk = new JTextField();
  77. tfmatk.setBounds(165, 83, 189, 20);
  78. contentPane.add(tfmatk);
  79. tfmatk.setColumns(10);
  80.  
  81. tftentk = new JTextField();
  82. tftentk.setBounds(165, 130, 189, 20);
  83. contentPane.add(tftentk);
  84. tftentk.setColumns(10);
  85.  
  86. JLabel lblSD = new JLabel("S\u1ED1 d\u01B0");
  87. lblSD.setFont(new Font("Times New Roman", Font.PLAIN, 14));
  88. lblSD.setBounds(44, 182, 46, 14);
  89. contentPane.add(lblSD);
  90.  
  91. tfsodu = new JTextField();
  92. tfsodu.setBounds(165, 179, 189, 20);
  93. contentPane.add(tfsodu);
  94. tfsodu.setColumns(10);
  95.  
  96. tfdiachi = new JTextField();
  97. tfdiachi.setBounds(165, 235, 189, 20);
  98. contentPane.add(tfdiachi);
  99. tfdiachi.setColumns(10);
  100.  
  101. JLabel lbldiachi = new JLabel("\u0110\u1ECBa ch\u1EC9");
  102. lbldiachi.setFont(new Font("Times New Roman", Font.PLAIN, 14));
  103. lbldiachi.setBounds(44, 238, 46, 14);
  104. contentPane.add(lbldiachi);
  105.  
  106. JButton btnToMi = new JButton("T\u1EA1o m\u1EDBi");
  107. btnToMi.addActionListener(new ActionListener() {
  108. public void actionPerformed(ActionEvent e) {
  109.  
  110. try {
  111. connectDB();
  112. stmt = conn.createStatement();
  113. int n = stmt.executeUpdate("Insert into Account values('"+tfmatk.getText()+"','"+tftentk.getText()+"','"+Float.parseFloat(tfsodu.getText())+"','"+tfdiachi.getText()+"')");
  114. if(n>0) JOptionPane.showMessageDialog(null, "Success");
  115. else JOptionPane.showMessageDialog(null, "Fail");
  116. } catch (Exception e2) {
  117. // TODO: handle exception
  118. e2.printStackTrace();
  119. }
  120.  
  121.  
  122. }
  123. });
  124. btnToMi.setBounds(212, 287, 89, 23);
  125. contentPane.add(btnToMi);
  126.  
  127. JLabel lblTenTK = new JLabel("T\u00EAn t\u00E0i kho\u1EA3n");
  128. lblTenTK.setFont(new Font("Times New Roman", Font.PLAIN, 14));
  129. lblTenTK.setBounds(44, 133, 89, 14);
  130. contentPane.add(lblTenTK);
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment