0

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?

Felix Pamittan
31.9k7 gold badges43 silver badges71 bronze badges
asked May 24, 2016 at 5:58
3
  • 2
    The " )" at the end of your SQL String should be removed. Commented May 24, 2016 at 5:59
  • You should also look at using PreparedStatement, as it provides (among other things) cleaner syntax than concatenating the parameter values. Commented May 24, 2016 at 6:20
  • Possible duplicate of ORA-00933: SQL command not properly ended Commented May 24, 2016 at 6:22

3 Answers 3

2

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

answered May 24, 2016 at 6:01
Sign up to request clarification or add additional context in comments.

3 Comments

I have a request for you if you are kind to help me. I'm working on a code for 2 weeks and I don't seem to get anywhere. It's my first time working with java and sql in the same time and I don't have a clue about what I'm doing most of the time. I my opinion I think that the code is almost done but i have little errors and I don't know how to handle them. Is there any chance that you could help me?
@katy you've been told four times what the issue is. If you apply the solutions, find they are correct then come back here and mark the answer as correct, rather than just asking for more help, this will be more constructive for everyone.
@katy Sure,I will try what ever I can.Send me mail.Check my profile
1

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());
answered May 24, 2016 at 6:08

Comments

1

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

answered May 24, 2016 at 6:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.