收获每一天

-----在JAVA的路上越走越远!
posts - 6, comments - 0, trackbacks - 0, articles - 2
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2006年5月4日

一年又一年,又一年过去了!

posted @ 2007-02-17 23:04 喜悦天天收获 阅读(86) | 评论 (0)编辑 收藏

数据库要求
1、建立一个表code_dam,包含以下字段
   id int,自增,主键
   code nvarchar(20), not null  (大坝编号)
   name nvarchar(20), not null  (大坝名称)
   remark nvarchar(200)         (备注)
2、编程实现对表code_dam的增删改查功能,包括:
   添加大坝编码(添加一条记录到数据库,要求:大坝编号不能重复,大坝名称不能重复)
   修改大坝编码(要求:大坝编号不能重复,大坝名称不能重复)
   删除一个大坝编码
   大坝编码列表(code_dam表中所有的记录,使用列表将其他功能串在一起)
   查看大坝编码(显示指定id的大坝编号、名称、备注)



//建立数据库

-- Create table
create table CODE_DAM
(
  ID     INTEGER not null,
  CODE   VARCHAR2(20) not null,
  NAME   VARCHAR2(20) not null,
  REMARK VARCHAR2(200)
)
tablespace DAM
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table
comment on table CODE_DAM
  is '大坝管理';
-- Add comments to the columns
comment on column CODE_DAM.CODE
  is '大坝编号';
comment on column CODE_DAM.NAME
  is '大坝名称';
-- Create/Recreate primary, unique and foreign key constraints
alter table CODE_DAM
  add constraint ID primary key (ID)
  using index
  tablespace DAM
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

//自增解决办法
//序列
-- Create sequence
create sequence SEQ_CODE_DAM
minvalue 1
maxvalue 999999999999999999999999999
start with 31
increment by 1
cache 10;
//触发器
CREATE OR REPLACE TRIGGER TRG_code_dam
BEFORE
INSERT  ON code_dam
FOR EACH ROW
begin
SELECT SEQ_code_dam.NEXTVAL
INTO:NEW.id
FROM DUAL;
End TRG_code_dam;

 

//相关存储过程

//查询所有数据
//包
create or replace package showall is
type mycursor is ref cursor;
procedure GetRecords(ret_cursor out mycursor);
end showall;
//包体
create or replace package body showall is
procedure GetRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from code_dam;
end GetRecords;
end showall;


//删除数据
create or replace procedure delRecord(id_in in int)
is
begin
delete from code_dam where id=id_in;
commit;
end;

//插入数据
create or replace procedure insertRecord(code_in in varchar2,name_in in varchar2,remark_in in varchar2) is
begin
insert into code_dam (code,name,remark) values (code_in,name_in,remark_in);
commit;
end;


//更新数据
create or replace procedure updataRecord(id_in in int,code_in in varchar2,name_in in varchar2,remark_in in varchar2)
is
begin
update code_dam set code=code_in,name=name_in,remark=remark_in where id=id_in;
commit;
end;


//检查重复
create or replace procedure checkdam
(code_in in varchar2,name_in in varchar,name_out out varchar2)
is
begin
select name into name_out from code_dam where code = code_in or name = name_in;
end checkdam;


//查询单个数据
//包
create or replace package showdam is
type mycursor is ref cursor;
procedure GetRecords(id_in in int,ret_cursor out mycursor);
end showdam;
//包体
create or replace package body showdam is
procedure GetRecords(id_in in int,ret_cursor out mycursor) as
begin
open ret_cursor for select * from code_dam where id = id_in;
end GetRecords;
end showdam;

posted @ 2006-05-04 21:51 喜悦天天收获 阅读(1292) | 评论 (0)编辑 收藏

系统环境:
1、操作系统:Windows 2000
2、数据库: Oracle 8i R2 (8.1.6) for NT 企业版
3、安装路径:C:\ORACLE

说明:

默认安装完Oracle后,初学者应该了解的一些SQL语句

1、连接
SQL*Plus system/manager

2、显示当前连接用户
SQL> show user

3、查看系统拥有哪些用户
SQL> select * from all_users;

4、新建用户并授权
SQL> create user a identified by a;(默认建在SYSTEM表空间下)
SQL> grant connect,resource to a;

5、连接到新用户
SQL> conn a/a

6、查询当前用户下所有对象
SQL> select * from tab;

7、建立第一个表
SQL> create table a(a number);

8、查询表结构
SQL> desc a

9、插入新记录
SQL> insert into a values(1);

10、查询记录
SQL> select * from a;

11、更改记录
SQL> update a set a=2;

12、删除记录
SQL> delete from a;

13、回滚
SQL> roll;
SQL> rollback;

14、提交
SQL> commit;


 
软件环境:
1、Windows 98 第二版
2、Oracle数据库版本为:Personal Oracle7 Release 7.3.4.0.0
3、Oracle安装路径为:C:\ORAWIN95

命令列表:
假设当前执行命令为:select * from tab;

(a)ppend     添加文本到缓冲区当前行尾    a  order by tname 结果:select * from tab order by tname;
                                      (注:a后面跟2个空格)
(c)hange/old/new 在当前行用新的文本替换旧的文本 c/*/tname     结果:select tname from tab;
(c)hange/text  从当前行删除文本        c/tab       结果:select tname from ;
del       删除当前行
del n      删除第n行
(i)nput 文本   在当前行之后添加一行
(l)ist      显示缓冲区中所有行
(l)ist n     显示缓冲区中第 n 行
(l)ist m n    显示缓冲区中 m 到 n 行
run       执行当前缓冲区的命令
/        执行当前缓冲区的命令
r        执行当前缓冲区的命令
@文件名     运行调入内存的sql文件,如:

SQL> edit s<回车>
如果当前目录下不存在s.sql文件,则系统自动生成s.sql文件,
在其中输入“select * from tab;”,存盘退出。

SQL> @s<回车>
系统会自动查询当前用户下的所有表、视图、同义词。

@@文件名     在.sql文件中调用令一个.sql文件时使用

save 文件名   将缓冲区的命令以文件方式存盘,缺省文件扩展名为.sql
get 文件名    调入存盘的sql文件
start 文件名   运行调入内存的sql文件

spool 文件名   把这之后的各种操作及执行结果“假脱机”即存盘到磁盘文件上,默认文件扩展名为.lst
spool      显示当前的“假脱机”状态
spool off    停止输出

例:
SQL> spool a
SQL> spool
正假脱机到 A.LST
SQL> spool off
SQL> spool
当前无假脱机


exit       退出SQL*PLUS
desc 表名    显示表的结构
show user    显示当前连接用户
show error    显示错误
show all     显示所有68个系统变量值
edit       打开默认编辑器,Windows系统中默认是notepad.exe,把缓冲区中最后一条SQL语句调入afiedt.buf文件中进行编辑
edit 文件名   把当前目录中指定的.sql文件调入编辑器进行编辑

clear screen   清空当前屏幕显示

posted @ 2006-05-04 21:45 喜悦天天收获 阅读(465) | 评论 (0)编辑 收藏

Java连接数据库实例


此文中的代码主要列出连接数据库的关键代码,其他访问数据库代码省略

1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);

2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

3、Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

4、Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);

5、Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);

6、MySQL数据库
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);

7、PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB为数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);

posted @ 2006-05-04 17:50 喜悦天天收获 阅读(222) | 评论 (0)编辑 收藏