gpt4 book ai didi

java - SQL 错误 (java.sql.SQLException)

转载 作者:行者123 更新时间:2023-11-29 13:26:21 26 4
gpt4 key购买 nike

我在 Java 中得到了这两个表单,它们链接到 MySQL 中的两个表。我已经完成连接,数据库保存通过前端输入的数据。但这两种形式是相互联系的。就像当在一个表单中单击一个按钮时,它会将所有数据以表格的形式发送到另一个表单。这是我为此所做的编码:

int dress1=100;
double price1=Double.parseDouble(Price1.getText());
DefaultTableModel CurrentPurchases= new DefaultTableModel();
int rows=CurrentPurchases.getRowCount();
if (rows>0){
for (int i = 0; i < rows; i++) {
CurrentPurchases.removeRow(0); }
}
try{
Connection connection=getConnection();
stmt=connection.createStatement();
String Buy1Query1="Update Products set Quantity=Quantity-1 where Product_no=1;";
String Buy1Query2="Insert into Buy values('"+Pname1.getText()+"',"+price1+");";
stmt.executeUpdate(Buy1Query1);
stmt.executeUpdate(Buy1Query2);
dress1--;
if(dress1==0){
JOptionPane.showMessageDialog(null,"Sorry, This Item is Out of Stock!");
}
new ShoppingCart().setVisible(true);
String Pname="";
double Price;
PreparedStatement buyquery=connection.prepareStatement("Select * from Buy;");
rs=buyquery.executeQuery();
Pname=rs.getString("ProductName");
Price=rs.getDouble("Price");
CurrentPurchases.addRow(new Object[]{Pname,Price});

}
catch(SQLException ex){
ex.printStackTrace();
ex.getErrorCode();
}
finally{}

当我运行该文件时,出现以下错误:

java.sql.SQLException
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at Fashion_Footwear.Buy1ActionPerformed(Fashion_Footwear.java:370)
at Fashion_Footwear.access$000(Fashion_Footwear.java:19)
at Fashion_Footwear$1.actionPerformed(Fashion_Footwear.java:100)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

当我点击此错误时:

 at Fashion_Footwear.Buy1ActionPerformed(Fashion_Footwear.java:370)

它给了我这个错误代码:

    Pname=rs.getString("ProductName");

我是这一切的新手,如果有人尽快帮助我,我将非常感激。先感谢您!

表购买:

create table buy(ProductName varchar(100),Price decimal(7,2));

最佳答案

问题是您的ResultSet位于第一行之前。您应该使用如下代码:

PreparedStatement buyquery=connection.prepareStatement("Select * from Buy;");
rs=buyquery.executeQuery();
if (rs.next())
{
Pname=rs.getString("ProductName");
}

关于java - SQL 错误 (java.sql.SQLException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20082703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com