2012年3月5日星期一

MAC address lookup using Java

MAC address lookup using Java
Posted 2007-07-09 in Java.
Code to extract the MAC address of the network card has been in UUID for a long time now. Because some people download UUID for just the MAC address part, here is a short explanation of how that code works and how you can use it in your projects.
First, download UUID and open com.eaio.uuid.UUIDGen. The MAC address extraction is performed in the static initializer.
The code does a little bit of OS sniffing to find a suitable program which is then used to print the MAC address. The output of this program is read in and parsed.
Operating systems
HP-UX
On HP-UX, /usr/sbin/lanscan is started.
Linux/MacOSX/possibly other Unices
The /sbin/ifconfig command is used.
Windows
ipconfig /all is used.
Solaris
On Solaris, the first line of the host name list is used to call /usr/sbin/arp. The code is somewhat equivalent to /usr/sbin/arp `uname -n | head -1`.
Ripping the MAC code
    * Download the latest version of the UUID software and extract the archive.
    * Copy the following source files from the uuid-x.x.x/src/java folder into your project:
          o com/eaio/util/lang/Hex.java
          o com/eaio/uuid/MACAddressParser.java
          o com/eaio/uuid/UUIDGen.java
    * Copy the static initializer up to and including
      if (macAddress != null) {
          if (macAddress.indexOf(':') != -1) {
              clockSeqAndNode |= Hex.parseLong(macAddress);
          }
          else if (macAddress.startsWith("0x")) {
              clockSeqAndNode |= Hex.parseLong(macAddress.substring(2));
          }
      }
      into a new class.
    * Retrieve the MAC address as clockSeqAndNode variable.
That's all.

这几个常见俚语的变迁,还真不知道

    1,"嫁鸡随鸡,嫁狗随狗",原为"嫁乞随乞,嫁叟随叟"意思是一个女人即使嫁给乞丐或者是年龄大的人也要随其生活一辈子。随着时代的变迁,这一俗语转音成鸡成狗了。
  2,"三个臭皮匠,顶个诸葛亮","皮匠"实际上是"裨将"的谐音,"裨将"在古代是指"副将",原意是指三个副将的智慧合起来能顶一个诸葛亮。流传中,人们将"裨将"说成了"皮匠"。
  3,"不见棺材不落泪",本是"不见亲棺不落泪",并不是见了任何棺材都落泪。讹变为"不见棺材不落泪",如果不管谁的棺材,只要见到就落泪,那就有点太莫名其妙了。
  4,"有眼不识金镶玉",本是"有眼不识荆山玉"。荆,指古代楚国;"荆山玉",是玉匠在荆山发现的玉。
  5,"不到黄河心不死",本是"不到乌江心不死"。乌江,项羽他老人家自刎的地方。乌江讹变成黄河,真是让人无从解释了。
  6,"舍不得孩子套不住狼"本是"舍不得鞋子套不住狼"意思是要打到狼,就要不怕跑路,不怕费鞋。不过这个我还能理解点,因为好像四川那边管鞋叫孩子。如果真的拿生的孩子去套狼,也太恐怖啦!
  7,"狗屁不通",这个成语最初是"狗皮不通"。狗的表皮没有汗腺,酷夏,狗要借助舌头来散发体内的燥热。"狗皮不通"就是指狗的身体这个特点,"屁"是污浊都象征,对于文理不通的东西,以屁来喻,也就将就吧!
  8,"王八蛋"这是民间的一句骂人话。实际上,这句话的原来面目是"忘八端"。古代是,"八端"指"孝,悌,忠,信,礼,义,廉,耻",此八端指的是做人之根本,忘记了这"八端"也就是忘了基本的做人根本,可是后来却被讹变成"王八蛋"。

Oracle在Windows下的操作系统认证总结


一、普通用户

1. 在 windows 下创建用户 niu,或将域用户加入本地组。

2. 修改参数文件 os_authent_prefix 默认值“ops$”。

3. 修改注册表,oracle/home0 下添加 OSAUTH_PREFIX_DOMAIN 项,如果使用域,设为 true,否则设为 false;如果不是在域中,并且将其设为 true,域用机器名代替,如果不添加该项,默认值为 true。

4. 修改 SQLNET.ORA 中的 SQLNET.AUTHENTICATION_SERVICES = (NTS) 或将其注释掉,不能设为 NONE。(该项说明使用操作系统认证)

5. 连接到 Oracle,创建相应的用户:

