gpt4 book ai didi

java - 将变量值从 UI 传递到操作监听器

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

我正在尝试学习 Java,所以如果这是一个菜鸟问题,我深表歉意。在在这里问这个问题之前我已经进行了足够的研究。我感谢您在这里的时间和指导。

我编写了一个简单的应用程序,要求用户输入“Serverprocessorspeed”、“RAM”等信息

我需要将用户在“Serverprocessorspeed”、“RAM”字段中输入的信息传递给操作监听器。这些值依次发送到数据库服务器。

Cloudbroker() {
f1 = new JFrame("Cloud Broker");

serverprocessorspeed = new JLabel("serverprocessorspeed :");
RAM = new JLabel("RAM :");
serverstorage = new JLabel("serverstorage :");
latency = new JLabel("latency :");
Region = new JLabel("Region");

txtserverprocessorspeed = new JTextField(60);
txtRAM = new JTextField(60);
txtserverstorage = new JTextField(60);
txtlatency = new JTextField(60);
txtRegion = new JTextField(60);

btnClose = new JButton("Close");
btnSave = new JButton("Save");
btnDelete = new JButton("Delete");
btnUpdate = new JButton("Update");

btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("The information you entered has been saved");

Connection conn1 = null;
try {
String dbURL1 =
"jdbc:sqlserver://localhost\\SQLEXPRESS:1433;"
+ "databaseName=dbcloudbroker;user=XXX;password=XXXX";
System.out.println("this is connection inside the SAVE button");
conn1 = DriverManager.getConnection(dbURL1);
Statement stmt1 = conn1.createStatement();

// I need these values to be the ones the user
// enters in the feilds serverprocessorspeed and RAM.
stmt1.executeUpdate(
"INSERT INTO cloudbrokertable " + "VALUES(XXXX, XXXXX)");
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
if (conn1 != null && !conn1.isClosed()) {
conn1.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}

}
}
}
}

根据我收到的建议,我相应地编辑了我的代码

        String speed = txtserverprocessorspeed.getText();
String ram = txtRAM.getText();
String storage = txtserverstorage.getText();
String latency = txtlatency.getText();
String region = txtRegion.getText();

System.out.println(speed);

stmt1.executeUpdate("INSERT INTO cloudbrokertable VALUES ('"+speed +"','"+ram+"','"+storage+"','"+latency+"','"+region+"')");

现在出现以下错误 - com.microsoft.sqlserver.jdbc.SQLServerException:“)”附近的语法不正确。

该值已正确传递到可变速度中,我可以将其打印出来。

最佳答案

I need the information entered by the used in the fields "Serverprocessorspeed", "RAM" to be passed to Action Listener. These values are in turn sent to a database server.

不,你不知道,这不是它的工作原理(抱歉)。但是,根据您的代码片段,我建议您已经可以访问所需的信息,例如......

btnSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ram = txtRAM.getText();
String speed = txtserverprocessorspeed.getText();
String storage = txtserverstorage.getText();
String latency = txtlatency.getText();
String region = txtRegion.getText();
//...

关于java - 将变量值从 UI 传递到操作监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32132065/

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