想要为所有表格形成一个常规更新查询

时间:2017-09-04 09:31:15

标签: java mysql jdbc

我已经为所有表编写了插入数据库的代码,但我仍然坚持更新查询。 tablename,tablepk_name,tablepk,table_sc_name,table_sc_id是我从另一个方法获取的值,这里是我的代码

try {
        PreparedStatement sv = localConnection.prepareStatement("select * from " + tablename + " where " + tablepk_name + " = '" + tablepk + "'" + " and " + table_sc_name + " = '" + table_sc_id + "'");
        ResultSet rs_local = sv.executeQuery();
        while (rs_local.next()) {
           localConnection.setAutoCommit(false);
            rsmd = rs_local.getMetaData();
            final int columnCount = rsmd.getColumnCount();
            List<List<String>> rowList = new LinkedList<>();
            List<String> columnNames = null;
            String insertColumns = "";
            String insertValues = "";
            columnNames = new ArrayList<>();
            List<String> columnList = new LinkedList<>();
            rowList.add(columnList);
            for (int j = 1; j <= columnCount; j++) {
                columnNames.add(rsmd.getColumnLabel(j));
            }
            if (columnNames != null && columnNames.size() > 0) {
                insertColumns += columnNames.get(0);
                insertValues += "?";
            }
            for (int j = 1; j < columnNames.size(); j++) {
                insertColumns += "='?', " + columnNames.get(j);
                insertValues += ", " + "?";
            }
            SQL = "UPDATE " + tablename + " set (" + insertColumns + ") values(" + insertValues + ") where "+tablepk_name+"= '"+ tablepk + "'";
            PreparedStatement ps = localConnection.prepareStatement(SQL);
            for (int column = 1; column <= columnCount; column++) {
                Object value = rs_local.getObject(column);
                ps.setObject(column, value);
            }
            String psa = ps.toString();
            query = psa.substring(psa.indexOf(": ") + 2);
            System.out.println(" quer " + query);
        }
    } catch (SQLException ex) {
        Logger.getLogger(PosSynchronizationPoll.class.getName()).log(Level.SEVERE, null, ex);
    }

这是我收到的输出

UPDATE products set (id='?', reference='?', code='?', codetype='?', name='?', 
pricebuy='?', pricesell='?', category='?', taxcat='?', attributeset_id='?', 
stockcost='?', stockvolume='?', image='?', iscom='?', isscale='?', isconstant='?',
printkb='?', sendstatus='?', isservice='?', attributes='?', display='?', 
isvprice='?', isverpatrib='?', texttip='?', warranty='?', stockunits='?',
printto='?', supplier='?', uom='?', memodate) values('04352e14-f96d-4301-bc72-f80f2ec19740',
'0012', '0012', 'EAN-13', 'text', 0.0, 8.333333333333334, '48eabc71-a100-48a2-9f11-23b60f86257d',
'001', 'd928c6b9-7a87-4bac-8d8c-ce846498393b', 0.0, 0.0, null, 0, 0, 0, 0,
0, 0, _binary'<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>  \n
<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n<properties>\n </properties>', '', 0, 0, '', 0, 0.0, '1',
'2ef4a3a1-f610-4eb4-9339-e7e19496b918', '0', null) where id= '04352e14-f96d-4301-bc72-f80f2ec19740';

*所需的输出应该

UPDATE products set (id=?, reference=?, code=?, codetype=?, 
name=?, pricebuy=?, pricesell=?, category=?, taxcat=?, 
attributeset_id=?, stockcost=?, stockvolume=?, image=?,
 iscom=?, isscale=?, isconstant=?, printkb=?, sendstatus=?, 
isservice=?, attributes=?, display=?, isvprice=?, isverpatrib=?, 
texttip=?, warranty=?, stockunits=?, printto=?, supplier=?, 
uom=?, memodate=?) values('04352e14-f96d-4301-bc72-f80f2ec19740', 
'0012', '0012', 'EAN-13', 'text', 0.0, 8.333333333333334, '48eabc71-a100-48a2-9f11-23b60f86257d', 
'001', 'd928c6b9-7a87-4bac-8d8c-ce846498393b', 0.0, 0.0, null, 0, 
0, 0, 0, 0, 0, _binary'<?xml version=\"1.0\"
 encoding=\"UTF-8\" standalone=\"no\"?>  \n<!DOCTYPE properties SYSTEM
 \"http://java.sun.com/dtd/properties.dtd\">\n<properties>\n </properties>', '', 0, 0, '',
 0, 0.0, '1', '2ef4a3a1-f610-4eb4-9339-e7e19496b918', '0', null) 
where id= '04352e14-f96d-4301-bc72-f80f2ec19740'*

1 个答案:

答案 0 :(得分:0)

try {
    PreparedStatement sv = localConnection.prepareStatement("select * from " + tablename + " where " + tablepk_name + " = '" + tablepk + "'" + " and " + table_sc_name + " = '" + table_sc_id + "'");
    ResultSet rs_local = sv.executeQuery();
    while (rs_local.next()) {
       localConnection.setAutoCommit(false);
        rsmd = rs_local.getMetaData();
        final int columnCount = rsmd.getColumnCount();
        List<List<String>> rowList = new LinkedList<>();
        List<String> columnNames = null;
        String insertColumns = "";
        String insertValues = "";
        columnNames = new ArrayList<>();
        List<String> columnList = new LinkedList<>();
        rowList.add(columnList);
        for (int j = 1; j <= columnCount; j++) {
            columnNames.add(rsmd.getColumnLabel(j));
        }
        if (columnNames != null && columnNames.size() > 0) {
            insertColumns += columnNames.get(0);
            insertValues += "?";
        }
        for (int j = 1; j < columnNames.size(); j++) {
            insertColumns += "='?', " + columnNames.get(j);
            insertValues += ", " + "?";
        }
        SQL = "UPDATE " + tablename + " set " + insertColumns + " = ? where " + tablepk_name +" = '"+ tablepk + "'";
        PreparedStatement ps = localConnection.prepareStatement(SQL);
        for (int column = 1; column <= columnCount; column++) {
            Object value = rs_local.getObject(column);
            ps.setObject(column, value);
        }
        String psa = ps.toString();
        query = psa.substring(psa.indexOf(": ") + 2);
        System.out.println(" quer " + query);
    }
} catch (SQLException ex) {
    Logger.getLogger(PosSynchronizationPoll.class.getName()).log(Level.SEVERE, null, ex);
}