OSAUTH_PREFIX_DOMAIN 为 TRUE 时:

Create User “OPS$DOMAIN_NAME\NIU” IDENTIFIED EXTERNALLY;
注:在使用 DOMAIN_NAME\ 时,因为有特殊符号 \,所以要用双引号包起来,此时双引号中所有字符必须用大写。

OSAUTH_PREFIX_DOMAIN为 FALSE 时:
Create User OPS$niu IDENTIFIED EXTERNALLY;
赋予给用户一些权限,如:
SQL>GRANT CONNECT TO "OPS$DOMAIN_NAME\NIU"(OPS$niu);
Grant succeeded.
SQL>
6. 以 NIU 用户登录操作操作系统
SQL> connect /
connected
SQL> show user;
USER is "<OPS$><DOMAIN_NAME>\niu"
SQL>
7. 通常情况下,数据库用户登录后,即使没有 ALTER USER 的权限,也是可以修改自己密码的,但若是以此认证方式登录数据库,则不可以(未经确认,只是尝试失败得到的结论,但仔细想想,这样要求也合理)


二、超级管理员用户

1. 与普通用户差不多,需要在创建 windows 中创建组 ora_dba(默认情况安装 oracle 的时候已经建立,ORA_<sid>_DBA 只对指明的 sid 有超级管理员权限),并将本地用户或域用户加入该组。(无需再创建 oracle 用户,相当于用 sys 登陆,os_authent_prefix 也就不起作用。)

2. 用该用户登陆 windows
连接 oracle :

SQL> connect / as sysdba
connected
SQL> show user;
USER is "sys"
SQL>


三、几个参数说明

1. 初始化文件中的 os_authent_prefix: 指明 Oracle 帐户对应 OS 帐户的前缀。

2. 注册表中的 OSAUTH_PREFIX_DOMAIN :如果不指明域(或机器名),一定要将其设为 false,通常这是造成认证失败的原因。

3. sqlnet.ora 中 sqlnet.authentication_services=(nts) 否则产生 ora-01004 错误。

4. 初始化文件中 remote_os_authent=true :该参数不一定要设,除非要用到客户端的操作系统认证(没有试验)。

5. 初始化文件中 REMOTE_LOGIN_PASSWORD :只是用于超级管理员用户,设为 NONE 则只使用操作系统认证,EXCLUSIVE 可用非共享的密码文件认证,SHARED 可用共享的密码文件认证(但只能有 sys 用户为超级管理员)。注:操作系统认证优先于密码文件认证。

四、参考:

http://www.itpub.net/showthread.php?s=&threadid=207909
http://www.itpub.net/162971,1.html
http://www.cnoug.org/viewthread.php?tid=15082
http://www.cnoug.org/viewthread.php?tid=27294&sid=DU7tZYea
http://www.cnblogs.com/zyk/archive/2004/11/09/61786.aspx

Oracle merge usage


CREATE OR REPLACE PACKAGE etl AS

    c_inserting CONSTANT PLS_INTEGER := 0;
    c_updating  CONSTANT PLS_INTEGER := 1;

    FUNCTION merge_counter (
             action_in IN PLS_INTEGER DEFAULT c_inserting
             ) RETURN PLS_INTEGER;

    FUNCTION get_merge_update_count RETURN PLS_INTEGER;

    FUNCTION get_merge_update_count (
             merge_count_in IN PLS_INTEGER
             ) RETURN PLS_INTEGER;

    FUNCTION get_merge_insert_count RETURN PLS_INTEGER;

    FUNCTION get_merge_insert_count (
             merge_count_in in PLS_INTEGER
             ) RETURN PLS_INTEGER;

    PROCEDURE reset_counters;

 END etl;
 /

 CREATE OR REPLACE PACKAGE BODY etl AS

    g_update_counter PLS_INTEGER NOT NULL := 0;
    g_insert_counter PLS_INTEGER NOT NULL := 0;

 /*-------------- START OF FUNCTION merge_counter --------------------*/
 FUNCTION merge_counter (
          action_in IN PLS_INTEGER DEFAULT c_inserting
          ) RETURN PLS_INTEGER IS
 BEGIN
    CASE action_in
       WHEN c_updating
          THEN g_update_counter := g_update_counter + 1;
       WHEN c_inserting
          THEN g_insert_counter := g_insert_counter + 1;
       ELSE
          RAISE PROGRAM_ERROR;
    END CASE;
    RETURN 0;
 END merge_counter;

 /*----------- START OF FUNCTION get_merge_update_count V1 ---------------*/
 FUNCTION get_merge_update_count
    RETURN PLS_INTEGER is
 BEGIN
    RETURN g_update_counter;
 END get_merge_update_count;

 /*----------- START OF FUNCTION get_merge_update_count V2 ---------------*/
 FUNCTION get_merge_update_count (
          merge_count_in IN PLS_INTEGER
          ) RETURN PLS_INTEGER IS
 BEGIN
    RETURN NVL( merge_count_in - g_insert_counter, 0 );
 END get_merge_update_count;

 /*----------- START OF FUNCTION get_merge_insert_count V1 ---------------*/
 FUNCTION get_merge_insert_count
    RETURN PLS_INTEGER IS
 BEGIN
    RETURN g_insert_counter;
 END get_merge_insert_count;

 /*----------- START OF FUNCTION get_merge_insert_count V2 ---------------*/
 FUNCTION get_merge_insert_count (
          merge_count_in IN PLS_INTEGER
          ) RETURN PLS_INTEGER IS
 BEGIN
    RETURN NVL( merge_count_in - g_update_counter, 0 );
 END get_merge_insert_count;

 /*-------------- START OF FUNCTION reset_counters --------------------*/
 PROCEDURE reset_counters IS
 BEGIN
    g_update_counter := 0;
    g_insert_counter := 0;
 END reset_counters;

 END etl;
 /






