jsp调用orcal存储过程小例子
create or replace procedure p_test2(i in number) as
begin
insert into t values (i,'x'||to_char(i));
commit;
end;
/
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<html>
<body>
<table>
<tr><td>aaa</td><td>bbb</td></tr>
<%
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@192.168.1.6:1521:db";
Class.forName(driver);
Connection conn = DriverManager.getConnection(strUrl, "scott", "tiger");
String procedure = "{call p_test2 (?) }";
CallableStatement cstmt = conn.prepareCall(procedure);
cstmt.setInt(1,33);
cstmt.executeUpdate();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from t");
while(rs.next())
{
out.println("<tr><td>" + rs.getString(1) + "</td><td>" + rs.getString(2) + "</td></tr>");
}
rs.close();
stmt.close();
conn.close();
%>
</table>
<p>
<%
out.print(conn.isClosed());
%>
</body>
</html>
</body>
</html>
转自:伊图网[www.4ico.com]
54,Bean调用存储过程
建立一个登录系统,要求达到以下目的。
1、用户通过Bean来认证以及得到得到用户信息。
2、记录用户登录信息,如用户登录次数,最后登录时间。
3、记录操作日志。
未解决及疑惑的问题:
1、用户登录后的Session是否可以通过Bean来判断。
2、通过Bean调用ORACLE存储过程,返回select后的记录集。
操作步骤:
1、建立用户验证Bean:
public boolean checkUser() throws Exception {
boolean flag=false;
ResultSet rs=conn.executeQuery(getSql());
if(rs.next()){
userID =rs.getString("userID");
userName =rs.getString("userName");
userPWD =rs.getString("userPWD");
userUnit =rs.getString("userUnit");
userLoadTime =rs.getDate("userLoadTime");
userLoadNumeric=rs.getInt("userLoadNumber");
flag=true;
}
rs.close();
conn.closeConn();
return flag;
}
通过返回的值判定用户是否存在。
2、记录用户登录信息:
public void changeLoginInfo(String userID) throws Exception{
String sql="update SystemUserTable set UserLoadTime=sysdate,UserLoadNumber=UserLoadNumber+1 where userID='"+userID+"'";
conn.executeUpdate(sql);
}
3、记录操作日志:
第一步,建立存储过程
create or replace procedure proc_writeNote(
description in varchar2,
wName in varchar2,
wIP in varchar2
)
is
begin
insert into Systemnote (Id,Description,Wname,Wip) values(Autoaddid.Nextval,description,wName,wIP);
commit;
end proc_writeNote;
第二步、建立操作存储过程的方法(重写prepareCall()方法)
public CallableStatement prepareCall(String produce){
try {
conn = DriverManager.getConnection(DBUrl, UserID, UserPWD);
cstmt=conn.prepareCall(produce);
}
catch (SQLException ex) {
System.err.print("prepareCall():"+ex.getMessage());
}
return cstmt;
}
第三步,执行存储过程
public void writeNote(String description,String wName,String wIP){
String sql="{call proc_writeNote(?,?,?)}";
try {
CallableStatement cstmt=conn.prepareCall(sql);
cstmt.setString(1, description);
cstmt.setString(2,wName);
cstmt.setString(3,wIP);
cstmt.executeUpdate();
}
catch (SQLException ex) {
System.out.print("writeNote():"+ex.getMessage());
}
}
456,日期数据操作
JAVA中的时间操作leshui(原作)
经常看见jsp版里有人问时间操作的问题,这些问题一般包括:取当前时间,把一个指定的字符串时间转化成时间类型,求两个时间之间的天数,求一段时间以前的时间,求一段时间以后的时间,在这里就把这些问题汇总一下。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%
//字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可)
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M/dd/yyyy hh:mm:ss a",java.util.Locale.US);
java.util.Date d = sdf.parse("5/13/2003 10:31:37 AM");
out.println(d);
out.println("<br>");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime1=formatter.format(d);
out.println(mDateTime1);
out.println("<br>");
out.println(d.getTime());
out.println("<br>");
//当前时间
Calendar cal = Calendar.getInstance();
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss G E D F w W a E F");
String mDateTime=formatter.format(cal.getTime());
out.println(mDateTime);
out.println("<br>");
//1年前日期
java.util.Date myDate=new java.util.Date();
long myTime=(myDate.getTime()/1000)-60*60*24*365;
myDate.setTime(myTime*1000);
String mDate=formatter.format(myDate);
out.println(mDate);
out.println("<br>");
//明天日期
myDate=new java.util.Date();
myTime=(myDate.getTime()/1000)+60*60*24;
myDate.setTime(myTime*1000);
mDate=formatter.format(myDate);
out.println(mDate);
out.println("<br>");
//两个时间之间的天数
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date= myFormatter.parse("2003-05-1");
java.util.Date mydate= myFormatter.parse("1899-12-30");
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
out.println(day);
out.println("<br>");
//加半小时
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date date1 = format.parse("2002-02-28 23:16:00");
long Time=(date1.getTime()/1000)+60*30;
date1.setTime(Time*1000);
String mydate1=formatter.format(date1);
out.println(mydate1);
out.println("<br>");
//年月周求日期
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");
java.util.Date date2= formatter2.parse("2003-05 5 星期五");
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");
String mydate2=formatter3.format(date2);
out.println(mydate2);
out.println("<br>");
//求是星期几
mydate= myFormatter.parse("2001-1-1");
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
String mydate3=formatter4.format(mydate);
out.println(mydate3);
out.println("<br>");
%>
457,数据库函数
SQLServer和Oracle是大家经常用到的数据库,在此感谢作者总结出这些常用函数以供大家参考。
数学函数
1.绝对值
S:select abs(-1) value
O:select abs(-1) value from dual
2.取整(大)
S:select ceiling(-1.001) value
O:select ceil(-1.001) value from dual
3.取整(小)
S:select floor(-1.001) value
O:select floor(-1.001) value from dual
4.取整(截取)
S:select cast(-1.002 as int) value
O:select trunc(-1.002) value from dual
5.四舍五入
S:select round(1.23456,4) value 1.23460
O:select round(1.23456,4) value from dual 1.2346
6.e为底的幂
S:select Exp(1) value 2.7182818284590451
O:select Exp(1) value from dual 2.71828182
7.取e为底的对数
S:select log(2.7182818284590451) value 1
O:select ln(2.7182818284590451) value from dual; 1
8.取10为底对数
S:select log10(10) value 1
O:select log(10,10) value from dual; 1
9.取平方
S:select SQUARE(4) value 16
O:select power(4,2) value from dual 16
10.取平方根
S:select SQRT(4) value 2
O:select SQRT(4) value from dual 2
11.求任意数为底的幂
S:select power(3,4) value 81
O:select power(3,4) value from dual 81
12.取随机数
S:select rand() value
O:select sys.dbms_random.value(0,1) value from dual;
13.取符号
S:select sign(-8) value -1
O:select sign(-8) value from dual -1
14.圆周率
S:SELECT PI() value 3.1415926535897931
O:不知道
15.sin,cos,tan 参数都以弧度为单位
例如:select sin(PI()/2) value 得到1(SQLServer)
16.Asin,Acos,Atan,Atan2 返回弧度
17.弧度角度互换(SQLServer,Oracle不知道)
DEGREES:弧度-〉角度
RADIANS:角度-〉弧度
数值间比较
18. 求集合最大值
S:select max(value) value from
(select 1 value
union
select -2 value
union
select 4 value
union
select 3 value)a
O:select greatest(1,-2,4,3) value from dual
19. 求集合最小值
S:select min(value) value from
(select 1 value
union
select -2 value
union
select 4 value
union
select 3 value)a
O:select least(1,-2,4,3) value from dual
20.如何处理null值(F2中的null以10代替)
S:select F1,IsNull(F2,10) value from Tbl
O:select F1,nvl(F2,10) value from Tbl
21.求字符序号
S:select ascii('a') value
O:select ascii('a') value from dual
22.从序号求字符
S:select char(97) value
O:select chr(97) value from dual
23.连接
S:select '11'+'22'+'33' value
O:select CONCAT('11','22') 33 value from dual
23.子串位置 --返回3
S:select CHARINDEX('s','sdsq',2) value
O:select INSTR('sdsq','s',2) value from dual
23.模糊子串的位置 --返回2,参数去掉中间%则返回7
S:select patindex('%d%q%','sdsfasdqe') value
O:oracle没发现,但是instr可以通过第四个参数控制出现次数
select INSTR('sdsfasdqe','sd',1,2) value from dual 返回6
24.求子串
S:select substring('abcd',2,2) value
O:select substr('abcd',2,2) value from dual
25.子串代替 返回aijklmnef
S:SELECT STUFF('abcdef', 2, 3, 'ijklmn') value
O:SELECT Replace('abcdef', 'bcd', 'ijklmn') value from dual
26.子串全部替换
S:没发现
O:select Translate('fasdbfasegas','fa','我' ) value from dual
27.长度
S:len,datalength
O:length
28.大小写转换 lower,upper
29.单词首字母大写
S:没发现
O:select INITCAP('abcd dsaf df') value from dual
30.左补空格(LPAD的第一个参数为空格则同space函数)
S:select space(10)+'abcd' value
O:select LPAD('abcd',14) value from dual
31.右补空格(RPAD的第一个参数为空格则同space函数)
S:select 'abcd'+space(10) value
O:select RPAD('abcd',14) value from dual
32.删除空格
S:ltrim,rtrim
O:ltrim,rtrim,trim
33. 重复字符串
S:select REPLICATE('abcd',2) value
O:没发现
34.发音相似性比较(这两个单词返回值一样,发音相同)
S:SELECT SOUNDEX ('Smith'), SOUNDEX ('Smythe')
O:SELECT SOUNDEX ('Smith'), SOUNDEX ('Smythe') from dual
SQLServer中用SELECT DIFFERENCE('Smithers', 'Smythers') 比较soundex的差
返回0-4,4为同音,1最高
日期函数
35.系统时间
S:select getdate() value
O:select sysdate value from dual
36.前后几日
直接与整数相加减
37.求日期
S:select convert(char(10),getdate(),20) value
O:select trunc(sysdate) value from dual
select to_char(sysdate,'yyyy-mm-dd') value from dual
38.求时间
S:select convert(char(8),getdate(),108) value
O:select to_char(sysdate,'hh24:mm:ss') value from dual
39.取日期时间的其他部分
S:DATEPART 和 DATENAME 函数 (第一个参数决定)
O:to_char函数 第二个参数决定
参数---------------------------------下表需要补充
year yy, yyyy
quarter qq, q (季度)
month mm, m (m O无效)
dayofyear dy, y (O表星期)
day dd, d (d O无效)
week wk, ww (wk O无效)
weekday dw (O不清楚)
Hour hh,hh12,hh24 (hh12,hh24 S无效)
minute mi, n (n O无效)
second ss, s (s O无效)
millisecond ms (O无效)
----------------------------------------------
40.当月最后一天
S:不知道
O:select LAST_DAY(sysdate) value from dual
41.本星期的某一天(比如星期日)
S:不知道
O:SELECT Next_day(sysdate,7) vaule FROM DUAL;
42.字符串转时间
S:可以直接转或者select cast('2004-09-08'as datetime) value
O:SELECT To_date('2004-01-05 22:09:38','yyyy-mm-dd hh24-mi-ss') vaule FROM DUAL;
43.求两日期某一部分的差(比如秒)
S:select datediff(ss,getdate(),getdate()+12.3) value
O:直接用两个日期相减(比如d1-d2=12.3)
SELECT (d1-d2)*24*60*60 vaule FROM DUAL;
44.根据差值求新的日期(比如分钟)
S:select dateadd(mi,8,getdate()) value
O:SELECT sysdate+8/60/24 vaule FROM DUAL;
45.求不同时区时间
S:不知道
O:SELECT New_time(sysdate,'ydt','gmt' ) vaule FROM DUAL;
-----时区参数,北京在东8区应该是Ydt-------
AST ADT 大西洋标准时间
BST BDT 白令海标准时间
CST CDT 中部标准时间
EST EDT 东部标准时间
GMT 格林尼治标准时间
HST HDT 阿拉斯加?夏威夷标准时间
MST MDT 山区标准时间
NST 纽芬兰标准时间
PST PDT 太平洋标准时间
YST YDT YUKON标准时间
879,存储过程
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydata","sa","sa");
Statement stm=con.createStatement();
ResultSet rs=null;
CallableStatement call=con.prepareCall("{call sel(1)}");
rs=call.executeQuery();
Connection con2=DriverManager.getConnection("jdbc:odbc:mydata","sa","sa");
Statement stm2=con2.createStatement();
CallableStatement call2=con2.prepareCall("{call inser(8)}");
call2.executeUpdate();
%>
34,java直接连接access数据库
try
{
String dburl ="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=..\\data\\UserInf.mdb";//注意空格
con=DriverManager.getConnection(dburl,"这里填用户名","这里填密码");
//如果没有设数据库管理的用户名和密码,为空就行了.
//还有上面dbq=后面填写的是数据库所在的位置,这里我是用相对地址,数据库是UserInf.mdb
}
catch(SQLException e2){}
jsp基本用法和命令汇总
[JSP]2016年10月3日ASP编码教程:如何实现/使用缓存
[ASP]2015年4月15日ASP编码教程:asp缓存的分类
[ASP]2015年4月15日ASP编码教程:何谓ASP缓存/为什么要缓存
[ASP]2015年4月15日ASP编码教程:asp实现的sha1加密解密代码
[ASP]2015年4月15日