高分请教oracle的default sysdate 怎么让每次写数据库时自动更新最新时间表和情况如下:当我在WEB页面上修改密码时,入库的时间没有改变,入库的时间是用了Oracle default sysdate,若不用Oracle default sysdate,用什么办法解决最新时间。之前想过用Java的Date,但想到JAVA的日期时间和数据库日期是有区别的。请高手们赐教。感激不尽。谢谢create table t_student
(
id number primary key not null, --主键
name varchar2(20) not null, --姓名
psw varchar2(20) not null, --密码
exchange_time date default sysdate --每次入库时间
);
commit;insert into t_student(id,name,psw) values (1,'csdn','csdn');
select * from t_student;1 csdn csdn 2011-4-11 上午 11:15:26
update t_student set psw='csdn123456' where id=1;
select * from t_student;1 csdn csdn123456 2011-4-11 上午 11:15:26
(
id number primary key not null, --主键
name varchar2(20) not null, --姓名
psw varchar2(20) not null, --密码
exchange_time date default sysdate --每次入库时间
);
commit;insert into t_student(id,name,psw) values (1,'csdn','csdn');
select * from t_student;1 csdn csdn 2011-4-11 上午 11:15:26
update t_student set psw='csdn123456' where id=1;
select * from t_student;1 csdn csdn123456 2011-4-11 上午 11:15:26
把sysdate 写到sql中就行了 不用搞的太复杂
SQL> create table tdate(id int primary key, col2 varchar(32), col3 date default sysdate);表已创建。SQL> insert into tdate (id, col2) values(1, 'wang');已创建 1 行。SQL> alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';会话已更改。SQL> select * from tdate; ID COL2
---------- ----------------------------------------------------------------
COL3
-------------------
1 wang
2011-04-11 11:35:21
SQL> update tdate set col2='wang_modified', col3=sysdate where id=1;已更新 1 行。SQL> select * from tdate; ID COL2
---------- ----------------------------------------------------------------
COL3
-------------------
1 wang_modified
2011-04-11 11:36:49
在开发的过程中是要结合Java代码,单在PLSQL里写SQL语句是没有问题的。PreparedStatement state= conn.prepareStatement("update t_student set psw=?,exchange_time =? where id=?");
state.setString(2,"sysdate"); //当设sysdate的时候,执行时是有问题的
在开发的过程中是要结合Java代码,单在PLSQL里写SQL语句是没有问题的。PreparedStatement state= conn.prepareStatement("update t_student set psw=?,exchange_time =? where id=?");
state.setString(2,"sysdate"); //当设sysdate的时候,执行时是有问题的
直接写到SQL里
这样update t_student set psw=?,exchange_time =sysdate where id=?
个人能力比较菜,没怎么用触发器 。不知有没有像Sequence一样每次都能自动生成更新的办法
10楼的语句你试过吗?把sysdate直接写进sql语句中 而不是以参数的形式传进去 我对java不熟 但是 如果sql没错 java语法不应该成为问题的还有 触发器其实很简单 随便百度一下就会的
before update on tablename
for each row
begin
:NEW.exchange_time := sysdate;
end;
这是最简单的触发器 一行代码就能实现你的功能
--建表
create table t_student(
id number primary key,
name varchar2(20) not null,
psw varchar2(20) not null,
exchange_time date);
--建触发器
create or replace trigger tri_exchange_time
before insert or update
on t_student
for each row
begin
select sysdate into :new.exchange_time from dual;
end;
--测试数据
insert into t_student(id,name,psw)
values(1001,'csdn','csdn');
insert into t_student(id,name,psw)
values(1002,'csdn_home','csdn_home');
--查看数据
select id,name,psw,to_char(exchange_time,'yyyy-mm-dd hh24:mi:ss') exchange_time from t_student;
ID NAME PSW EXCHANGE_TIME
---------- -------------------- -------------------- -------------------
1001 csdn csdn 2011-04-11 15:01:37
1002 csdn_home csdn_home 2011-04-11 15:01:37
--更新数据
update t_student set psw='itlover'
where id=1001;
--
SQL> select id,name,psw,to_char(exchange_time,'yyyy-mm-dd hh24:mi:ss') exchange_time from t_student;
ID NAME PSW EXCHANGE_TIME
---------- -------------------- -------------------- -------------------
1001 csdn itlover 2011-04-11 15:04:52
1002 csdn_home csdn_home 2011-04-11 15:01:37
sysdate不是string变量,当然不能了。
PreparedStatement state= conn.prepareStatement("update t_student set psw=?,exchange_time =sysdate where id=?");
// state.setString(2,"sysdate");state.setString(1, "<newpsw>");
state.setInt(2, <newId>);.......
//在传参数的时候,将你的参数设置为id号和pwd;
//你只需要传入需要修改密码的id号和新pwd就可以了,
//关于修改时间问题,oracle触发器会为你解决的。
PreparedStatement state= conn.prepareStatement("update t_student set psw=? where id=?");
state.setString(2,"new_pwd");
pl/sql 编程不就是干这个用的
有必要 几百张表都有exchange_time吗
不会产生数据冗余吧.
设计很重要的 后面改起来
工作量很大....