So Oracle is listening inside the virtual machine? And you're running this code from the virtual machine? Because even though the virtual machine is running on the same physical hardware as your host operating system, it's not the same logical machine - it has its own IP address, name, etc.
Localhost means the logical machine (i.e. Windows installation, Linux installation, etc) where you are currently running on. Oracle will be listening wherever you installed it. You're the only one who knows if that's the virtual machine or its host machine.
If it's in the virtual machine and you're running your Java program in the host machine you can connect by replacing localhost with the IP address or server name of the virtual machine. You'll need to make sure there's no firewall blocking access.