Vivek Sood
Vivek Sood

Reputation: 3

Passing variable values from UI to action listener

I am trying to learn Java so I apologize if this is a rookie question. I have researched enough before asking this question here. I appreciate your time and guidance here.

I have written a simple application where I am asking user to enter information like "Serverprocessorspeed", "RAM" etc

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

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();
                }

            }
        }
    }
}

Based upon the suggestion I received, I edited my code accordingly

        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+"')");

Now get the following error - com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ')'.

The value is correctly passed into the variable speed and I can print it out.

Upvotes: 0

Views: 39

Answers (2)

Trung Pham
Trung Pham

Reputation: 28

It's the problem with your SQL statement. You need to check if it does work. Simply open your DB to test it with a sample statement like:

INSERT INTO cloudbrokertable VALUES ('100','DDR2','500','1000','US')

If this doesn't work it means you need to fix your statement. I hope this help.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

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.

No, you don't, this isn't how this works (sorry). But, based on you code snippet, I would suggest you already have access to the information you need, for example...

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();
        //...

Upvotes: 1

Related Questions