我正在尝试创建一个GUI程序,简单的LOGIN维护。我有4个gui组件,即:jlabel,jtextfield,jpasswordfield和jbutton。到目前为止,这是我的代码:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class BankGui extends JApplet implements ActionListener{
// GUI components
JLabel lblUser, lblPass;
JTextField txtUser;
JPasswordField txtPass;
JButton btnOk, btnClear;
// connections to MYSQL
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet resultSet = null;
//public static Scanner in = new Scanner(System.in);
public void init(){
Container c = getContentPane();
c.setLayout( new FlowLayout() );
lblUser = new JLabel( "Username: " );
c.add( lblUser );
txtUser = new JTextField( 10 );
c.add( txtUser );
lblPass = new JLabel( "Password:" );
c.add( lblPass );
txtPass = new JPasswordField( 10 );
c.add( txtPass );
btnOk = new JButton( "OK" );
btnOk.addActionListener( this );
c.add( btnOk );
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if( btnOk ){
}
}
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/USERS", "root", "root");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT lname, fname FROM employees");
} catch (Exception e) {
e.printStackTrace();
}
}
}
但我陷入了actionperformed方法和我的连接方法。我不知道该怎么做才能验证登录的人是否是授权用户。
答案 0 :(得分:2)
您不能使用Applet
执行 SQL操作,请将其更改为Java应用程序(扩展 Frame / JFrame )。阅读这篇文章 - What Applets Can and Cannot Do。
您必须使用WHERE子句编写SELECT
语句。
String sql="SELECT userName FROM employees WHERE UserName=? and Password=?";
编辑:
在static
块
static {
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception ex) {
System.err.println(ex);
}
}
并在actionPerformed
方法中编写代码:
PreparedStatement stmt=null;
boolean isFound=false;
try{
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/USERS", "root", "root");
String sql="SELECT userName FROM employees WHERE UserName=? and Password=?";
stmt=connection.prepareStatement(sql);
stmt.setString(1,txtUser.getText());
stmt.setString(2,txtPass.getPassword());
resultSet=stmt.executeQuery();
if(resultSet.next()){
isFound=true;
}
//
}catch(SQLException ex){
System.err.println(ex);
}finally{
if(stmt!=null){
try{
stmt.close();
}catch(Exception ex) { /* */ }
}
if(connection!=null){
try{
connection.close();
}catch(Exception ex) { /* */ }
}
}
答案 1 :(得分:0)
将您的代码更改为此
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("OK")){
if(connect(txtUser.getText(), txtPass.getText())) {
// Valid
}
else {
//Invalid
}
}
}
public boolean connect(String usr, String pwd){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/USERS", "root", "root");
PreparedStatement ps= connection.prepareStatement("SELECT lname, fname FROM employees where username=? and password =?");
ps.setString(1,usr);
ps.setString(2,pwd);
resultSet = ps.executeQuery();
if(resultSet.next()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}