我试图在用户使用Java登录系统后显示用户名。有什么办法吗?
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// JDBC driver name and database URL
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/unimas";
// Database credentials
final String USER = "root";
final String PASS = "shojib420";
Connection conn = null;
Statement stmt = null;
try {
dec();
} catch (NoSuchAlgorithmException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//String name=name2.getText();
String username=user1.getText();
//String faculty=fac1.getText();
if(username.equalsIgnoreCase("") || password.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(contentPane, "OOPS You miss some filed");
}
else
{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
stmt = (Statement) conn.createStatement();
/* String sql="INSERT INTO useruser " +
"VALUES (username,password, name,faculty)";*/
String sql="SELECT * FROM useruser WHERE username='"+username+"' and password='"+password+"' ";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
{
dashboard d=new dashboard();
d.setVisible(true);
d.name=username;
dispose();
}
else
{
JOptionPane.showMessageDialog(contentPane, "Sorry "+password);
}
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e1){
//Handle errors for Class.forName
e1.printStackTrace();
}
}
}
});
此处d.name=username;
名称位于我尝试设置d.name=username;
的信息中心类中,但我无法获取用户名的值
JTextArea showName = new JTextArea();
showName.setBounds(29, 25, 105, 22);
contentPane.add(showName);
showName.setName(name);
我在仪表板类中调用该名称。知道我做错了什么或者更好的建议吗?
答案 0 :(得分:2)
dashboard d=new dashboard();
d.setVisible(true);
d.name=username;
这里你只是设置了类变量,但是没有在任何标签(或textarea或textfield)中设置变量。
应该是
dashboard d=new dashboard();
d.nameLabel.setText(username);
d.setVisible(true);