簡體   English   中英

ExecuteBatch()沒有更新

[英]ExecuteBatch() is not updating

所以我試圖在單個更新中插入多行。 在進行了一些谷歌搜索之后,rewriteBatch是大多數人建議的代碼。

基本上,當我嘗試執行批處理時,它不會更新我的數據庫。 數據庫是本地主機,我可以使用相同的URL從數據庫讀取數據,並且沒有任何異常(Exception或SQLException)。

public void insert(Node[] nodes) {  
    try { 
        conn = (Connection) DriverManager.getConnection(url);
        System.out.println(conn.getMetaData().supportsBatchUpdates());
        conn.setAutoCommit(false);
        for (int i = 0; i < nodes.length; i++) {
            if(nodes[i].next!=null){
       pstmt=conn.prepareStatement(StatementUtils.createInsertStatement(i));
                addToBatch(nodes[i].next);
                int [] res=pstmt.executeBatch();
                System.out.print("has "+res.length+" elements. Status:[");
                for (int j = 0; j < res.length; j++) {
                    System.out.print(res[j]+" ");
                }
                System.out.println("]");
                conn.commit();
                pstmt.close();
            }
        }
    }
    catch(Exception e){
        try {
            conn.rollback();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    finally { 
        if (conn != null) {
        try { conn.close(); }
        catch (SQLException e) {
            printSQLException(e); 
            } 
        }
    }
}

private void addToBatch(Node n) throws SQLException{
    if(n!=null){
        pstmt.setString(1, n.can.getCar());
        pstmt.setInt(2, n.can.getID());
        pstmt.setTimestamp(3, n.can.getDate());
        pstmt.setInt(4, n.can.getLength());
        for (int i = 1; i <= n.can.getLength(); i++) {
            pstmt.setInt(i+4, n.can.getData(i-1));
        }
        pstmt.addBatch();
        addToBatch(n.next);
    }
}

public static String createInsertStatement(int length){
        String s="INSERT into CanData (carName,systemID,dataDate,size";
        for (int i = 0; i < length; i++) 
                s+=",d"+i;
        s+= ")values(?,?,?,?";
        for (int i = 0; i <length; i++)
                s+=",?";
        s+=")";
        return s;
    }

即使insert()上有一個for,出於測試目的,現在節點數組僅具有1個位置。

這也是我的輸出:

true
has 18 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]
true
has 14 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]
true
has 15 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]
true
has 18 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]
true
has 14 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]
true
has 11 elements. Status:[1 1 1 1 1 1 1 1 1 1 1 ]

為什么不更新/插入我的數據庫?

順便說一句,如果你讀到這里直到這里,謝謝你的耐心

首先,感謝所有嘗試幫助我並花費時間進行搜索並嘗試回答我的問題的人。

問題是我的URL連接。 它是:

url="jdbc:sqlserver://localhost;rewriteBatchUpdates=true;instanceName=MSSQLSERVER;integratedSecurity=true;";

但是缺少數據庫名稱和端口號。 似乎當您嘗試同時插入多行時,您需要定義數據庫和端口號,如下所示:

url = "jdbc:sqlserver://localhost:1433;database=IFS_DB;rewriteBatchUpdates=true;instanceName=MSSQLSERVER;integratedSecurity=true;";

我也很抱歉沒有發布該URL。 如果我這樣做的話,這可能早已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM