传球达阵参数prepare声明 - 让" java.sql.SQLFeatureNotSupportedException" [英] Passing array parameter in prepare statement - getting "java.sql.SQLFeatureNotSupportedException"

查看:1392
本文介绍了传球达阵参数prepare声明 - 让" java.sql.SQLFeatureNotSupportedException"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对错误java.sql.SQLFeatureNotSupportedException在我的prepare声明。我使用的MySQL数据库。

下面是我的code。

 类TMP {
公共静态无效的主要(字符串ARG []){    尝试{
        的Class.forName(com.mysql.jdbc.Driver);
        康涅狄格州的连接=的DriverManager.getConnection(
                的jdbc:mysql的://本地主机/样本,根,根);
        preparedStatement PST =康恩
                。prepareStatement(SELECT * FROM用户信息,其中在名字()?);        的String []参数= {user1的,管理员};
        阵列sqlArray = conn.createArrayOf(VARCHAR,参数);
        pst.setArray(1,sqlArray);
        结果集RS = pst.executeQuery();
        而(rs.next()){
            的System.out.println(rs.getInt(1));
        }
    }赶上(例外五){
        e.printStackTrace();
        }
    }
}


解决方案

有关 Mysql的 -

设置阵列是不可能在MySQL。

相反的,可以形成用于在回路用于设置值的查询(?,?,..)和相同的方式

 的String []参数= {user1的,管理员};
查询字符串=从用户信息,其中姓名中(选择*;
字符串TEMP =;
对于(i = 0; I< Parameter.length;我++)
{
  温度+ =,?;
}
临时= temp.replaceFirst(,,);
温度+ =);
查询=查询+温度;

preparedStatement PST =康恩prepareStatement(查询);

这样的查询变得 - (?,?)SELECT * FROM用户信息,其中姓在

和同样使用循环传递值。

有关甲骨文 -

  ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor(CHAR_ARRAY,康恩);
的String []参数= {user1的,管理员};
了java.sql.Array sqlArray =新oracle.sql.ARRAY(arrayDescriptor,康涅狄格州,内容);


pstmt.setArray(1,sqlArray);

I am facing error java.sql.SQLFeatureNotSupportedException in my prepare statement. I am using Mysql database.

Below is my code.

class tmp {
public static void main(String arg[]) {

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost/sample", "root", "root");
        PreparedStatement pst = conn
                .prepareStatement("select * from userinfo where firstname in(?)");

        String[] Parameter = { "user1", "Administrator" };
        Array sqlArray = conn.createArrayOf("VARCHAR", Parameter);
        pst.setArray(1, sqlArray);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
        }
    }
}

解决方案

For Mysql -

Setting array is not possible in Mysql.

Instead of that you can form a query for (?,?,..) in the loop and same way for setting values.

String[] Parameter = { "user1", "Administrator" };
String query="select * from userinfo where firstname in (";
String temp="";
for(i=0;i<Parameter.length;i++)
{
  temp+=",?";
}
temp=temp.replaceFirst(",","");
temp+=")";
query=query+temp;

PreparedStatement pst = conn.prepareStatement(query);

so query becomes - select * from userinfo where firstname in (?,?)

and pass values also using loop.

For Oracle -

ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn);
String[] Parameter = { "user1", "Administrator" };
java.sql.Array sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content);
.
.
pstmt.setArray(1, sqlArray);

这篇关于传球达阵参数prepare声明 - 让&QUOT; java.sql.SQLFeatureNotSupportedException&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