รบกวนช่วยดูว่าแก้ไขตรงไหนถึงจะไม่ซ้อนครับ
package com.java.myapp;
import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel;
public class MyDetail extends JDialog {
/** * Create the dialog. */ public MyDetail(final String sid) { setTitle("Customer_nresu"); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setBounds(100, 100, 760, 550); getContentPane().setLayout(null); setResizable(false); // Header Customer Detail JLabel hCustomerAdd = new JLabel("Insert Customer"); hCustomerAdd.setBounds(144, 21, 132, 14); getContentPane().add(hCustomerAdd); //*** Header ***// JLabel hid = new JLabel("id :"); hid.setBounds(100, 51, 89, 20); getContentPane().add(hid);
JLabel hname = new JLabel("name :"); hname.setBounds(100, 81, 120, 20); getContentPane().add(hname);
JLabel haddress = new JLabel("address :"); haddress.setBounds(100, 111, 89, 20); getContentPane().add(haddress);
JLabel htel = new JLabel("tel :"); htel.setBounds(100, 141, 89, 20); getContentPane().add(htel);
JLabel hEmail = new JLabel("Email :"); hEmail.setBounds(100, 171, 89, 20); getContentPane().add(hEmail);
JLabel htxtregister_of_commercial_No = new JLabel("register of commercial No. :"); htxtregister_of_commercial_No.setBounds(100, 201, 200, 20); getContentPane().add(htxtregister_of_commercial_No); JLabel hcapital_investment = new JLabel("capital of register :"); hcapital_investment.setBounds(100, 231, 150, 20); getContentPane().add(hcapital_investment); JLabel hวงเงิน = new JLabel("วงเงิน :"); hวงเงิน.setBounds(100, 261, 89, 20); getContentPane().add(hวงเงิน); JLabel hsellid = new JLabel("sellid :"); hsellid.setBounds(100, 291, 89, 20); getContentPane().add(hsellid); //*** Detail ***// JLabel lblid = new JLabel("Id"); lblid.setBounds(257, 51, 89, 20); getContentPane().add(lblid); JLabel lblname = new JLabel("lblName"); lblname.setBounds(257, 81, 200, 20); getContentPane().add(lblname); JLabel lbladdress = new JLabel("lblAddress"); lbladdress.setBounds(257, 111, 400, 20); getContentPane().add(lbladdress); JLabel lbltel = new JLabel("lblTel"); lbltel.setBounds(257, 141, 100, 20); getContentPane().add(lbltel); JLabel lblEmail = new JLabel("lblEmail"); lblEmail.setBounds(257, 171, 150, 20); getContentPane().add(lblEmail); JLabel lblregister_of_commercial_No = new JLabel("lblRegisty of commercial No."); lblregister_of_commercial_No.setBounds(257, 201, 100, 20); getContentPane().add(lblregister_of_commercial_No); JLabel lblcapital_investment = new JLabel("lblCapital investment"); lblcapital_investment.setBounds(257, 231, 150, 20); getContentPane().add(lblcapital_investment); JLabel lblวงเงิน = new JLabel("lblวงเงิน"); lblวงเงิน.setBounds(257, 261, 120, 20); getContentPane().add(lblวงเงิน); JLabel lblsellid = new JLabel("lblsellid"); lblsellid.setBounds(257, 291, 120, 20); getContentPane().add(lblsellid); //*** Bind Data ***// Connection connect = null; Statement s = null; try { Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/history" + "?user=root&password=027434323"); s = connect.createStatement(); String sql = "SELECT * FROM customer_nresu " + "WHERE id = '" + sid + "' "; ResultSet rec = s.executeQuery(sql); if(rec != null) { rec.next(); lblid.setText(rec.getString("Id")); lblname.setText(rec.getString("Name")); lbladdress.setText(rec.getString("Address")); lbltel.setText(rec.getString("Tel")); lblEmail.setText(rec.getString("Email")); lblregister_of_commercial_No.setText(rec.getString("Register_of_commercial_No")); lblcapital_investment.setText(rec.getString("Capital_investment")); lblวงเงิน.setText(rec.getString("วงเงิน")); lblsellid.setText(rec.getString("Sellid")); } rec.close();
} catch (Exception e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e.getMessage()); e.printStackTrace(); } try { if(s != null) { s.close(); connect.close(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); e.printStackTrace(); } // Close Button JButton btnClose = new JButton("Close"); btnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); btnClose.setBounds(400, 400, 69, 23); getContentPane().add(btnClose); JButton btnEdit = new JButton("Edit"); btnEdit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { UpdateForm(); } private void UpdateForm(){ setTitle("Customer_nresu"); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setBounds(100, 100, 760, 550); getContentPane().setLayout(null); setResizable(false);
// Header Customer Update JLabel hCustomerUpdate = new JLabel("Customer Update"); hCustomerUpdate.setBounds(144, 21, 132, 20); getContentPane().add(hCustomerUpdate);
// *** Header ***// JLabel hid = new JLabel("ID :"); hid.setBounds(100, 51, 89, 20); getContentPane().add(hid);
JLabel hname = new JLabel("Name :"); hname.setBounds(100, 81, 89, 20); getContentPane().add(hname); JLabel haddress = new JLabel("Address :"); haddress.setBounds(100, 111, 89, 20); getContentPane().add(haddress); JLabel htel = new JLabel("Tel :"); htel.setBounds(100, 141, 89, 20); getContentPane().add(htel);
JLabel hemail = new JLabel("Email :"); hemail.setBounds(100, 171, 89, 20); getContentPane().add(hemail);
JLabel hregister_of_commercial_No = new JLabel("registy of commercial No. :"); hregister_of_commercial_No.setBounds(100, 201, 89, 20); getContentPane().add(hregister_of_commercial_No);
JLabel hcapital_investment = new JLabel("capital :"); hcapital_investment.setBounds(100, 231, 89, 20); getContentPane().add(hcapital_investment); JLabel hวงเงิน = new JLabel("วงเงิน :"); hวงเงิน.setBounds(100, 261, 89, 20); getContentPane().add(hวงเงิน); JLabel hsellid = new JLabel("Sellid :"); hsellid.setBounds(100, 291, 89, 20); getContentPane().add(hsellid);
final JLabel txtid = new JLabel("id"); txtid.setBounds(207, 51, 99, 20); getContentPane().add(txtid);
final JTextField txtname = new JTextField(""); txtname.setBounds(207, 81, 99, 20); getContentPane().add(txtname);
final JTextField txtaddress = new JTextField(""); txtaddress.setBounds(207, 111, 99, 20); getContentPane().add(txtaddress); final JTextField txttel = new JTextField(""); txttel.setBounds(207, 141, 99, 20); getContentPane().add(txttel); final JTextField txtemail = new JTextField(""); txtemail.setBounds(207, 171, 99, 20); getContentPane().add(txtemail);
final JTextField txtregister_of_commercial_No = new JTextField(""); txtregister_of_commercial_No.setBounds(207, 201, 99, 20); getContentPane().add(txtregister_of_commercial_No);
final JTextField txtcapital_investment = new JTextField(""); txtcapital_investment.setBounds(207, 231, 99, 20); getContentPane().add(txtcapital_investment);
final JTextField txtวงเงิน = new JTextField(""); txtวงเงิน.setBounds(207, 261, 99, 20); getContentPane().add(txtวงเงิน); final JTextField txtsellid = new JTextField(""); txtsellid.setBounds(207, 291, 99, 20); getContentPane().add(txtsellid);
Connection connect = null; Statement s = null;
try { Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager .getConnection("jdbc:mysql://localhost/history" + "?user=root&password=027434323");
s = connect.createStatement();
String sql = "SELECT * FROM customer_nresu " + "WHERE id = '" + sid + "' ";
ResultSet rec = s.executeQuery(sql);
if (rec != null) { rec.next(); txtid.setText(rec.getString("ID")); txtname.setText(rec.getString("Name")); txtaddress.setText(rec.getString("Address")); txttel.setText(rec.getString("Tel")); txtemail.setText(rec.getString("email")); txtregister_of_commercial_No.setText(rec.getString("register_of_commercial_No")); txtcapital_investment.setText(rec.getString("capital_investment")); txtวงเงิน.setText(rec.getString("วงเงิน")); txtsellid.setText(rec.getString("Sellid")); } rec.close();
} catch (Exception e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e.getMessage()); e.printStackTrace(); }
try { if (s != null) { s.close(); connect.close(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); e.printStackTrace(); }
// Save Button JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { SaveData(txtid.getText(), txtname.getText(), txtaddress.getText(),txttel.getText(), txtemail.getText(),txtregister_of_commercial_No.getText(), txtcapital_investment.getText(), txtวงเงิน.getText(), txtsellid.getText()); dispose(); } }); btnSave.setBounds(131, 400, 69, 23); getContentPane().add(btnSave);
// Button Close JButton btnClose = new JButton("Close"); btnClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); } }); btnClose.setBounds(217, 400, 69, 23); getContentPane().add(btnClose);
}
// Update private void SaveData(String strid, String strname, String straddress,String stremail, String strtel, String strregister_of_commercial_No, String strcapital_investment, String strวงเงิน, String strsellid) { Connection connect = null; Statement s = null; try { Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/history" + "?user=root&password=027434323"); s = connect.createStatement(); String sql = "UPDATE customer_nresu " + "SET ID = '" + strid + "' " + ", Name = '" + strname + "' " + ", Address = '" + straddress + "' " + ", Tel = '" + strtel + "' " + ", Email = '" + stremail + "' " + ", register_of_commercial_No = '" + strregister_of_commercial_No +"' " + ", capital_investment = '" + strcapital_investment + "' " + ", วงเงิน = '" + strวงเงิน + "' " + ", Sellid = '" + strsellid + "' " + " WHERE ID = '"+strid+"' "; s.execute(sql); JOptionPane.showMessageDialog(null, "Record Update Successfully"); } catch (Exception e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e.getMessage()); e.printStackTrace(); } try { if(s != null) { s.close(); connect.close(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); e.printStackTrace(); } } }); btnEdit.setBounds(280, 400, 69, 23); getContentPane().add(btnEdit);
} }
|