This question already has an answer here:
这个问题已经有了答案:
- ResultSet exception - before start of result set 6 answers
- ResultSet异常——在开始结果集6个答案之前
I have tried the following code for retrieving an image stored in a database. I have created a database called image_db
that contains an table called image_details
. The table has two fields, id
and image_path
and both are of type mediumblob
.I have stored a few images in the image_path
field as binary. Now I want to retrieve & display it.
我尝试了以下代码来检索存储在数据库中的映像。我创建了一个名为image_db的数据库,其中包含一个名为image_details的表。该表有两个字段,id和image_path,它们都是mediumblob类型。我在image_path字段中存储了一些图像作为二进制的。现在我想要检索和显示它。
package cbir.imageAddition;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
enter code here
public class ImageRetrieve {
public ImageRetrieve() throws SQLException, IOException, ClassNotFoundException
{
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "image_db";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
//System.out.println("Connection url : "+url + db);
st = con.createStatement();
String sql = "select image_path from image_details where id=1";
rs = st.executeQuery(sql);
InputStream stream = rs.getBinaryStream(2);
ByteArrayOutputStream output = new ByteArrayOutputStream();
int a1 = stream.read();
while (a1 >= 0) {
output.write((char) a1);
a1 = stream.read();
}
Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
output.close();
}
}
I get the following exception when running the code above:
我在运行上述代码时遇到以下异常:
awtJan 12, 2012 12:55:48 AM cbir.imageAddition.add_image_window jButton5ActionPerformed
SEVERE: null
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5650)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5570)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5610)
at cbir.imageAddition.ImageRetrieve.<init>(ImageRetrieve.java:49)
at cbir.imageAddition.add_image_window.jButton5ActionPerformed(add_image_window.java:280)
at cbir.imageAddition.add_image_window.access$400(add_image_window.java:26)
at cbir.imageAddition.add_image_window$5.actionPerformed(add_image_window.java:89)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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: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(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.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(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java..EventDispatchThread.run(EventDispatchThread.java:90)
How is this caused and how can I solve it?
这是怎么引起的,我怎么解决呢?
2 个解决方案
#1
48
You must call rs.next() (and check that it returns true) to access the first row of the result set:
必须调用rs.next()(并检查它是否返回true)来访问结果集的第一行:
if (rs.next() {
InputStream stream = rs.getBinaryStream(1);
...
Also not that the index should be 1, since your query only selects one column.
也不是索引应该是1,因为您的查询只选择一个列。
I also don't understand the point in casting the int to a char. The method takes an int as argument. A cast to byte would at least be logical, but bytes and char are not the same thing in Java.
我也不理解将int类型转换为char的意义。该方法以int作为参数。对字节的转换至少是合乎逻辑的,但是在Java中字节和char是不一样的。
#2
1
Once if you execute the select query will get ResultSet object then iterate it you won't get this exception. ResultSet rs = null;
一旦执行了select查询,就会得到ResultSet对象,然后遍历它,就不会得到这个异常。ResultSet rs =零;
rs = statement.executeQuery("select UUID_BINARY()");
rs =声明。executeQuery(“选择UUID_BINARY()");
if(rs.next()){
newTripUUID = rs.getBytes(1);
}
#1
48
You must call rs.next() (and check that it returns true) to access the first row of the result set:
必须调用rs.next()(并检查它是否返回true)来访问结果集的第一行:
if (rs.next() {
InputStream stream = rs.getBinaryStream(1);
...
Also not that the index should be 1, since your query only selects one column.
也不是索引应该是1,因为您的查询只选择一个列。
I also don't understand the point in casting the int to a char. The method takes an int as argument. A cast to byte would at least be logical, but bytes and char are not the same thing in Java.
我也不理解将int类型转换为char的意义。该方法以int作为参数。对字节的转换至少是合乎逻辑的,但是在Java中字节和char是不一样的。
#2
1
Once if you execute the select query will get ResultSet object then iterate it you won't get this exception. ResultSet rs = null;
一旦执行了select查询,就会得到ResultSet对象,然后遍历它,就不会得到这个异常。ResultSet rs =零;
rs = statement.executeQuery("select UUID_BINARY()");
rs =声明。executeQuery(“选择UUID_BINARY()");
if(rs.next()){
newTripUUID = rs.getBytes(1);
}