katy
katy

Reputation: 97

SQL command not ended properly

I try to make a code in java that accesses some tables from sql but when I try to run the code I get an error saying:

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

Here is the code that it's been giving me troubles:

history.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent actionEvent)
    {
        for(int i = 0; i < table.getRowCount(); i++)
            for(int j = 0; j < table.getColumnCount(); j++)
                table.setValueAt("", i, j);

        int i=0;
        try
        {
            rs = stmt.executeQuery("SELECT toyname, toyid, price "
                    +" FROM toys t, userbuy u " 
                                                +" WHERE u.toyid=t.toyid "
                    +" AND u.userid= "+user1.getUserid()+" )");
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try {
                if(rs.next())
                {
                    table.setValueAt(rs.getString(1), i, 0);
                    table.setValueAt(rs.getString(2), i, 1);
                    table.setValueAt(rs.getString(3), i, 2);
                    i++;
                    while(rs.next())
                    {
                        table.setValueAt(rs.getString(1), i, 0);
                        table.setValueAt(rs.getString(2), i, 1);
                        table.setValueAt(rs.getString(3), i, 2);
                        i++;
                    }
                }
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }
    }
});

Those are my two tables:

CREATE TABLE users
(
  userid    NUMBER(2) NOT NULL CONSTRAINT users_pk PRIMARY KEY,
  username        VARCHAR(17) NOT NULL,
  password    VARCHAR(20),
  email   VARCHAR(20),
  adress    VARCHAR(20),
  CNP     VARCHAR(14)
);
CREATE TABLE userbuy
(
  userid    NUMBER(2),
  buyid    NUMBER(2) ,
  toyid NUMBER(2),
  CONSTRAINT userid_fk FOREIGN KEY (userid) REFERENCES users(userid),
  CONSTRAINT buyid_fk FOREIGN KEY (buyid) REFERENCES buy(buyid)
);

Does anyone know what is wrong here?

Upvotes: 0

Views: 190

Answers (3)

Moolerian
Moolerian

Reputation: 562

remove that " )", may be you forgot it :)

Upvotes: 1

george
george

Reputation: 41

correct sql query :

rs = stmt.executeQuery("SELECT t.toyname, t.toyid, t.price "
                  +" FROM toys t, userbuy u "+" WHERE u.toyid=t.toyid "
                   +" AND u.userid= "+user1.getUserid());

Upvotes: 1

SpringLearner
SpringLearner

Reputation: 13854

your sql query is wrong.correct sql

rs = stmt.executeQuery("SELECT toyname, toyid, price "
                            +" FROM toys t, userbuy u "                                                     +" WHERE u.toyid=t.toyid "
                            +" AND u.userid= "+user1.getUserid());

It is advisable to use PreparedStatement to get rid of sql injection

Example of PreparedStatement

Upvotes: 2

Related Questions