java - 想要为所有表形成一个通用更新查询

标签 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'*

最佳答案

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);
}

关于java - 想要为所有表形成一个通用更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46034157/

相关文章:

java - 如何从执行插入操作的 DB2 存储过程将生成的键返回到 JDBC?

Java - 固定数量任务的ExecutorService

php - mysql 通过描述查找,其中描述等于 id 号,而不使用 id 列

php - 如何通过单击按钮来更新信息?

java - 如何使用 JNI 将 C ODBC 连接公开到 JVM?

java - 我应该把 JDBC 驱动程序放在哪里?

java - 如何在 Tor 中使用 HtmlUnit?

java - 有关如何使用 Jasper Reports JRPPTxExpoter 将 PDF 导出到 PowerPoint 的示例

java - 我怎样才能快速 ((A^z1 * y^z2) mod P) mod Q

mysql语句没有返回正确的结果