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
3 Answers 3
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
SpringLearner
13.9k20 gold badges82 silver badges117 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
katy
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?
Nick.Mc
@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.
SpringLearner
@katy Sure,I will try what ever I can.Send me mail.Check my profile
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());
Comments
remove that " )", may be you forgot it :)
" )"at the end of your SQL String should be removed.PreparedStatement, as it provides (among other things) cleaner syntax than concatenating the parameter values.