Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gui;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.Font;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class TaoTK extends JFrame {
- private JPanel contentPane;
- private JTextField tfmatk;
- private JTextField tftentk;
- private JTextField tfsodu;
- private JTextField tfdiachi;
- Connection conn;
- Statement stmt;
- //tao ham connect DB
- public void connectDB() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/bankdb", "root", "");
- System.out.println("Connect Sucess");
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- TaoTK frame = new TaoTK();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public TaoTK() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 545, 372);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JLabel lblToMiTi = new JLabel("T\u1EA1o m\u1EDBi t\u00E0i kho\u1EA3n");
- lblToMiTi.setBounds(212, 38, 102, 24);
- contentPane.add(lblToMiTi);
- JLabel lblMTK = new JLabel("M\u00E3 t\u00E0i kho\u1EA3n");
- lblMTK.setFont(new Font("Times New Roman", Font.PLAIN, 14));
- lblMTK.setBounds(44, 86, 75, 14);
- contentPane.add(lblMTK);
- tfmatk = new JTextField();
- tfmatk.setBounds(165, 83, 189, 20);
- contentPane.add(tfmatk);
- tfmatk.setColumns(10);
- tftentk = new JTextField();
- tftentk.setBounds(165, 130, 189, 20);
- contentPane.add(tftentk);
- tftentk.setColumns(10);
- JLabel lblSD = new JLabel("S\u1ED1 d\u01B0");
- lblSD.setFont(new Font("Times New Roman", Font.PLAIN, 14));
- lblSD.setBounds(44, 182, 46, 14);
- contentPane.add(lblSD);
- tfsodu = new JTextField();
- tfsodu.setBounds(165, 179, 189, 20);
- contentPane.add(tfsodu);
- tfsodu.setColumns(10);
- tfdiachi = new JTextField();
- tfdiachi.setBounds(165, 235, 189, 20);
- contentPane.add(tfdiachi);
- tfdiachi.setColumns(10);
- JLabel lbldiachi = new JLabel("\u0110\u1ECBa ch\u1EC9");
- lbldiachi.setFont(new Font("Times New Roman", Font.PLAIN, 14));
- lbldiachi.setBounds(44, 238, 46, 14);
- contentPane.add(lbldiachi);
- JButton btnToMi = new JButton("T\u1EA1o m\u1EDBi");
- btnToMi.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- connectDB();
- stmt = conn.createStatement();
- int n = stmt.executeUpdate("Insert into Account values('"+tfmatk.getText()+"','"+tftentk.getText()+"','"+Float.parseFloat(tfsodu.getText())+"','"+tfdiachi.getText()+"')");
- if(n>0) JOptionPane.showMessageDialog(null, "Success");
- else JOptionPane.showMessageDialog(null, "Fail");
- } catch (Exception e2) {
- // TODO: handle exception
- e2.printStackTrace();
- }
- }
- });
- btnToMi.setBounds(212, 287, 89, 23);
- contentPane.add(btnToMi);
- JLabel lblTenTK = new JLabel("T\u00EAn t\u00E0i kho\u1EA3n");
- lblTenTK.setFont(new Font("Times New Roman", Font.PLAIN, 14));
- lblTenTK.setBounds(44, 133, 89, 14);
- contentPane.add(lblTenTK);
- }
- }
Add Comment
Please, Sign In to add comment