2. 创建一个验证的sql文件:run_mrg.sql
SQL code
set serverout on
set echo on
set pagesize 100
--
-- Run merge...
--
begin


   etl.reset_counters;


   merge into target tgt
      using source src
      on (src.id = tgt.id)
   when matched then
      update
      set value = (case etl.merge_counter(etl.c_updating)
                        when 0
                       then src.value
                    end)
    when not matched then
       insert
          ( tgt.id
          , tgt.value )
       values
          ( case etl.merge_counter(etl.c_inserting)
               when 0
               then src.id
            end
          , src.value );

    /* Use update count... */
    dbms_output.put_line(sql%rowcount || ' rows merged.');
    dbms_output.put_line(etl.get_merge_update_count || ' rows updated.');
    dbms_output.put_line(etl.get_merge_insert_count(sql%rowcount) || ' rows inserted.');

    /* Use insert count... */
    dbms_output.put_line(etl.get_merge_update_count(sql%rowcount) || ' rows updated.');
    dbms_output.put_line(etl.get_merge_insert_count || ' rows inserted.');

 end;
 /






3. 验证上述用法:
create table source ( id int, value varchar2(1) );
create table target ( id int, value varchar2(1) );


insert into source select rownum, substr(object_type,1,1) from user_objects where rownum <= 15;
insert into target select * from source where rownum <= 10;
commit;


运行结果如下:
SQL> insert into source select rownum, substr(object_type,1,1) from user_objects where rownum <= 15;


已创建11行。


SQL> insert into target select * from source where rownum <= 10;


已创建10行。


SQL> commit;


提交完成。
SQL> @E:\MyDocument\notes\oracle_run_merge.sql
SQL> set serverout on
SQL> set echo on
SQL> set pagesize 100
SQL> --
SQL> -- Run merge...
SQL> --
SQL> begin
  2
  3 etl.reset_counters;
  4
  5 merge into target tgt
  6 using source src
  7 on (src.id = tgt.id)
  8 when matched then
  9 update
 10 set value = (case etl.merge_counter(etl.c_updating)
 11 when 0
 12 then src.value
 13 end)
 14 when not matched then
 15 insert
 16 ( tgt.id
 17 , tgt.value )
 18 values
 19 ( case etl.merge_counter(etl.c_inserting)
 20 when 0
 21 then src.id
 22 end
 23 , src.value );
 24
 25 /* Use update count... */
 26 dbms_output.put_line(sql%rowcount || ' rows merged.');
 27 dbms_output.put_line(etl.get_merge_update_count || ' rows updated.');
 28 dbms_output.put_line(etl.get_merge_insert_count(sql%rowcount) || ' rows inserted.');
 29
 30 /* Use insert count... */
 31 dbms_output.put_line(etl.get_merge_update_count(sql%rowcount) || ' rows updated.');
 32 dbms_output.put_line(etl.get_merge_insert_count || ' rows inserted.');
 33
 34 end;
 35 /
