Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, ahmet.

Asked: January 06, 2009 - 9:23 am UTC

Last updated: October 12, 2022 - 7:47 am UTC

Version: 10.2.0.1.0

Viewed 10K+ times! This question is

You Asked

i use database, Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
and
i found a java source that gives the mac address of the host,
so i wrote it in oracles objects (below from the question)

when i write it in eclipse it works
but when i write it in oracle developer it doest work it gives error,

"
Error: cannot resolve symbol
Line: 20
Text: byte[] mac = ni.getHardwareAddress();
"

question is;
how can i correct it in oracle's java ? or how it works ?
thanks


JAVA class in oracle:
----------------------------
create or replace and compile java source named macaddres as
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MACADDRES
{

public static String mac()
{
try {
InetAddress address = InetAddress.getLocalHost();

/*
* Get NetworkInterface for the current host and then read the
* hardware address.
*/
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();

/*
* Extract each array of mac address and convert it to hexa with the
* following format 08-00-27-DC-4A-9E.
*/
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");

}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
----------------------------

and Tom said...

Rating

  (6 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Get Mac adress in datbabase

emaduldeen, June 18, 2022 - 7:38 pm UTC

i make a class and in working fine in eclipse

--------

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;


public class MACAddress {
public static void main(String[] args) throws Exception
{
// instantiate the MACAddress class
MACAddress obj = new MACAddress();

// call the getMAC() method on the current object
// passing the localhost address as the parameter
//obj.getMAC();


System.out.println( "getMacWindow2="+getMacWindow2() );

System.out.println( "getMacWindow="+getMacWindow() );

System.out.println( "getMac="+getMAC() );

}

// method to get the MAC addresses of the
// localhost machine
public static String getMAC ()
{
String stMac = "adress is :";
try {

// create an Enumeration of type
// NetworkInterface and store the values
// returned by
// NetworkInterface.getNetworkInterfaces()
// method
Enumeration<NetworkInterface> networks
= NetworkInterface.getNetworkInterfaces();

// for every network in the networks Enumeration
while (networks.hasMoreElements()) {
NetworkInterface network
= networks.nextElement();

// call getHardwareAddress() method on each
// network and store the returned value in a
// byte array
byte[] mac = network.getHardwareAddress();

if (mac != null) {


// convert the obtained byte array into
// a printable String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format(
"%02X%s", mac[i],
(i < mac.length - 1) ? "-"
: ""));
}

// print the final String containing the
// MAC Address

//System.out.print(sb.toString()+",");
stMac = stMac+ sb.toString()+"," ;
}

}
}
catch (SocketException e) {
e.printStackTrace();
}
return stMac;
}

public static String getMacWindow() throws SocketException, UnknownHostException
{
// create a variable of type NetworkInterface and
// assign it with the value returned by the
// getByInetAddress() method
String machinMacAdress = "";
InetAddress addr = InetAddress.getLocalHost();

// instantiate the MACAddress class
//MACAddress obj = new MACAddress();
//System.out.print("MAC Address of the system : ");

// call the getMAC() method on the current object
// passing the localhost address as the parameter


NetworkInterface iface
= NetworkInterface.getByInetAddress(addr);

// create a byte array and store the value returned
// by the NetworkInterface.getHardwareAddress()
// method
byte[] mac = iface.getHardwareAddress();

// convert the obtained byte array into a printable
// String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format(
"%02X%s", mac[i],
(i < mac.length - 1) ? "-" : ""));
}

// print the final String containing the MAC Address
//System.out.println(sb.toString());
machinMacAdress = sb.toString() ;
return machinMacAdress;
}

public static String getMacWindow2() {
InetAddress ip;
String myMacAdress = null ;
try {
ip = InetAddress.getLocalHost();
System.out.println("IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
myMacAdress=sb.toString() ;

} catch (Exception e) {
e.printStackTrace();
}
return myMacAdress;
}
}

---------

import it in database
and make package


--------

CREATE OR REPLACE PACKAGE SYS.MACADDRESS AS

PROCEDURE main(Param1 VARCHAR2)
AS
LANGUAGE java
NAME 'MACAddress.main(java.lang.String[])';

FUNCTION getMAC
return VARCHAR2
AS
LANGUAGE java
NAME 'MACAddress.getMAC() return java.lang.String';

FUNCTION getMacWindow
return VARCHAR2
AS
LANGUAGE java
NAME 'MACAddress.getMacWindow() return java.lang.String';

FUNCTION getMacWindow2
return VARCHAR2
AS
LANGUAGE java
NAME 'MACAddress.getMacWindow2() return java.lang.String';

FUNCTION getClass
return VARCHAR2
AS
LANGUAGE java
NAME 'MACAddress.getClass() return java.lang.Class';

FUNCTION hashCode
return NUMBER
AS
LANGUAGE java
NAME 'MACAddress.hashCode() return int';

FUNCTION equals(Param1 VARCHAR2)
return VARCHAR2
AS
LANGUAGE java
NAME 'MACAddress.equals(java.lang.Object) return boolean';

FUNCTION toString
return VARCHAR2
AS
LANGUAGE java
NAME 'MACAddress.toString() return java.lang.String';

PROCEDURE notify
AS
LANGUAGE java
NAME 'MACAddress.notify()';

PROCEDURE notifyAll
AS
LANGUAGE java
NAME 'MACAddress.notifyAll()';

