我想从数据库中提取用户名,使用JAVA的Jframe。

huangapple go评论76阅读模式
英文:

I want to present the username from database JAVA Jframe

问题

以下是您提供的代码的翻译部分:

登录页面的代码:

import java.awt.EventQueue;
import java.sql.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login {

    // ...(省略其他代码)

    public Login() {
        initialize();
        connected = DBConnection.DBconnector();
    }

    private void initialize() {
        frame = new JFrame();
        // ...(省略其他代码)

        JLabel lblNewLabel = new JLabel("用户名:");
        // ...(省略其他代码)

        JLabel lblPassword = new JLabel("密码:");
        // ...(省略其他代码)

        JButton Loginbtn = new JButton("登录");
        // ...(省略其他代码)

        MainLogo = new JLabel("");
        // ...(省略其他代码)
    }
}

欢迎页面的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.proteanit.sql.DbUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

public class UserPage extends JFrame {

    // ...(省略其他代码)

    public UserPage() {
        connected = DBConnection.DBPreconnector();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 576, 410);
        contentPane = new JPanel();
        // ...(省略其他代码)

        JButton bntLoadData = new JButton("显示用户数据");
        // ...(省略其他代码)

        JLabel lblNewLabel = new JLabel("你好,用户名");
        // ...(省略其他代码)

        bntLoadData.setFont(new Font("Tahoma", Font.BOLD, 20));
        // ...(省略其他代码)

        table = new JTable();
        // ...(省略其他代码)
    }
}
英文:

I have two JFrame windows one for the login and the second for the welcome. Now I want to present the Firstname and the Lastname of the connected user near to the hello in the welcome screen.

I tried many things but none of them worked so I'm asking for you help. I just started to learn GUI:

我想从数据库中提取用户名,使用JAVA的Jframe。
我想从数据库中提取用户名,使用JAVA的Jframe。

The login code:

import java.awt.EventQueue;
import java.sql.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login {

	private JFrame frame;
	private JLabel MainLogo;
	String 	Firstname=null; 
	String	Lastname=null;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	Connection connected = null;
	private JTextField UsernameFiled;
	private JPasswordField passwordField;
	
	public Login() {
		initialize();
		
		connected = DBConnection.DBconnector();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 560, 256);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("UserName: ");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblNewLabel.setBounds(160, 11, 140, 50);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblPassword = new JLabel("PassWord: ");
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblPassword.setBounds(160, 72, 140, 40);
		frame.getContentPane().add(lblPassword);
		
		JButton Loginbtn = new JButton("Login");
		Loginbtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					
					
					String query = "Select * from Users where nickname=? and password=?";
					PreparedStatement pst = connected.prepareStatement(query);
					pst.setString(1, UsernameFiled.getText());
					pst.setString(2, passwordField.getText());
					
					ResultSet res = pst.executeQuery();
					Firstname = res.getString("Firstname");
					Lastname = res.getString("Lastname");
					int c=0;
					
					while(res.next()) {
						c++;
					}
					
					if(c == 0) {
						JOptionPane.showMessageDialog(null, "You didnt put any username or password!");
					}
					else if(c == 1) {
						JOptionPane.showMessageDialog(null, "Hello to you, " + Firstname +" "+ Lastname);
						frame.dispose();
						UserPage UserFrame = new UserPage();
						UserFrame.setVisible(true);
						
					}else if( c > 1) {
						JOptionPane.showMessageDialog(null, "Duplicate Username and Password");
					}else {
						JOptionPane.showMessageDialog(null, "Username and Password are inncorect...Try agian!");
					}
					
					pst.close();
					res.close();
					
				}catch(Exception LoginError) {
					JOptionPane.showMessageDialog(null, LoginError);

				}
				
				
				
				
			}
		});
		
		Loginbtn.setFont(new Font("Tahoma", Font.BOLD, 20));
		Loginbtn.setBounds(160, 148, 186, 40);
		frame.getContentPane().add(Loginbtn);
		
		UsernameFiled = new JTextField();
		UsernameFiled.setFont(new Font("Tahoma", Font.BOLD, 16));
		UsernameFiled.setBounds(310, 28, 186, 25);
		frame.getContentPane().add(UsernameFiled);
		UsernameFiled.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setFont(new Font("Tahoma", Font.BOLD, 16));
		passwordField.setBounds(310, 84, 186, 25);
		frame.getContentPane().add(passwordField);
		
		MainLogo = new JLabel("");
		Image imgs = new ImageIcon(this.getClass().getResource("/login.png")).getImage();
		MainLogo.setIcon(new ImageIcon(imgs));
		MainLogo.setBounds(10, 11, 128, 128);
		frame.getContentPane().add(MainLogo);
	}
}