22 rows merged.
20 rows updated.
2 rows inserted.
20 rows updated.
2 rows inserted.


PL/SQL 过程已成功完成。


SQL>


上述过程我是从asktom上整理出来的。希望对你有所帮助。
merge本身是不带有updated和inserted的详细信息的。




MySQL分组排名查询

--按某一字段分组取最大(小)值所在行的数据
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
go
--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name       val         memo              
---------- ----------- --------------------
a          3           a3:a的第三个值
b          5           b5b5b5b5b5
*/
--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name       val         memo              
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值
*/
--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name       val         memo              
---------- ----------- --------------------
a          2           a2(a的第二个值)
b          1           b1--b的第一个值
*/
--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name       val         memo              
---------- ----------- --------------------
a          1           a1--a的第一个值
b          5           b5b5b5b5b5
*/
--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name       val         memo              
---------- ----------- --------------------
a          1           a1--a的第一个值
a          2           a2(a的第二个值)
b          1           b1--b的第一个值
b          2           b2b2b2b2
*/
--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name       val         memo              
---------- ----------- --------------------
a          2           a2(a的第二个值)
a          3           a3:a的第三个值
b          4           b4b4
b          5           b5b5b5b5b5
*/
--七,假如整行数据有重复,所有的列都相同。
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    1   a1--a的第一个值
a    3   a3:a的第三个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
go
select * , px = identity(int,1,1) into tmp from tb
select m.name,m.val,m.memo from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) m where px = (select min(px) from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) n where n.name = m.name)
drop table tb,tmp
/*
name       val         memo
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值
(2 行受影响)
*/
--在sql server 2005中可以使用row_number函数,不需要使用临时表。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
go
select m.name,m.val,m.memo from
(
select * , px = row_number() over(order by name , val) from tb
) m where px = (select min(px) from
(
select * , px = row_number() over(order by name , val) from tb
) n where n.name = m.name)
drop table tb
/*
name       val         memo
---------- ----------- --------------------
a          1           a1--a的第一个值
b          1           b1--b的第一个值
(2 行受影响)
*/

Mysql Install by compiling the source code


tar zxvf mysql-5.1.45.tar.gz
cd mysql-5.1.45
./configure --prefix=/var/lib/mysql --without-debug --with-charset=utf8 --with-extra-charsets=all  --with-plugins=all
make
make install
cp support-files/my-medium.cnf /etc/my.cnf
cd /var/lib/mysql/bin
./mysql_install_db --user=mysql
chown -R root .
cd ..
chown -R mysql /var/
chgrp -R mysql .
cd ../mysql/bin/
./mysqld_safe --user=mysql &  ----查看err.log
cd /var/lib/mysql/share/mysql/
cp mysql.server /etc/init.d/mysql
./mysql.server start
./mysqld_safe --defaults-file =/etc/my.cnf
/etc/init.d/mysql restart
cd /var/lib/mysql/bin/
cp mysql /usr/bin/




2.8.1. 源码安装概述
你必须执行的安装MySQL源码分发版的基本命令是:


shell> groupadd mysql
shell> useradd -g mysql mysql
shell> gunzip < mysql-VERSION.tar.gz | tar -xvf -
shell> cd mysql-VERSION
shell> ./configure --prefix=/usr/local/mysql
shell> make
shell> make install
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> cd /usr/local/mysql
shell> bin/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql var
shell> chgrp -R mysql .
shell> bin/mysqld_safe --user=mysql &
如果从一个源码RPM开始,那么执行如下命令:


shell> rpmbuild --rebuild --clean MySQL-VERSION.src.rpm
这样你制作一个可以安装的二进制RPM。


Oracle Ref Cursor 示例


--如这个存储过程,该如何接收外部的一个变量进来?
create or replace procedure pro_show_emp(
       empno_in in scott.emp.empno%type,--在要传进来的参数后面加上一个关键字in
       cv_emp in out sys_refcursor
       )
is
begin
     open cv_emp for
     select * from scott.emp emp
     where emp.empno=empno_in;
     
     exception
     when others then
          dbms_output.put_line(sqlerrm);
end pro_show_emp;
/
SQL>  variable x refcursor;--refcursor:sqlplus下运行
SQL>  exec pro_show_emp(7788,:x);
PL/SQL procedure successfully completed.
SQL>  print x;


     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------------- ---------- ---------- ----------
      7788 SCOTT      ANALYST         7566 19-4月 -87           3100                    20