PROCEDURE wait(Param1 NUMBER)
AS
LANGUAGE java
NAME 'MACAddress.wait(long)';

PROCEDURE wait(Param1 NUMBER, Param2 NUMBER)
AS
LANGUAGE java
NAME 'MACAddress.wait(long, int)';

PROCEDURE wait
AS
LANGUAGE java
NAME 'MACAddress.wait()';

end;
/


and when i use database

select MACADDRESS.getMacwindow2
from dual

return null



Connor McDonald
July 05, 2022 - 12:19 am UTC

Skip the java ... just use an an external table. Example from Windows


getmac.cmd
===========
@echo off
set SystemRoot=C:\WINDOWS
ipconfig /all | grep Physical | gawk -F: "{print $2}"


SQL> create table addr
  2      (
  3       mac varchar2(100)
  4     )
  5      organization external
  6      (  type oracle_loader
  7         default directory temp
  8         access parameters
  9         ( records delimited by newline
 10           preprocessor  temp:'getmac.cmd'
 11        )
 12        location ( temp:'getmac.cmd' )
 13     ) reject limit unlimited ;

Table created.

SQL> select * from addr;
MAC
------------------------------------------------
 12-AF-97-7E-89-5E
 65-00-27-00-A7-1A
 A2-B6-C0-7D-93-96
 11-FF-48-C8-0E-11
 9C-B6-C2-FE-0C-0E


Get Mac adress in datbabase

emaduldeen, June 18, 2022 - 7:39 pm UTC

i am using oracle 12 c , i think it is using java 8

All Required Steps To Get MAC Address

Mohamed Younis, September 28, 2022 - 8:42 am UTC

Would you please give us all steps with it is explanations because when I try to do it i couldn't

Connor McDonald
September 29, 2022 - 12:31 am UTC

which platform?

A reader, October 10, 2022 - 1:53 pm UTC

Iam using Oracle Apex
Connor McDonald
October 11, 2022 - 3:27 am UTC

So you saw my external table example.

Then on a page in APEX, you could just create an item and you're done!

select mac
into :P1_MY_ITEM
from addr;

Get Logged User MAC Address Using Oracle Apex

Mohamed Younis, October 12, 2022 - 6:38 am UTC

Thanks for your reply,
I don't know how to create this java class or how to import it into Oracle Database



import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;


public class MACAddress {
public static void main(String[] args) throws Exception
{
// instantiate the MACAddress class
MACAddress obj = new MACAddress();

// call the getMAC() method on the current object
// passing the localhost address as the parameter
//obj.getMAC();


System.out.println( "getMacWindow2="+getMacWindow2() );

System.out.println( "getMacWindow="+getMacWindow() );

System.out.println( "getMac="+getMAC() );

}

// method to get the MAC addresses of the
// localhost machine
public static String getMAC ()
{
String stMac = "adress is :";
try {

// create an Enumeration of type
// NetworkInterface and store the values
// returned by
// NetworkInterface.getNetworkInterfaces()
// method
Enumeration<NetworkInterface> networks
= NetworkInterface.getNetworkInterfaces();

// for every network in the networks Enumeration
while (networks.hasMoreElements()) {
NetworkInterface network
= networks.nextElement();

// call getHardwareAddress() method on each
// network and store the returned value in a
// byte array
byte[] mac = network.getHardwareAddress();

if (mac != null) {


// convert the obtained byte array into
// a printable String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format(
"%02X%s", mac[i],
(i < mac.length - 1) ? "-"
: ""));
}

// print the final String containing the
// MAC Address

//System.out.print(sb.toString()+",");
stMac = stMac+ sb.toString()+"," ;
}

}
}
catch (SocketException e) {
e.printStackTrace();
}
return stMac;
}

public static String getMacWindow() throws SocketException, UnknownHostException
{
// create a variable of type NetworkInterface and
// assign it with the value returned by the
// getByInetAddress() method
String machinMacAdress = "";
InetAddress addr = InetAddress.getLocalHost();

// instantiate the MACAddress class
//MACAddress obj = new MACAddress();
//System.out.print("MAC Address of the system : ");

// call the getMAC() method on the current object
// passing the localhost address as the parameter


NetworkInterface iface
= NetworkInterface.getByInetAddress(addr);

// create a byte array and store the value returned
// by the NetworkInterface.getHardwareAddress()
// method
byte[] mac = iface.getHardwareAddress();

// convert the obtained byte array into a printable
// String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format(
"%02X%s", mac[i],
(i < mac.length - 1) ? "-" : ""));
}

// print the final String containing the MAC Address
//System.out.println(sb.toString());
machinMacAdress = sb.toString() ;
return machinMacAdress;
}

public static String getMacWindow2() {
InetAddress ip;
String myMacAdress = null ;
try {
ip = InetAddress.getLocalHost();
System.out.println("IP address : " + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
myMacAdress=sb.toString() ;

} catch (Exception e) {
e.printStackTrace();
}
return myMacAdress;
}
}

Chris Saxon
October 12, 2022 - 7:47 am UTC

Use the loadjava tool to import it. The Java Developer's Guide has examples of how to do this and publish it as a stored procedure

https://docs.oracle.com/en/database/oracle/oracle-database/19/jjdev/Java-stored-procedure-application-example.html#GUID-F3E46789-808B-4D0D-84B7-95AA8DB66B55

SQL Reference

A reader, October 12, 2022 - 11:22 am UTC

I don't know how to create this java class

CREATE JAVA

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library