لايسمح بادخال أي قيمة الى الحقول
package GUI;
import java.awt.Color;
import java.awt.EventQueue;
..
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
..
import DBconnection.DbConnection;
import DBconnection.ResultSetTableModel;
..
public class ST
{
private JFrame frmStack;
private JTextField textField_admin_edit_id;
private static final String DEFAULT_Query_employee = "SELECT * FROM employee";
private static final String url= "jdbc:mysql://localhost/stack?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private static final String userName = "root";
private static final String password ="123456";
private static DbConnection dbc;
private JTextField textField_Employee_edit_address;
public static void main (String[] args)
{
System.out.println("\n\n***** MySQL JDBC Connection Testing *****");
Connection conn = null;
try
{
String userName = "root";
String password = "zeina123456";
String url = "jdbc:mysql://localhost/stack?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("\nDatabase Connection Established...");
JOptionPane.showMessageDialog(null, "Database Connection Established...");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "cannot connect to database server");
System.err.println ("Cannot connect to database server");
ex.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
System.out.println("\n***** Let terminate the Connection *****");
JOptionPane.showMessageDialog(null, "Let terminate the Connection...");
conn.close ();
System.out.println ("\nDatabase connection terminated...");
}
catch (Exception ex)
{
System.out.println ("Error in connection termination!");
}
}
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ST window = new ST();
window.frmStack.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ST() {
initialize();
}
private void initialize() {
frmStack = new JFrame();
frmStack.setTitle("STACK");
frmStack.setBounds(100, 100, 1200, 750);
frmStack.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmStack.getContentPane().setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent arg0) {
String sql= "Select * From admin";
ResultSet rs=dbc.runSelect(sql);
try {
rs.next();
textField_admin_edit_id.setText(rs.getString("Admin_Id"));
textField_admin_edit_fname.setText(rs.getString("First_Name"));
textField_admin_edit_lname.setText(rs.getString("Last_Name"));
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
/*panel_1.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent arg0) {
textField_admin_edit_id.setText(label_admin_info_id.getText());
textField_admin_edit_phone.setText(label_admin_info_phone.getText());
}
});
*/
JPanel panel = new JPanel();
tabbedPane_1.addTab("Info", null, panel, null);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("ID");
lblNewLabel.setFont(new Font("Segoe Print", Font.BOLD, 25));
lblNewLabel.setBounds(42, 72, 162, 26);
panel.add(lblNewLabel);
JLabel label_admin_info = new JLabel("");
label_admin_info.setBounds(556, 72, 305, 314);
panel.add(label_admin_info);
tabbedPane_1.addTab("Edit", null, panel_1, null);
panel_1.setLayout(null);
JLabel lblNewLabel_10 = new JLabel("ID");
lblNewLabel_10.setFont(new Font("Segoe Print", Font.BOLD, 21));
lblNewLabel_10.setBounds(27, 45, 125, 22);
panel_1.add(lblNewLabel_10);
textField_admin_edit_id = new JTextField();
textField_admin_edit_id.setEnabled(false);
textField_admin_edit_id.setBounds(260, 45, 145, 22);
panel_1.add(textField_admin_edit_id);
textField_admin_edit_id.setColumns(10);
JLabel lblAdminphoto = new JLabel("");
lblAdminphoto.setBounds(647, 45, 304, 314);
panel_1.add(lblAdminphoto);
JButton btnNewButton = new JButton("Browse");
btnNewButton.setFont(new Font("Segoe Print", Font.BOLD, 15));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jf=new JFileChooser();
jf.showOpenDialog(null);
try {
textField_admin_edit_image.setText(jf.getSelectedFile().getAbsolutePath());
Icon c = new ImageIcon(textField_admin_edit_image.getText());
lblAdminphoto.setIcon(c);
}
catch (NullPointerException e) {
}
}
});
btnNewButton.setBounds(434, 269, 97, 31);
panel_1.add(btnNewButton);
JButton btnEdit = new JButton("Edit");
btnEdit.setFont(new Font("Segoe Print", Font.BOLD, 25));
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Admindao ad = new Admindao();
Admin adm = new Admin(textField_admin_edit_fname.getText(), textField_admin_edit_lname.getText(), textField_admin_edit_username.getText(), textField_admin_edit_phone.getText(),textField_admin_edit_id.getText(),textField_admin_edit_image.getText());
try {
ad.uptadeAdmin(dbc.getConnection(), adm);
JOptionPane.showMessageDialog(null, "admin updated succesufly");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnEdit.setBounds(200, 513, 97, 30);
panel_1.add(btnEdit);
JPanel panel_2 = new JPanel();
panel_2.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent arg0) {
String sql="select * from company ;";
ResultSet rs= dbc.runSelect(sql);
try {
rs.next();
textField_admin_company_name.setText( rs.getString("Name"));
textField_admin_company_address1.setText(rs.getString("Address1"));
textField_admin_company_address2.setText(rs.getString("Address2"));
textField_admin_company_phone1.setText(rs.getString("Phone1"));
textField_admin_company_phone2.setText(rs.getString("Phone2"));
textField_admin_company_website.setText(rs.getString("Website"));
textField_admin_company_postalcode.setText(rs.getString("PostalCode"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
tabbedPane_1.addTab("Comapny", null, panel_2, "");
panel_2.setLayout(null);
JButton btnEdit_1 = new JButton("Edit");
btnEdit_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Company comp = new Company(textField_admin_company_name.getText(), textField_admin_company_address1.getText(), textField_admin_company_address2.getText(), textField_admin_company_phone1.getText(), textField_admin_company_phone2.getText(), textField_admin_company_website.getText(), textField_admin_company_postalcode.getText());
Companydao compdao = new Companydao();
try {
compdao.uptadeCompany(dbc.getConnection(), comp);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnEdit_1.setBounds(520, 575, 97, 25);
panel_2.add(btnEdit_1);
textField_admin_company_name = new JTextField();
textField_admin_company_name.setToolTipText("");
textField_admin_company_name.setBounds(265, 151, 304, 31);
panel_2.add(textField_admin_company_name);
textField_admin_company_name.setColumns(10);
JButton btnEdit_7 = new JButton("edit");
btnEdit_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Company comp = new Company(textField_admin_company_name.getText(), textField_admin_company_address1.getText(), textField_admin_company_address2.getText(), textField_admin_company_phone1.getText(), textField_admin_company_phone2.getText(), textField_admin_company_website.getText(), textField_admin_company_postalcode.getText());
Companydao cdao= new Companydao();
try {
cdao.uptadeCompany(dbc.getConnection(), comp);
JOptionPane.showMessageDialog(null, "company edited successfully!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnEdit_7.setFont(new Font("Segoe Print", Font.BOLD, 25));
btnEdit_7.setBounds(652, 278, 142, 39);
panel_2.add(btnEdit_7);
JPanel panel_3 = new JPanel();
panel_3.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent arg0) {
try {
tableModel_employee.setQuery(DEFAULT_Query_employee);
table_employee.setRowSorter(null);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
tabbedPane_3.addTab("Show", null, panel_3, null);
panel_3.setLayout(null);
JScrollPane scrollPane = new JScrollPane(table_employee);
scrollPane.setBounds(0, 86, 1024, 459);
panel_3.add(scrollPane);
table_employee= new JTable(tableModel_employee);
table_employee.setFont(new Font("Segoe Print", Font.BOLD, 16));
scrollPane.setViewportView(table_employee);
JButton button = new JButton("Refresh table");
button.setFont(new Font("Segoe Print", Font.BOLD, 18));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
tableModel_employee.setQuery(DEFAULT_Query_employee);
table_employee.setRowSorter(null);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button.setBounds(91, 32, 162, 41);
panel_3.add(button);
JButton button_1 = new JButton("Filter table");
button_1.setFont(new Font("Segoe Print", Font.BOLD, 18));
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel_employee);
table_employee.setRowSorter(sorter);
sorter.setRowFilter(RowFilter.regexFilter(textField_employee_show.getText()));
}
});
button_1.setBounds(513, 31, 154, 42);
panel_3.add(button_1);
textField_employee_show = new JTextField("");
textField_employee_show.setFont(new Font("Segoe Print", Font.BOLD, 18));
textField_employee_show.setToolTipText("inter text to filter");
textField_employee_show.setBounds(706, 39, 120, 30);
panel_3.add(textField_employee_show);
JPanel panel_4 = new JPanel();
tabbedPane_3.addTab("Add", null, panel_4, null);
panel_4.setLayout(null);
JTextField();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
comboBox_employee_edit_employee.setModel(dc);
}
});
btnEdit_2.setBounds(100, 387, 178, 53);
panel_18.add(btnEdit_2);
panel_18.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent arg0) {
String sql="select First_Name , last_Name from employee; ";
ResultSet rs= dbc.runSelect(sql);
DefaultComboBoxModel<String> dc= new DefaultComboBoxModel<String>();
try {
while(rs.next()) {
dc.addElement(rs.getString("First_Name")+" "+rs.getString("Last_Name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
comboBox_employee_edit_employee.setModel(dc);
}
});
comboBox_employee_edit_employee.setBounds(209, 76, 153, 25);
panel_18.add(comboBox_employee_edit_employee);
JButton btnShowInfo = new JButton("show info");
btnShowInfo.setFont(new Font("Segoe Print", Font.BOLD, 25));
btnShowInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s=(String)comboBox_employee_edit_employee.getSelectedItem();
if(s.isEmpty())
{
}
else{
Scanner scn = new Scanner(s);
String name=scn.next();
String last=scn.next();
scn.close();
String sql="select * from employee where First_Name='"+name+"' and Last_Name='"+last+"';";
ResultSet rs=dbc.runSelect(sql);
try {
rs.next();
textField_employee_edit_fname.setText( rs.getString("First_Name"));
textField_Employee_edit_lname.setText(rs.getString("Last_name"));
textField_Employee_edit_phone.setText(rs.getString("phone"));
textField_Employee_edit_address.setText(rs.getString("Address"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
btnShowInfo.setBounds(419, 66, 184, 35);
panel_18.add(btnShowInfo);
JComboBox comboBox_employee_delete_fullname = new JComboBox();
comboBox_employee_delete_fullname.setFont(new Font("Segoe Print", Font.BOLD, 25));
JPanel panel_6 = new JPanel();
panel_6.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent arg0) {
String sql="select First_Name , last_Name from employee; ";
ResultSet rs= dbc.runSelect(sql);
DefaultComboBoxModel<String> dc= new DefaultComboBoxModel<String>();
try {
while(rs.next()) {
dc.addElement(rs.getString("First_Name")+" "+rs.getString("Last_Name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
comboBox_employee_delete_fullname.setModel(dc);
}
});
علما أنه نجح الاتصال مع قاعدة MySQL