将字符数组插入数据库

I want to insert a 2 dimensional array into a DB table. Is there any way to insert these values into DB with a single INSERT statement rather than using multiple INSERT statements? These multiple statements create a tendency for DB connection pool issues and can create a latency in the application.

String[][] a = new String[10][2];
for(int i =0;i<10;i++)
{
        st.executeUpdate("Insert into sap_details  VALUES a[i][0],a[i][1]);
}

What happens here is there are effectively 10 INSERT statements being called for each row. I don't want it to; it should happen with only one INSERT statement.

Is there any way to do that?

#0

Use JDBC Batch Updates? Using prepared statements should also help.

Example

    String[][] a = new String[10][2];
    PreparedStatement pst = con.prepareStatement("INSERT INTO sap_details VALUES (?,?)");
    for (int i = 0; i < 10; i++) {
        pst.setString(1, a[i][0]);
        pst.setString(2, a[i][1]);
        pst.addBatch();
    }
    int[] results = pst.executeBatch();

#1

With MySQL, something like this should do the trick, perfectly fine , Oracle won't like it. This feature is supported by DB2, SQL Server (since version 10.0 - i.e. 2008), PostgreSQL (since version 8.2), MySQL, and H2.

        String[][] a = new String[10][2];    
        StringBuilder sb = new StringBuilder("Insert into sap_details (a,b) VALUES ");               
        for(int i =0;i<a.length;i++){
                sb.append("(\'");
                sb.append(a[i][0]);
                sb.append("\',\'");
                sb.append(a[i][1]);
                sb.append("\')");
                if(i < a.length -1 )
                    sb.append(",");
         }
         st.executeUpdate(sb.toString());

推荐文章

YQL示例查询最多返回260个结果

YQL示例查询最多返回260个结果

推荐文章

如何基于option从php文件动态加载表单

如何基于option从php文件动态加载表单

推荐文章

错误MSB3073:copy/d“C:\ LOANAPP\UX\UserControls\*.css”“C:\ LOANAPP\UX\getquotes\thirdpartyooperator\css”“退出,代码为1”

错误MSB3073:copy/d“C:\ LOANAPP\UX\UserControls\*.css”“C:\ LOANAPP\UX\getquotes\thirdpartyooperator\css”“退出,代码为1”

推荐文章

相对布置,图像视图在右侧,文本视图在图像左侧,一个在另一个下面

相对布置,图像视图在右侧,文本视图在图像左侧,一个在另一个下面

推荐文章

安卓,限制同一按钮多次按下

安卓,限制同一按钮多次按下

推荐文章

用于google.news搜索和“geo”键的YQL文档

用于google.news搜索和“geo”键的YQL文档

推荐文章

Adobe AIR make屏幕截图

Adobe AIR make屏幕截图

推荐文章

git守护程序:“无法在主机(空)端口9418上分配任何侦听套接字”

git守护程序:“无法在主机(空)端口9418上分配任何侦听套接字”

推荐文章

Jquery ui拖放:多次使用draggable

Jquery ui拖放:多次使用draggable

推荐文章

WPF:向画布添加形状

WPF:向画布添加形状

推荐文章

有什么方法可以让Visual Studio 2010停止启动我的MVC项目吗?

有什么方法可以让Visual Studio 2010停止启动我的MVC项目吗?

推荐文章

何时使用DTD与XSD进行验证

何时使用DTD与XSD进行验证

推荐文章

Silverlight退出浏览器而不安装?

Silverlight退出浏览器而不安装?

推荐文章

如何实现购物车

如何实现购物车

推荐文章

有没有办法用Foursquare用户搜索API返回搜索词?

有没有办法用Foursquare用户搜索API返回搜索词?

推荐文章

如何打印long long

如何打印long long