mysql - Java DB 选择特定行上的特定列

标签 mysql sql javadb

这里是第一篇文章:) 疯狂寻求帮助。

我想做的是检索数据库中以 blob 形式存储的特定图像。我不明白为什么这个查询没有执行,我一到达executeQuery 语句就收到异常。

我的 table 是:

名称 x 坐标 y 坐标 vista 第一屏 0 0 图片 第二屏 0 1 img2 ...等等

ResultSet rs = null;
Statement stmnt = null;
Connection con = null;

String host = ...
String unm = ...
String pswrd = ...

BufferedImage imgt = null;
InputStream fis = null;
int xcoord;
int ycoord;
int newcoord;

 String SQLNorth = "select vista from location where xcoordinate = "+xcoord+" and ycoordinate = "+newcoord;
            newcoord = ycoord + 1;
            System.out.println("New coord x and y are" + xcoord + newcoord);

            con = DriverManager.getConnection(host, unm, pswrd);
            stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = stmnt.executeQuery(SQLNorth);
            rs.next();  
            fis = rs.getBinaryStream(1);
            imgt = javax.imageio.ImageIO.read(fis);
        Image newImg = SwingFXUtils.toFXImage(imgt, null);
        img_1.setImage(newImg);

最佳答案

我的猜测是,这与您构建查询的方式有关。尝试使用准备好的语句来代替。

ResultSet rs = null;
PreparedStatement stmnt = null;
Connection con = null;

String host = ...
String unm = ...
String pswrd = ...

BufferedImage imgt = null;
InputStream fis = null;
int xcoord;
int ycoord;
int newcoord;

String SQLNorth = "select vista from location where xcoordinate = ? and ycoordinate = ?";

newcoord = ycoord + 1;
System.out.println("New coord x and y are" + xcoord + newcoord);

con = DriverManager.getConnection(host, unm, pswrd);

stmnt = con.prepareStatement(SQLNorth);
stmnt.setInt(1, xcoord);
stmnt.setInt(2, newcoord);

rs = stmnt.executeQuery(SQLNorth);
rs.next();  
fis = rs.getBinaryStream(1);
imgt = javax.imageio.ImageIO.read(fis);
Image newImg = SwingFXUtils.toFXImage(imgt, null);
img_1.setImage(newImg);

关于mysql - Java DB 选择特定行上的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29499029/

相关文章:

SQL查询获取最多

sql - Microsoft SSMS 中的运行总计

JavaDB 不会在应用程序启动时启动,给我一个 java.sql.SQLNonTransientConnectionException :

derby - JAVA DB:Shutdown Derby不起作用

MySQL过滤可能的空值

php - 转义 MySQL 通配符

php - Codeigniter 不允许用户向数据库添加重复的电子邮件

sql select 使用 mysql 查询浏览器

mysql - SQL-CASE NULL

mysql - 如何在MySQL中设置auto_increment字段的preparedStatement()值