我正在开发一个java项目,它应该显示来自MySQl epos数据库的产品。 gui有两个可滚动的jpanel,一个用于类别,在启动时动态填充每个类别的按钮和一个Jlabel,它显示(如果存在于DB中)来自中等blob字段的图像。 第二个scrollabe jpanel应该通过提取和加载显示产品名称的按钮以及显示标签的标签来响应类别面板中按钮的单击,如果该类别中的所有产品都显示中等blob的图像。
我的类别面板可以正常加载按钮和图像,问题在于产品面板在我选择包含没有图像的产品的类别时加载正常但在尝试加载产品时会引发以下错误:
java.sql.SQLException: After end of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
at com.mysql.jdbc.ResultSetImpl.getBytes(ResultSetImpl.java:1923)
at com.mysql.jdbc.ResultSetImpl.getBytes(ResultSetImpl.java:1917)
at gapricing.app.first.GuiEvents.populateProducts(GuiEvents.java:103)
at gapricing.app.first.GuiEvents.actionPerformed(GuiEvents.java:78)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
sql查询由DataLogic类的getProducts方法生成,如下所示:
package gapricing.app.data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DataLogic {
MySQLConnect mysqlConnect = new MySQLConnect();
private Connection connect = null;
private PreparedStatement preparedStatement = null;
public ResultSet getCategories(){
try {
connect = mysqlConnect.connDatabase();
preparedStatement = connect.prepareStatement("SELECT image, name, texttip from CATEGORIES ORDER BY name");
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet;
} catch (Exception e){
}
return null;
}
public ResultSet getProducts(String catName){
try {
connect = mysqlConnect.connDatabase();
preparedStatement = connect.prepareStatement("SELECT IMAGE, NAME FROM PRODUCTS WHERE CATEGORY = (SELECT ID FROM CATEGORIES WHERE NAME = ?) ORDER BY NAME");
preparedStatement.setString(1, catName);
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet;
} catch (Exception e){
}
return null;
}
//close the resultSet
public void close(){
try {
if (preparedStatement != null){
preparedStatement.close();
}
if (connect != null){
connect.close();
}
if (mysqlConnect != null){
mysqlConnect.close();
}
} catch (Exception e){
}
}
}
这将ResultSet返回给名为GuiEvents的类的populateProducts()方法,其相关部分为:
public void populateProducts(String catName){
ResultSet prodResults = null;
prodResults = dataLogic.getProducts(catName);
gui.prodList.removeAll();
try{
int count = 0;
while (prodResults.next()) {
++count; // Get data from the current row and use it
}
prodResults.beforeFirst();
GridLayout prodLayout = new GridLayout(count,2);
gui.prodList.setLayout(prodLayout);
while (prodResults.next()){
System.out.println(prodResults.getString(2) + " , " + prodResults.getBytes(1));
JButton prodBtn = new JButton();
JLabel prodImgLabel = new JLabel();
if (prodResults.getBytes(1) != null){
prodImgLabel.setIcon(displayImage(results.getBytes(1)));
}
prodBtn.setText(prodResults.getString(2));
prodImgLabel.setHorizontalAlignment(JLabel.CENTER);
prodBtn.setHorizontalAlignment(JButton.LEFT);
prodBtn.setBorderPainted(false);
prodBtn.setActionCommand(prodResults.getString(2));
prodBtn.addActionListener(this);
gui.prodList.add(prodImgLabel);
gui.prodList.add(prodBtn);
gui.prodList.updateUI();
}
} catch (Exception e) {
e.printStackTrace();
}
try{
prodResults.close();
} catch (Exception e){
e.printStackTrace();
}
dataLogic.close();
}
我对java很新,答案很可能是直截了当地盯着我看。其他帖子似乎暗示关闭数据库连接和结果集是一个问题,我很确定我这样做了。有人可以帮忙吗?谢谢