Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package client.system;
- import java.awt.HeadlessException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.swing.JOptionPane;
- public class LoginForm extends javax.swing.JPanel {
- Connection con = null;
- PreparedStatement pst = null;
- ResultSet rs = null;
- /**
- * Creates new form LoginForm
- */
- public LoginForm() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- username = new javax.swing.JTextField();
- jLabel3 = new javax.swing.JLabel();
- password = new javax.swing.JPasswordField();
- login = new javax.swing.JButton();
- jLabel1.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
- jLabel1.setText("Log In");
- jLabel2.setLabelFor(username);
- jLabel2.setText("Username:");
- username.setText("Username");
- jLabel3.setLabelFor(password);
- jLabel3.setText("Password:");
- password.setText("Password");
- login.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
- login.setText("Login");
- login.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- loginActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGap(50, 50, 50)
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
- .addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
- .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(username))
- .addContainerGap())
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 283, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel3))
- .addGap(0, 43, Short.MAX_VALUE))))
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1)
- .addGap(18, 18, 18)
- .addComponent(jLabel2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel3)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap(70, Short.MAX_VALUE))
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
- this.setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- );
- }// </editor-fold>
- private void loginActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String sql="select * from login where username=? and password=?";
- try{
- con = DriverManager.getConnection("jdbc:mysql://localhost/testsql","root","mysql");
- pst = con.prepareStatement(sql);
- pst.setString(1, username.getText());
- pst.setString(2,password.getText());
- rs = pst.executeQuery();
- if(rs.next()){
- JOptionPane.showMessageDialog(null,"username and password matched");
- }else{
- JOptionPane.showMessageDialog(null, "Incorrect Username or Password");
- }
- }catch(SQLException | HeadlessException ex){
- JOptionPane.showMessageDialog(null,ex);
- }
- }
- public static void main(String args[]){
- java.awt.EventQueue.invokeLater(new Runnable(){
- public void run(){
- new LoginForm().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JButton login;
- private javax.swing.JPasswordField password;
- private javax.swing.JTextField username;
- // End of variables declaration
- }
- public class ExampleFrame extends JFrame {
- public ExampleFrame() {
- MyPanel panel = new MyPanel();
- getContentPane().add(panel);
- setTitle("My Cool Custom Panel");
- setSize(300, 200);
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public static void main(String[] args) {
- new ExampleFrame().setVisible(true);
- }
- }
Add Comment
Please, Sign In to add comment