非静态变量 username 不能从静态内容引用

问题描述 投票:0回答:1

我写了这个测试代码:

private static void recordPresensi() 抛出 SocketException { 尝试{
com.mysql.jdbc.Connection koneksi = (com.mysql.jdbc.Connection) DriverManager.getConnection("jdbc:mysql://10.10.10.51/db_apps_ad", "root", ""); koneksi.createStatement().executeUpdate("插入 tbl_presensi 值 (NULL,'" + getCurrentDateTime() + "',NULL,NULL'" + getIPAddress() + "','"
+ 用户名.getText() + "')"); } catch (SQLException ex) { 例如:printStackTrace(); } }

但它给出了以下错误:

非静态变量 username 不能从静态内容中引用 在行 "+ username.getText() + "')");"

如何让我的方法识别我的类变量?

我尝试创建一个名为 String username = username.getText(); 的字符串对象在主要方法中。然后通过用户名给他打电话,但仍然没有成功。希望各位朋友的回答能够解决我的程序

java variables compiler-errors non-static
1个回答
0
投票

您是否尝试过将其设置为公开?

© www.soinside.com 2019 - 2024. All rights reserved.