日期:2014-05-19  浏览次数:20705 次

100分哦
问题是这样的:
有两张表,表1字段:期号,红球1,红球2,红球3,红球4,红球5,红球6
  表2字段:期号,[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
  [11],[12],[13],[14],[15],[16],[17],[18],[19],[20],
  [21],[22],[23],[24],[25],[2]6,[27],[28],[29],[30],[31],[32],[33]
表2中除期号外的所有字段的值都是0,
现在我要达到的效果是:获取到表1的值比如 2 , 12 , 13 ,23 , 25 ,28后,然后使表2相对应的值都变为1(原来是0).

请问有什么好方法可以做到?

------解决方案--------------------
你得到表1的值后,根据id把表2的对应的值set为1就行了啊
------解决方案--------------------
双色球啊 你研究研究java容器很容易做到的 hashmap
------解决方案--------------------
这个很简单,你判断表1中字段是否有值,如果有就更新相对应的值为 1
------解决方案--------------------
在写一个update方法
获得id后同时调用这个update方法,把id传过去
------解决方案--------------------
查询了表1每个字段的值,然后根据这些值,去更新查2的相应字段。

Java code
String sql = "select 红球1,红球2,红球3,红球4,红球5,红球6 from table1";

ResultSet rs = stmt.executeQuery(sql);
String col1 = rs.getString("红球1");
.............
String col6 = rs.getString("红球6");

update table2 set col1=1,col2=1,col3=1,col4=1,col5=1,col6=1 where 期号= ?;

------解决方案--------------------
基本上就是 5 楼给出的方法,不过要做字符串连接,直接写是不行的。。。

String no = 111;
String sql = "select 红球1,红球2,红球3,红球4,红球5,红球6 from table1 where 期号="+no;
ResultSet rs = stmt.executeQuery(sql);
String col1 = rs.getString("红球1");
.............
String col6 = rs.getString("红球6");

String update = "update table2 set " + col1 + "=1, " + col2 + "=1, ..." + col6 + "=1 where 期号= " + no;
stmt.execute(update);
------解决方案--------------------
你想进行SQL执行嵌套,呵呵,常见问题;因为第二个SQL一执行,第一个SQL的游标会自动关闭。
方案一:内层SQL用另一个Connection去处理(这句:sql.executeUpdate(updateCondition););
方案二:先将外层rs的所有数据取出,存为List<Map<String, String>> 或其它随意啥。
------解决方案--------------------
探讨
哦,我用Statement创建了两个对象sql,sql1解决了问题,但还是不太清楚有什么冲突?为什么会关闭?

------解决方案--------------------
顺手复制下Statement的API说明:

The object used for executing a static SQL statement and returning the results it produces.

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.