Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import javax.swing.*;
- public class javaconnect {
- public static Connection ConnecDB(){
- Connection conn = null;
- String url = "jdbc:mysql://localhost:3306/testing";
- String driver = "com.mysql.jdbc.Driver";
- String user = "root";
- String pass = "root";
- try {
- Class.forName(driver);
- conn = DriverManager.getConnection(url, user, pass);
- if (conn == null) {
- System.out.println("Connection cannot be established");
- }
- System.out.println("Connected to database");
- return conn;
- } catch (Exception e) {
- System.out.println(e);
- }
- return null;
- }
- import java.awt.*;
- import java.sql.*;
- import javax.swing.*;
- public class Login extends javax.swing.JFrame {
- Connection conn = null;
- ResultSet rs=null;
- PreparedStatement pst=null;
- public Login() {
- initComponents();
- }
- private void txt_usernameActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void cmd_loginActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String sql = "select * from employee_info where username=? and password=?";
- try{
- pst=conn.prepareStatement(sql);
- pst.setString(1, txt_username.getText());
- pst.setString(2, txt_password.getText());
- rs=pst.executeQuery();
- if (rs.next()) {
- JOptionPane.showMessageDialog(null, "Login Sucessfully");
- employee_info s = new employee_info();
- s.setVisible(true);
- }
- else{
- JOptionPane.showMessageDialog(null, "Incorrect Username or password");
- }
- }
- catch(Exception e){
- JOptionPane.showMessageDialog(null,e);
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- 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 ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Login().setVisible(true);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement