1

これは、jFrame のボタンのコーディングであり、単純なログイン フォーム、ユーザー名とパスワードの認証が正常に機能していますが、条件が実行されない場合は、ここで立ち往生しています。

String u_id=jTextField1.getText();
String p_word=jPasswordField1.getText();
        try
        {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName=LoginForm; user=sa; password=aptech");
        Statement st=con.createStatement();
        ResultSet result=st.executeQuery("select pass_word from employee where user_name='"+u_id+"'  ");

         if(result.next())
        {

          String pwd =  result.getString("pass_word");

           if( p_word.equals(pwd))
           {
                jLabel4.setText("you are logged in");
           }
           else if (!p_word.equals(pwd))
           {
                jLabel4.setText("Your id password is Wrong");
           }

        }
        else 
        {
            jLabel4.setText("Enter your id password again");
            jTextField1.setText("");
            jPasswordField1.setText("");
        }       
     }            
     catch (ClassNotFoundException a)
     {
          System.out.println(""+a);
     }
     catch (SQLException s)
     {
          System.out.println(""+s);
     }
4

2 に答える 2

0

必要なコードの約10倍のコードがあります。代わりにこれを行ってください(擬似コード):

代わりに次のクエリを使用してください。

select count(*)
from employee
where user_name = ?
and pass_word = ?

1つの列が返される正確に1つの行を取得します。
その列の値は、1または0

残りは自分で処理できるはずです。

于 2013-02-20T13:20:22.133 に答える
0

代わりにこれを試してください:

(IDE を使用していることを願っています) "Log.java" という名前の新しいクラスを作成します。次のコードをコピーしてクラスに貼り付けます。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Log extends JFrame {

public static void main(String[] args) {
Log frameTable = new Log();
}

JButton Login = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);

Log(){
super("Login Autentification");
setSize(300,200);
setLocation(500,280);
panel.setLayout (null); 


txuser.setBounds(70,30,150,20);
pass.setBounds(70,65,150,20);
Login.setBounds(110,100,80,20);

panel.add(Login);
panel.add(txuser);
panel.add(pass);

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}

public void actionlogin(){
Login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String puname = txuser.getText();
String ppaswd = pass.getText();
if(puname.equals("1") && ppaswd.equals("2")) {
newframe regFace =new newframe();
regFace.setVisible(true);
dispose();
} else {

JOptionPane.showMessageDialog(null,"Wrong Password or Username!");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}

}
});
}
}

次に、新しいクラス「newframe.java」を作成し、次のコードを入力します。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class newframe extends JFrame {

public static void main(String[] args) {
newframe frameTabel = new newframe();
}

JLabel welcome = new JLabel("Welcome to a New Frame");
JPanel panel = new JPanel();

newframe(){
super("Welcome");
setSize(300,200);
setLocation(500,280);
panel.setLayout (null); 

welcome.setBounds(70,50,150,60);

panel.add(welcome);

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

}

何か不明な点がある場合は、お気軽に質問するか、自分で解決してください。この素敵なコミュニティがお手伝いします。

于 2013-02-20T15:12:20.727 に答える