我创建了一个java类,我想从数据库中获取第一条记录,我正在使用查询来获取它,但它不起作用并给出了一些异常。
使用此代码:
else if(ae.getSource()==btnfirst)
{
try{
m.rs=m.st.executeQuery("Select * from purchase ");
m.rs.first();
inv=m.rs.getInt(1);
String inv1=Integer.toString(inv);
int qty2=m.rs.getInt(5);
String qty1=Integer.toString(qty2);
rate=m.rs.getInt(6);
String rate1=Integer.toString(rate);
txtinvoice1.setText(inv1);
txtdate1.setText(m.rs.getString(2));
txtparty1.setText(m.rs.getString(3));
ctrucktype1.setSelectedItem(m.rs.getString(4));
txtcalu1.setText(qty1);
txtrate1.setText(rate1);
txtbuilty1.setText(m.rs.getString(7));
}catch(Exception e)
{e.printStackTrace();}
}
使用限制1 堆栈跟踪后
H:\Workspace\Mining Inventory\Project\src>java mdiform
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in
FROM clause.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:2
52)
at PurchaseMaster.actionPerformed(PurchaseMaster.java:307)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20
18)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
答案 0 :(得分:2)
如果您只需要首先记录您的查询应该是:
Select * from purchase LIMIT 1
您应该从数据库中询问更多信息,因为结果需要时间才能找到您。
编辑:正如您可能使用Oracle
一样,您应该尝试这样做:
SELECT * FROM purchase WHERE ROWNUM = 1
此外,您需要WHERE
中的特定内容,因为限制可以帮助您只获得一个结果,但如果您未指定任何条件,则不太可能获得您想要的结果。
答案 1 :(得分:0)
我猜你错过了一个rs.next()语句,但是在没有看到异常的情况下很难说清楚。 rs.first
如果在结果集TYPE_FORWARD_ONLY
上使用,则会引发异常。我相信这是默认的。
try{
m.rs=m.st.executeQuery("Select * from purchase ");
*** m.rs.next(); ***
inv=m.rs.getInt(1);
String inv1=Integer.toString(inv);
int qty2=m.rs.getInt(5);
答案 2 :(得分:0)
由于您只需要第一条记录,从数据库中获取所有记录并从中检索第一条记录毫无意义。我建议你限制根据默认订单选择的记录。
Select * from purchase order by purchase_date LIMIT 1