gpt4 book ai didi

java - 登录系统并有权 Access

转载 作者:行者123 更新时间:2023-12-02 04:01:11 24 4
gpt4 key购买 nike

大家好,我是 java 新手,我正在尝试运行此 java 代码,但由于某种原因,它没有在 Eclipse 控制台中显示错误。它确实在这一行有一个语法错误:

b.addActionListener (new ActionListener() {

我已经尝试了所有建议,但没有取得任何进展..我知道这很简单,但我以前从未让控制台不显示任何错误。感谢您的帮助!

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

public class Loging {
Connection con;
Statement st;
ResultSet rs;

JFrame f = new JFrame("User Login");
JLabel l = new JLabel("Username");
JLabel l1 = new JLabel("password");
JTextField t = new JTextField(10);
JTextField t1 = new JTextField(10);
JButton b = new JButton("Login");

public static void main(String[] args) {
// TODO Auto-generated method stub
}

public void Loging1() {
connect();
frame();
}

public void connect() {
try {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";// Driver needed for
// connection
Class.forName(driver);// Class.forName(driver);

String db = "jdbc:odbc:db1";
con = DriverManager.getConnection(db);
st = con.createStatement();

} catch (Exception ex) {

}

}

public void frame()
{
f.setSize (600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

JPanel p = new JPanel();
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
p.add(b);

f.add(p);
b.addActionListener (new ActionListener() {


}

public void actionPerformed(ActionEvent e) {
try {
String user = t.getText().trim();
String pass = t1.getText().trim();

String sql = "select user, pass from Table1 where user = '" + user
+ "'and pass = '" + pass + "'";// for sql query
// String sql =
// "select user, pass from Table1 where user = '"+user+"'pass = '"+pass+"'";
// Getting the fields user and pass checks tthat both are equal to
// each orther
rs = st.executeQuery(sql);// executes query

int count = 0;// cont the rows for the query
while (rs.next()) {
count = count + 1;// if no rows are returned then no user exist,
// if count is = to 1 then user exist }
}

if (count == 1)// if = to 1 user exist
{
JOptionPane.showMessageDialog(null, "Logged in!");
} else if (count > 1)// if more that one
{
JOptionPane.showMessageDialog(null, "Duplicate user, DENIED!");
}

else {
JOptionPane.showMessageDialog(null, "user not found");
}

} catch (Exception ex) {

}
// public static void main(String[] args) {

}
// New Loging1 ();
}

最佳答案

这个...

b.addActionListener (new ActionListener() {

}

将会给您一个编译器错误,因为您未能满足 ActionListener 接口(interface)的要求,即实现 actionPerformed 方法。

更像是......

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.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Loging {

Connection con;
Statement st;
ResultSet rs;

JFrame f = new JFrame("User Login");
JLabel l = new JLabel("Username");
JLabel l1 = new JLabel("password");
JTextField t = new JTextField(10);
JTextField t1 = new JTextField(10);
JButton b = new JButton("Login");

public static void main(String[] args) {
// TODO Auto-generated method stub
}

public void Loging1() {
connect();
frame();
}

public void connect() {
try {
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";//Driver needed for connection
Class.forName(driver);// Class.forName(driver);

String db = "jdbc:odbc:db1";
con = DriverManager.getConnection(db);
st = con.createStatement();

} catch (Exception ex) {

}

}

public void frame() {
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

JPanel p = new JPanel();
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
p.add(b);

f.add(p);
b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
try {
String user = t.getText().trim();
String pass = t1.getText().trim();

String sql = "select user, pass from Table1 where user = '" + user + "'and pass = '" + pass + "'";//for sql query
//String sql = "select user, pass from Table1 where user = '"+user+"'pass = '"+pass+"'";
//Getting the fields user and pass checks tthat both are equal to each orther
rs = st.executeQuery(sql);//executes query

int count = 0;//cont the rows for the query
while (rs.next()) {
count = count + 1;//if no rows are returned then no user exist, if count is = to 1 then user exist }
}

if (count == 1)//if = to 1 user exist
{
JOptionPane.showMessageDialog(null, "Logged in!");
} else if (count > 1)//if more that one
{
JOptionPane.showMessageDialog(null, "Duplicate user, DENIED!");
} else {
JOptionPane.showMessageDialog(null, "user not found");
}

} catch (Exception ex) {

}
//public static void main(String[] args) {

}
// New Loging1 ();
});
}
}

至少会编译(抱歉,还有一些其他格式问题我不想讨论)

您还应该学习如何使用 PreparedStatements 请参阅 Using Prepared Statements更多详情,它们通常更灵活、更安全。

JdbcOdbcDriver 已折旧,您不应使用它。类似于 UCanAccess可能是一个更合适的解决方案,但您需要做一些研究

您可能还想查看How to Write an Action ListenersNested Classes

关于java - 登录系统并有权 Access ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34867831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com