【发布时间】:2016-09-21 04:30:49
【问题描述】:
我尝试在 java 中编写一个从 sql 访问一些表的代码,但是当我尝试运行该代码时,我收到一条错误消息:
java.sql.SQLSyntaxErrorException: ORA-00933: SQL 命令不正确 结束了
这是一直困扰我的代码:
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());
}
}
}
});
这是我的两张桌子:
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)
);
有人知道这里出了什么问题吗?
【问题讨论】:
-
应该删除 SQL 字符串末尾的
" )"。 -
您还应该考虑使用
PreparedStatement,因为它提供(除其他外)比连接参数值更简洁的语法。
标签: oracle