Here's my code......
package MyProject;
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.lang.*;
/**
*
* @author hp
*/
public class User1 extends javax.swing.JFrame {
Connection con = null;
PreparedStatement ps;
ResultSet rs;
/**
* Creates new form User1
*/
public User1() {
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() {
label1 = new javax.swing.JLabel();
Back1 = new javax.swing.JButton();
label2 = new javax.swing.JLabel();
username = new javax.swing.JTextField();
password = new javax.swing.JTextField();
Submit1 = new javax.swing.JButton();
label3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
label1.setText("Username:");
Back1.setText("Back");
Back1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Back1ActionPerformed(evt);
}
});
label2.setText("Password:");
username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
usernameActionPerformed(evt);
}
});
password.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passwordActionPerformed(evt);
}
});
Submit1.setText("Submit");
Submit1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Submit1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(Back1))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(label1)
.addGap(103, 103, 103)
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(label2)
.addGap(107, 107, 107)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(145, 145, 145)
.addComponent(Submit1))
.addGroup(layout.createSequentialGroup()
.addGap(121, 121, 121)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(161, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(Back1)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(label1))
.addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(label2))
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addComponent(Submit1)
.addGap(63, 63, 63)
.addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(184, Short.MAX_VALUE))
);
pack();
}//
private void Back1ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
private void usernameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void passwordActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void Submit1ActionPerformed(java.awt.event.ActionEvent evt) {
con=MysqlConnection.ConnectionDB();
String Query="select * from users where Username=? AND Password=?";
try{
ps = con.prepareStatement(Query);
ps.setString(1,username.getText());
ps.setString(2,password.getText());
rs = ps.executeQuery(Query);
if(rs.next())
{
JOptionPane.showMessageDialog(null,"invalid","invalid",JOptionPane.ERROR_MESSAGE);
}
else
{
this.dispose();
}
}
catch (SQLException | HeadlessException ex) {
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(User1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new User1().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton Back1;
private javax.swing.JButton Submit1;
private javax.swing.JLabel label1;
private javax.swing.JLabel label2;
private javax.swing.JLabel label3;
private javax.swing.JTextField password;
private javax.swing.JTextField username;
// End of variables declaration
}
What I have tried:
here'a MysqlConnection file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyProject;
import java.awt.HeadlessException;
import java.sql.*;
import javax.swing.*;
/**
*
* @author hp
*/
public class MysqlConnection {
Connection con=null;
public static Connection ConnectionDB(){
try{
System.out.println("1");
Class.forName("com.jdbc.mysql.Driver");
System.out.println("1");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/airlines?autoReconnect=true&useSSL=false", "root","riyard");
JOptionPane.showMessageDialog(null,"Connected to Database");
return con;
}
catch(ClassNotFoundException | SQLException | HeadlessException e){
JOptionPane.showMessageDialog(null,e);
return null;
}
}
}
donno how to fix this.. Please help me...