Saturday, March 17, 2018

Create Login Form in Java Using Netbeans and SQL Server

In this tutorial you will learn to create a login form that uses SQL Server as a database. The user typed value is matched with the database value and if it matches, then a message is displayed for successful login and next window is displayed.If the value is not matched, a message is displayed which says incorrect input value.

Please watch the video because there are some task to be done beside code

Find the original video on youtube

Below is the code for login Form

private void signinActionPerformed(java.awt.event.ActionEvent evt) {                                     
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url="jdbc:sqlserver://localhost:1433;databaseName=testdb;user=sa;password=orchid";
            Connection con = DriverManager.getConnection(url);
            String sql = "Select * from test where username=? and password = ?";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1, username.getText());
            pst.setString(2, password.getText());
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                JOptionPane.showMessageDialog(null, "Username and Password Matched");
                Succes field= new Succes();
                field.setVisible(true);
                setVisible(false);
             
            }
            else{
                JOptionPane.showMessageDialog(null, "Username and password not Correct");
                username.setText("");
                password.setText("");
            }
            con.close();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
     
    }
For Reset Button:
private void resetActionPerformed(java.awt.event.ActionEvent evt) {                                      
       username.setText("");
       password.setText("");
    }   

Youtube Channel Subscription Click Here

Link for Sql Connector:

Please don't forget To Like, Share and Subscribe my Youtube video and Channel.

3 comments:

  1. where did user=sa come please ?
    when i copied the test and changed the database and table name it says that user=sa not found , what should i do?

    ReplyDelete
    Replies
    1. User sa is a primary key in database it automatically increment so when you have 2 3 values it also increment from 1 2 3 4 so it's not come
      (1,1) in a code

      Delete