如何使用preparedstatement setObject()提取Object []并将其转换为查询?

时间:2015-08-19 16:17:38

标签: java oracle model-view-controller

可能是一个愚蠢的问题,但我不能让查询运行。这是我的代码:

ArrayList<Attraction> getAttraction(Object[] obj) throws SQLException
{

    Connection connection = facade.getConnection();

    ArrayList<Attraction> attractions = new ArrayList<>();

    // Check obj value
    System.out.println(java.util.Arrays.toString(obj));

    PreparedStatement ps = connection.prepareStatement("SELECT DISTINCT attra.attractid,attractname,x,y,z,age,weight,"
            + "height,duration,price,status FROM attraction attra "
            + "INNER JOIN attrib_bridge ab ON attra.attractId = ab.attractId "
            + "INNER JOIN attribute attri ON ab.attributeId = attri.attributeid"
            + "WHERE attri.attributename IN ?");

    ps.setString(1, java.util.Arrays.toString(obj));

    ResultSet rs = ps.executeQuery(); 

    while(rs.next()){

        // Initialize attraction object
        Attraction attraction = new Attraction();

        attraction.setAttractionId(rs.getInt(1));
        attraction.setAttractionName(rs.getString(2));
        attraction.setxLocation(rs.getInt(3));
        attraction.setyLocation(rs.getInt(4));
        attraction.setzLocation(rs.getInt(5));
        attraction.setMinAge(rs.getInt(6));
        attraction.setMaxWeight(rs.getInt(7));
        attraction.setMinHeight(rs.getInt(8)); 
        attraction.setRideDuration(rs.getInt(9));
        attraction.setPriceRide(rs.getInt(10));
        attraction.setStatus(rs.getString(11));

        // Add new object to to movies
        attractions.add(attraction);
    }

    return attractions;
}

这是我得到的错误:

  

[温柔的游乐设施,刺激的游乐设施,乘坐交通工具]   com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:&gt;您的SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册&gt;正确的语法使用&#39; attri.attributename IN&#39; [Gentle rides,Thrill&gt; rides,Transport rides]&#39;&#39;&#39;&#39 ;在第1行

     

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)       at java.lang.reflect.Constructor.newInstance(Unknown Source)       在com.mysql.jdbc.Util.handleNewInstance(Util.java:411)       在com.mysql.jdbc.Util.getInstance(Util.java:386)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)       在com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)       在com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)       在com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)       at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)       在com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2293)       at com.ThemeParkG02.rs.control.manager.ManageAttraction.getAttraction(ManageAttraction.java:123)       at com.ThemeParkG02.rs.control.manager.Facade.getAttraction(Facade.java:85)       at com.ThemeParkG02.rs.control.listener.ViewAttractionListListener.run(ViewAttractionListListener.java:32)

我现在真的很头晕。值已经出来但我的查询不起作用。 &#39; [&#39;和&#39;]&#39;真的阻止了我的目标。有输入的人吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

注意:我错过了IN子句的问题,但我的答案在技术上是正确的,因为SQL中的第一个错误是缺少的空间。 Miljen Mikic提供的链接解释了如何解决IN条款问题。

您的错误说:

MySQLSyntaxErrorException: You have an error in >your SQL syntax

您在WHERE之前缺少空格。

<强>更新

为了提高可读性,我更喜欢这样写:

String sql = "SELECT DISTINCT attra.attractid, attractname, x, y, z, age" +
                           ", weight, height, duration, price, status" +
              " FROM attraction attra" +
              " JOIN attrib_bridge ab ON attra.attractId = ab.attractId" +
              " JOIN attribute attri ON ab.attributeId = attri.attributeid" +
             " WHERE attri.attributename IN ?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
    ps.setString(1, Arrays.toString(obj));
    try (ResultSet rs = ps.executeQuery()) {
        while (rs.next()) {
            // Process row here
        }
    }
}