[import java.awt.EventQueue;
public class GUI {
private JFrame frmResturant;
public JTextField firstNameField;
// Global Fields
Connection con;
Statement st;
ResultSet rs;
// Constructor for the class
/**
* Create the application.
*/
public void connection() {
/**
* The Code Below allows me connect to the sql database Login is the
* name of the database
*
*/
try {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // driver string
// stored in the
// database
Class.forName(driver);
String db = "jdbcdbc:Login"; // db = database string stored in the
// database
con = DriverManager.getConnection(db);
st = con.createStatement();
} catch (Exception ex) {
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frmResturant.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
connection();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmResturant = new JFrame();
frmResturant.getContentPane().setForeground(Color. WHITE);
frmResturant.setForeground(Color.WHITE);
frmResturant
.setIconImage(Toolkit
.getDefaultToolkit()
.getImage(
GUI.class
.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));
frmResturant.setTitle(" Resturant");
frmResturant.setResizable(false);
frmResturant.setBounds(100, 100, 722, 341);
frmResturant.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE);
frmResturant.getContentPane().setLayout(null);
JLabel lblPleaseCompleteThe = new JLabel("Please Complete The Form ");
lblPleaseCompleteThe.setForeground(new Color(0, 0, 255));
lblPleaseCompleteThe.setBounds(290, 11, 158, 14);
frmResturant.getContentPane().add(lblPleaseComplet eThe);
JLabel lblFirstName = new JLabel("First Name :");
lblFirstName.setBounds(10, 45, 72, 20);
frmResturant.getContentPane().add(lblFirstName);
firstNameField = new JTextField();
firstNameField.setBounds(90, 45, 160, 20);
frmResturant.getContentPane().add(firstNameField);
firstNameField.setColumns(10);
JButton btnButton = new JButton("button");
btnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String firstname = firstNameField.getText();
String sql = "insert into login values UserName (' " +firstname + " ')";
// Running the sql query
rs = st.executeQuery(sql);
int count = 0;
while (rs.next()) {
count = count + 1;
}
if (count == 1) {
JOptionPane.showMessageDialog(null, "Welcome");
} else if (count > 1) {
JOptionPane.showMessageDialog(null,
"Duplicate User Access Denied");
}
else {
JOptionPane.showMessageDialog(null, " User Not Found ");
}
}
catch (Exception ex) {
}
}
});
btnButton.setBounds(114, 99, 89, 23);
frmResturant.getContentPane().add(btnButton);
}
}]