The Welcome Code:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import net.proteanit.sql.DbUtils;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

public class UserPage extends JFrame {

	private JPanel contentPane;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserPage frame = new UserPage();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	Connection connected=null;
	private JTable table;
	
	public UserPage() {
		connected=DBConnection.DBPreconnector();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 576, 410);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton bntLoadData = new JButton("Show Users Data");
		bntLoadData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			
				
				try {
					
					String query = "Select ID,Age,Password,Firstname,Lastname from users";
					PreparedStatement pst = connected.prepareStatement(query);
					
					ResultSet res = pst.executeQuery();
					table.setModel(DbUtils.resultSetToTableModel(res));
					
					
				} catch (Exception Error) {
					
					Error.printStackTrace();
				}
				
			}
		});
		
		JLabel lblNewLabel = new JLabel("Hello,  "  + "The Username name");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblNewLabel.setBounds(10, 11, 280, 19);
		contentPane.add(lblNewLabel);
		
		bntLoadData.setFont(new Font("Tahoma", Font.BOLD, 20));
		bntLoadData.setBounds(162, 102, 238, 23);
		contentPane.add(bntLoadData);
		
		table = new JTable();
		table.setBounds(28, 136, 508, 221);
		contentPane.add(table);
	}
}

答案1

得分: 0

在你的代码中,似乎没有加载登录用户在欢迎界面的用户名或密码。你可以选择从登录界面传递这些信息,或者重新查询数据库以获取名字和姓氏。

我建议,通过构造函数简单地从欢迎界面传递名字和姓氏。

在成功登录时,只需在构造函数中传递名字:登录代码添加部分:

} else if (c == 1) {
    JOptionPane.showMessageDialog(null, "Hello to you, " + Firstname + " " + Lastname);
    frame.dispose();
    UserPage UserFrame = new UserPage(Firstname, Lastname);
    UserFrame.setVisible(true);
}

在欢迎界面中,添加一个构造函数来处理这个问题:

public UserPage(String loggedInFirstName, String loggedInLastName){
   // 你的其余代码
   JLabel lblNewLabel = new JLabel("Hello,  "  + loggedInFirstName + " " + loggedInLastName );
   // 你的其余代码。
}

像这样的方法可能适用于你的情况,如果对你有用,请不要忘记将答案标记为已接受。

英文:

In your code, you does not seem to load the username or password of the logged in user at the welcome frame. Either you will have to pass that from the login frame or you will have to re-query the database and get firstname and lastname.

I suggest, simply pass the firstname and lastname from welcome screen via constructor.

In successful login, simply pass the name in the constructor: Login code addition:

                } else if(c == 1) {
                    JOptionPane.showMessageDialog(null, "Hello to you, " + Firstname +" "+ Lastname);
                    frame.dispose();
                    UserPage UserFrame = new UserPage( Firstname, Lastname );
                    UserFrame.setVisible(true);
                }

In the welcome frame, add a constructor to handle this:

public UserPage(String loggedInFirstName, String loggedInLastName){
   // your remaining code
   JLabel lblNewLabel = new JLabel("Hello,  "  + loggedInFirstName + " " + loggedInLastName );
   // your remaining code.
}

A approach like this might work for your case and don't forget to mark the answer as accepted if this works for you

huangapple
  • 本文由 发表于 2020年4月10日 08:15:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61132224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定