I’m trying to create a GUI program, simple LOGIN maintenance. I have 4 gui components namely: jlabel, jtextfield, jpasswordfield and jbutton. so far, this is my code:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class BankGui extends JApplet implements ActionListener{
// GUI components
JLabel lblUser, lblPass;
JTextField txtUser;
JPasswordField txtPass;
JButton btnOk, btnClear;
// connections to MYSQL
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet resultSet = null;
//public static Scanner in = new Scanner(System.in);
public void init(){
Container c = getContentPane();
c.setLayout( new FlowLayout() );
lblUser = new JLabel( "Username: " );
c.add( lblUser );
txtUser = new JTextField( 10 );
c.add( txtUser );
lblPass = new JLabel( "Password:" );
c.add( lblPass );
txtPass = new JPasswordField( 10 );
c.add( txtPass );
btnOk = new JButton( "OK" );
btnOk.addActionListener( this );
c.add( btnOk );
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if( btnOk ){
}
}
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/USERS", "root", "root");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT lname, fname FROM employees");
} catch (Exception e) {
e.printStackTrace();
}
}
}
But I’m stuck in the method actionperformed and my connect method. I don’t know what to put there to validate if that person who logged in is an authorized user or not.
You can’t perform SQL operation with
Appletplease change it to Java application (extends the Frame/JFrame). Read this article – What Applets Can and Cannot Do.You have to write
SELECTstatement with WHERE clause.EDIT:
Register the JDBC driver in
staticblockand write code in
actionPerformedmethod: