/* 2 功能:生成博客目录的JS工具 3 测试:IE8,火狐,google测试通过 6 */ 7 var BlogDirectory = { 8 /* 9 获取元素位置,距浏览器左边界的距离(left)和距浏览器上边界的距离(top) 10 */ 11 getElementPosition:function (ele) { 12 var topPosition = 0; 13 var leftPosition = 0; 14 while (ele){ 15 topPosition += ele.offsetTop; 16 leftPosition += ele.offsetLeft; 17 ele = ele.offsetParent; 18 } 19 return {top:topPosition, left:leftPosition}; 20 }, 21 22 /* 23 获取滚动条当前位置 24 */ 25 getScrollBarPosition:function () { 26 var scrollBarPosition = document.body.scrollTop || document.documentElement.scrollTop; 27 return scrollBarPosition; 28 }, 29 30 /* 31 移动滚动条,finalPos 为目的位置,internal 为移动速度 32 */ 33 moveScrollBar:function(finalpos, interval) { 34 35 //若不支持此方法,则退出 36 if(!window.scrollTo) { 37 return false; 38 } 39 40 //窗体滚动时,禁用鼠标滚轮 41 window.onmousewheel = function(){ 42 return false; 43 }; 44 45 //清除计时 46 if (document.body.movement) { 47 clearTimeout(document.body.movement); 48 } 49 50 var currentpos =BlogDirectory.getScrollBarPosition();//获取滚动条当前位置 51 52 var dist = 0; 53 if (currentpos == finalpos) {//到达预定位置,则解禁鼠标滚轮,并退出 54 window.onmousewheel = function(){ 55 return true; 56 } 57 return true; 58 } 59 if (currentpos < finalpos) {//未到达,则计算下一步所要移动的距离 60 dist = Math.ceil((finalpos - currentpos)/10); 61 currentpos += dist; 62 } 63 if (currentpos > finalpos) { 64 dist = Math.ceil((currentpos - finalpos)/10); 65 currentpos -= dist; 66 } 67 68 var scrTop = BlogDirectory.getScrollBarPosition();//获取滚动条当前位置 69 window.scrollTo(0, currentpos);//移动窗口 70 if(BlogDirectory.getScrollBarPosition() == scrTop)//若已到底部,则解禁鼠标滚轮,并退出 71 { 72 window.onmousewheel = function(){ 73 return true; 74 } 75 return true; 76 } 77 78 //进行下一步移动 79 var repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")"; 80 document.body.movement = setTimeout(repeat, interval); 81 }, 82 83 htmlDecode:function (text){ 84 var temp = document.createElement("div"); 85 temp.innerHTML = text; 86 var output = temp.innerText || temp.textContent; 87 temp = null; 88 return output; 89 }, 90 91 /* 92 创建博客目录, 93 id表示包含博文正文的 div 容器的 id, 94 mt 和 st 分别表示主标题和次级标题的标签名称(如 H2、H3,大写或小写都可以!), 95 interval 表示移动的速度 96 */ 97 createBlogDirectory:function (id, mt, st, interval){ 98 //获取博文正文div容器 99 var elem = document.getElementById(id); 100 if(!elem) return false; 101 //获取div中所有元素结点 102 var nodes = elem.getElementsByTagName("*"); 103 //创建博客目录的div容器 104 var divSideBar = document.createElement('DIV'); 105 divSideBar.className = 'sideBar'; 106 divSideBar.setAttribute('id', 'sideBar'); 107 var divSideBarTab = document.createElement('DIV'); 108 divSideBarTab.setAttribute('id', 'sideBarTab'); 109 divSideBar.appendChild(divSideBarTab); 110 var h2 = document.createElement('H2'); 111 divSideBarTab.appendChild(h2); 112 var txt = document.createTextNode('目录导航'); 113 h2.appendChild(txt); 114 var divSideBarContents = document.createElement('DIV'); 115 divSideBarContents.style.display = 'none'; 116 divSideBarContents.setAttribute('id', 'sideBarContents'); 117 divSideBar.appendChild(divSideBarContents); 118 //创建自定义列表 119 var dlist = document.createElement("dl"); 120 divSideBarContents.appendChild(dlist); 121 var num = 0;//统计找到的mt和st 122 mt = mt.toUpperCase();//转化成大写 123 st = st.toUpperCase();//转化成大写 124 //遍历所有元素结点 125 for(var i=0; i

oracle学习笔记(二十三)——JDBC调用存储过程以及批量操作

jdbc调用存储过程

使用并获得out模式的参数返回值

//存储过程为sum_sal(deptno department.deptno%type,sum in out number)
CallableStatement cs =conn.prepareCall("{call sum_sal(?,?)}"); 
cs.setInteger(1,7879);
cs.setDouble(2,0.0);//第二个传什么都无所谓,因为第二个参数是in out模式,是作为输出的
cs.registerOutParameter(2,java.sql.Types.Double,2);//最后那个参数是保留小数点2位
cs.excute();//执行会返回一个boolean结果

//获得结果,获取第二个参数
double result = cs.getDouble(2);

获得oracle返回的结果集

//存储过程为list(result_set out sys_refcursor, which in number)
CallableStatement cs =conn.prepareCall("{call list(?,?)}"); 
cs.setInteger(2,1);
cs.registerOutParameter(1,racleTypes.CURSOR);
cs.execute();
//获得结果集
ResultSet rs = (ResultSet)cs.getObject(1);

批量操作

批量插入

People表中只有两列,id和name ,还有对应的一个实体类People
批量操作应该放到事务里进行,因为它会存在某条语句执行失败的情况。

public int[] insetBatch(List<People> list) {
    try (Connection connection = JdbcUtil.getConnection();
         PreparedStatement ps = connection.prepareStatement("insert into PEOPLE values (?,?)");
    ) {
        // 关闭事务自动提交,手动提交
        connection.setAutoCommit(false);
        //从list中取出数据
        for (People people : list) {
            ps.setInt(1, people.getId());
            ps.setString(2, people.getName());
            //加入到指语句组中
            ps.addBatch();
        }
        int[] recordsEffect = ps.executeBatch();
        // 提交事务
        connection.commit();
        return recordsEffect;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

批量插入测试

public static void main(String[] args) {
    List<People> list = new ArrayList<>();
    int id = 1;
    list.add(new People(id++, "james"));
    list.add(new People(id++, "andy"));
    list.add(new People(id++, "jack"));
    list.add(new People(id++, "john"));
    list.add(new People(id++, "scott"));
    list.add(new People(id++, "jassica"));
    list.add(new People(id++, "jerry"));
    list.add(new People(id++, "marry"));
    list.add(new People(id++, "alex"));

    int[] ints = new BatchTest().insetBatch(list);

    System.out.println(Arrays.toString(ints));
}

批量更新

public int[] updateBatch(List<People> list) {
    try (Connection connection = JdbcUtil.getConnection();
         PreparedStatement ps = connection.prepareStatement("undate people set name=? where id=?");
    ) {
        // 关闭事务自动提交,手动提交
        connection.setAutoCommit(false);
        //从list中取出数据
        for (People people : list) {
            ps.setInt(1, people.getId());
            ps.setString(2, people.getName());
            //加入到指语句组中
            ps.addBatch();
        }
        int[] recordsEffect = ps.executeBatch();
        // 提交事务
        connection.commit();
        return recordsEffect;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

批量删除

public int[] updateBatch(List<People> list) {
    try (Connection connection = JdbcUtil.getConnection();
         PreparedStatement ps = connection.prepareStatement("delete people where id=?");
    ) {
        // 关闭事务自动提交,手动提交
        connection.setAutoCommit(false);
        //从list中取出数据
        for (People people : list) {
            ps.setInt(1, people.getId());
            //加入到指语句组中
            ps.addBatch();
        }
        int[] recordsEffect = ps.executeBatch();
        // 提交事务
        connection.commit();
        return recordsEffect;

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
posted @ 2019-09-19 14:45  我的人生  阅读(409)  评论(0)    收藏  举报