收獲每一天

          -----在JAVA的路上越走越遠!
          posts - 6, comments - 0, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          2006年5月4日

          一年又一年,又一年過去了!

          posted @ 2007-02-17 23:04 喜悅天天收獲 閱讀(95) | 評論 (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 喜悅天天收獲 閱讀(1302) | 評論 (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 喜悅天天收獲 閱讀(478) | 評論 (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 喜悅天天收獲 閱讀(234) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 三原县| 通辽市| 六盘水市| 宣城市| 石景山区| 紫金县| 马鞍山市| 韩城市| 博客| 齐河县| 焦作市| 分宜县| 丰城市| 南丰县| 永康市| 增城市| 建始县| 遂平县| 英山县| 八宿县| 常德市| 和平区| 平塘县| 福安市| 枣强县| 革吉县| 延寿县| 西藏| 宜黄县| 建水县| 建昌县| 连州市| 星座| 泗阳县| 凤翔县| 天峻县| 绥芬河市| 广汉市| 浪卡子县| 丹阳市| 辽中县|