java实现sql 语句实现同一列上两个值互换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现sql 语句实现同一列上两个值互换相关的知识,希望对你有一定的参考价值。
sql 语句实现同一列上两个值互换
我用两条update语句但是第一个更新以后,值已经不是以前的值,是第二个值,所以第二个不更新
String sql = "update s_student set sushehao=(select sushehao from s_student where studentnum='"+xuehao2+"') where studentnum=?";
String sql1 = "update s_student set sushehao=(select sushehao from s_student where studentnum='"+xuehao1+"') where studentnum=?";
ConnDb db = ConnDb.createInstance();
db.setPs(sql);
db.setString(1,xuehao1);
db.update();
db.setPs(sql1);
db.setString(1,xuehao2);
db.update();
请高手帮忙啊,怎么实现??
String xuehao1 = tfXuehao1.getText();
String xuehao2 = tfXuehao2.getText();
String sushehao1="select sushehao from s_student where studentnum=xuehao1";
String sushehao2="select sushehao from s_student where studentnum=xuehao2";
String sql = "update s_student set sushehao=sushehao2 where studentnum=xuehao1";
String sql1 = "update s_student set sushehao=sushehao1 where studentnum=xuehao2";
ConnDb db = ConnDb.createInstance();
db.setPs(sql);
db.update();
db.setPs(sql1);
db.update();
改成这样了,但是还是不对 错误信息是[SQLServer]列名 'sushehao2' 无效。
如果这样sushehao1 sushehao2相当于select整条语句而不是select语句获得的值吧
select sushehao1 from s_student where studentnum=<xuehao1>
select sushehao2 from s_student where studentnum=<xuehao2>
update s_student set sushehao=<sushehao2> where studentnum=<xuehao1>
update s_student set sushehao=<sushehao1> where studentnum=<xuehao2>
不是要你照搬SQL呢。。你要替换值啊,比如第一条:
string sushehao1="select sushehao from s_student where studentnum= ‘" + xuehao1 + “’”;
要替换的都在<>里面括起来了
另外下面那个不要中间变量的别照做,就(a=a+b=5;)这句,你一条SQL要多长??你不怕麻烦??
UPDATE ..... SET ? = (SELECT A FROM ....) + (SELECT B FROM ....) WHERE ....????您有这精力去做志愿者,也给祖国做点奉献嘛。。。我多余的精力就给了志愿者协会了 参考技术A 看看这样一个简单的代码:
a = b;
b = a;
这样的代码能完成a和b的互换吗?显然不行
正确的方法之一是temp=a;a=b;b=temp,用sql表达是
select sushehao1 from s_student where studentnum=xuehao1
update s_student set sushehao=(select sushehao from s_student where studentnum=xuehao2) where studentnum=xuehao1
update s_student set sushehao=sushehao1 where studentnum=xuehao2
或者是前面同学提供的方法也可以,而且更直观。 参考技术B 这也就是交换两个变量的值,不引用第三变量,完全可以做到
如a=2,b=3
a=a+b=5;
b=a-b=2;
a=a-b=3;
现在用三条update语句就行了,怎么写,应该很简单了吧 参考技术C pstmt.close() ;
con.close() ;放在后面finally()里面
catch(Exception e)
System.out.println(e.getMessage());
return "failed";
finally(){
pstmt.close() ;
con.close() ;
}
java实现sql语句批处理
Statement实现批处理:
优点:能够处理多种不同结构的sql语句
缺点:不能预处理,执行效率较差。对于参数不同的同一条sql语句需要多次调用addBatch()
- package com.itheima.batch;
- import java.sql.Connection;
- import java.sql.Statement;
- import org.junit.Test;
- import com.itheima.util.DBUtil;
- public class StatementBatch {
- /* mysql数据库:
- create database batch;
- use batch;
- create table mybatch(
- id int primary key auto_increment,
- name varchar(50)
- );
- insert into mybatch values (null, ‘1‘);
- insert into mybatch values (null, ‘2‘);
- insert into mybatch values (null, ‘3‘);
- insert into mybatch values (null, ‘4‘);
- */
- @Test
- public void statementBatch() {
- Connection conn = null;
- Statement stat = null;
- try{
- conn = DBUtil.getConn();
- stat = conn.createStatement();
- stat.addBatch("create database batch");
- stat.addBatch("use batch");
- stat.addBatch("create table mybatch( id int primary key auto_increment, name varchar(50) )");
- stat.addBatch("insert into mybatch values (null, ‘1‘)");
- stat.addBatch("insert into mybatch values (null, ‘2‘)");
- stat.addBatch("insert into mybatch values (null, ‘3‘)");
- stat.addBatch("insert into mybatch values (null, ‘4‘)");
- stat.executeBatch();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- DBUtil.close(conn, stat, null);
- }
- }
- }
PreparedStatement实现批处理:
优点:能够预处理,执行效率高;参数不同的同一条sql语句执行简便
缺点:只能批处理参数不同的同一条sql语句
- package com.itheima.batch;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import org.junit.Test;
- import com.itheima.util.DBUtil;
- public class PrepBatch {
- @Test
- public void prepBatch() {
- Connection conn = null;
- PreparedStatement ps = null;
- try{
- conn = DBUtil.getConn();
- ps = conn.prepareStatement("insert into mybatch value(null, ?)");
- for(int i = 0; i < 1000; i++) {
- ps.setString(1, "name" + i);
- ps.addBatch();
- }
- ps.executeBatch();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- DBUtil.close(conn, ps, null);
- }
- }
- }
以上是关于java实现sql 语句实现同一列上两个值互换的主要内容,如果未能解决你的问题,请参考以下文章