SQL(mySQLワークベンチ)とJava(Eclipse)を使用して簡単なログインデータベースを作成しようとしています。他の文字列に対しても、SQLのデータベースに対して文字列入力(JTextFieldとJPasswordField)をテストする方法がわかりません。JPanelからユーザー入力をテストするにはどうすればよいですか?
ログイン資格情報が指定された文字列と等しい場合は、Javaにステートメントを出力したいと思います。私は.getText()と.getSelectedText()メソッドを使用して、各userText/passwordText内のテキストを取得しようとしている
import javax.swing.*;
import java.sql.*;
public class Hello1 extends JFrame {
private static final long serialVersionUID = 1487932324102279819L;
public static void main(String[] args) {
JFrame frame = new JFrame("Frame Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
String username0 = "java";
String password = "password";
Statement stmt = null;
try{
String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
Connection connection = DriverManager.getConnection(url, username0, password);
stmt = connection.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT * from userids ");
while(rs.next()) {
String user = rs.getString("username");
String pass = rs.getString("paswrd");
System.out.println(user);
System.out.println(pass);
}
connection.close();
}
catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("Username");
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);
String user = userText.getText();
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
String pass = passwordText.getSelectedText();
if(user.equals('b') && pass.equals('t')){
System.out.println("Correct Login");
}
System.out.println(pass);
JButton loginB = new JButton("Login");
loginB.setBounds(10, 80, 80, 25);
panel.add(loginB);
}
}
:ここに私の現在のコードです。
あなたはどこにいるのですか? – GhostCat
@GhostCat私は、ボタンを使って、SQLデータベース内のユーザー名とパスワードをチェックするクラスを呼び出す方法について悩まされました。 –