ChristerH
asked on
Connecting to a jdbc:odbc database on a remote machine ...
The normal way to get a connection if you run the built-in ODBC database driver for Microsoft Access that Access installs in the control panel is this java line.
DriverManager.getConnectio n("jdbc:od bc:mydb");
But how do I connect to a database on the LAN/WAN or just using the IP for my own machine
This does not work!
DriverManager.getConnectio n("jdbc:od bc://127.0 .0.1/mydb" );
Do I have to export the database?
/Christer
DriverManager.getConnectio
But how do I connect to a database on the LAN/WAN or just using the IP for my own machine
This does not work!
DriverManager.getConnectio
Do I have to export the database?
/Christer
ASKER
Hi!
DriverManager.getConnectio n("jdbc:od bc:mydb");
works just fine on my machine. I just would like to run on my friends machine and just tried out the syntax:
DriverManager.getConnectio n("jdbc:od bc://127.0 .0.1/mydb" );
before entering his IP-address. But I just get an SQL exception.
DriverManager.getConnectio
works just fine on my machine. I just would like to run on my friends machine and just tried out the syntax:
DriverManager.getConnectio
before entering his IP-address. But I just get an SQL exception.
Try using the following syntax:
<protocol>:@<hostname>:<po rt>
For example,
jdbc:oracle:thin:@hostname .domain.co m:1234
<protocol>:@<hostname>:<po
For example,
jdbc:oracle:thin:@hostname
ASKER
For me, would that be
jdbc:odbc:mydb@myhost.mydo main.com:1 234
What is the portnumber? How do I export a DB?
jdbc:odbc:mydb@myhost.mydo
What is the portnumber? How do I export a DB?
ASKER
Should it be like:
"jdbc:odbc://hostname:port /mydb"
But I'm still confused about the portnumber ... is it possible to export a Microsoft Access database this way?
"jdbc:odbc://hostname:port
But I'm still confused about the portnumber ... is it possible to export a Microsoft Access database this way?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi ChristerH,
I will explaint in detail.
1.Create a database (Assume name is chris.mdb) in a directory (Assume xyz) in a system (Assume Christerh )
2.In each client in LAN/WAN
a. type
net use f: \\Christerh\c
b.Create a dsn (Assume abc) in
start->setting->controlpan el->odbc-> systemdsn- >mdb->
with path
f:\xyz\chris.mdb
3.Use dsn name abc in java program
as jdbc:odbc:abc
That's all
Best of luck.
I will explaint in detail.
1.Create a database (Assume name is chris.mdb) in a directory (Assume xyz) in a system (Assume Christerh )
2.In each client in LAN/WAN
a. type
net use f: \\Christerh\c
b.Create a dsn (Assume abc) in
start->setting->controlpan
with path
f:\xyz\chris.mdb
3.Use dsn name abc in java program
as jdbc:odbc:abc
That's all
Best of luck.
My answer:
---
URL syntax
The "odbc" subprotocol
The "odbc" subprotocol has been reserved for URLs that specify ODBC style Data Source Names. For this subprotocol we specify a URL syntax that allows arbitrary attribute values to be specified after the data source name.
The full odbc subprotocol URL syntax is:
jdbc:odbc:<data-source-nam e>[;<attri bute-name> =<attribut e-value>]*
Thus valid jdbc:odbc names include:
jdbc:odbc:qeor7
jdbc:odbc:wombat
jdbc:odbc:wombat;CacheSize =20;Extens ionCase=LO WER
jdbc:odbc:qeora;UID=kgh;PW D=fooey
---
if u cannot map drive (its not easy in WAN ) use this solution
:
http://java.sun.com/products/jdbc/faq.html
How can I use the JDBC API to access a desktop database like Microsoft Access over the network?
Most desktop databases currently require a JDBC solution that uses ODBC underneath. This is because the vendors of these database products haven't implemented all-Java JDBC drivers.
The best approach is to use a commercial JDBC driver that supports ODBC and the database you want to use. See the JDBC drivers page for a list of available JDBC drivers.
The JDBC-ODBC bridge from Sun's Java Software does not provide network access to desktop databases by itself. The JDBC-ODBC bridge loads ODBC as a local DLL, and typical ODBC drivers for desktop databases like Access aren't networked. The JDBC-ODBC bridge can be used together with the RMI-JDBC bridge , however, to access a desktop database like Access over the net. This RMI-JDBC-ODBC solution is free.
http://dyade.inrialpes.fr/mediation/download/
---
URL syntax
The "odbc" subprotocol
The "odbc" subprotocol has been reserved for URLs that specify ODBC style Data Source Names. For this subprotocol we specify a URL syntax that allows arbitrary attribute values to be specified after the data source name.
The full odbc subprotocol URL syntax is:
jdbc:odbc:<data-source-nam
Thus valid jdbc:odbc names include:
jdbc:odbc:qeor7
jdbc:odbc:wombat
jdbc:odbc:wombat;CacheSize
jdbc:odbc:qeora;UID=kgh;PW
---
if u cannot map drive (its not easy in WAN ) use this solution
:
http://java.sun.com/products/jdbc/faq.html
How can I use the JDBC API to access a desktop database like Microsoft Access over the network?
Most desktop databases currently require a JDBC solution that uses ODBC underneath. This is because the vendors of these database products haven't implemented all-Java JDBC drivers.
The best approach is to use a commercial JDBC driver that supports ODBC and the database you want to use. See the JDBC drivers page for a list of available JDBC drivers.
The JDBC-ODBC bridge from Sun's Java Software does not provide network access to desktop databases by itself. The JDBC-ODBC bridge loads ODBC as a local DLL, and typical ODBC drivers for desktop databases like Access aren't networked. The JDBC-ODBC bridge can be used together with the RMI-JDBC bridge , however, to access a desktop database like Access over the net. This RMI-JDBC-ODBC solution is free.
http://dyade.inrialpes.fr/mediation/download/
Christ,
> But how do I connect to a database on the LAN/WAN or just using the IP for my own machine
This should depend on the settings of your network and database. This is what i do :
Class.forName("com.informi x.jdbc.Ifx Driver");
{ I am using informix db so here, specify the drivers }
DriverManager.getConnectio n("jdbc:in formix-sql i://127.0. 0.1:1234/m ydb:INFORM IXSERVER=d benv; user=user; password=password");
127.0.0.1 = your db server ip
:1234 = the port number set. (how to get this? ask your db admin)
This is set in your db server (DBMS). Remember it is a db server, not just a database.
mydb = your db name
INFORMIXSERVER = enviroment set for your db. This is specific to informix only.
user , password = login information to your db. u must have access to the db in order to use it, again ask your db admin for it.
The connection string depends largely on the db server and the network. It will not work for you, but hopefully it would help you to figure out whats your conn- string should be.
But if your are not running a dbms, i would suggest you try ravindra76's method..
> But how do I connect to a database on the LAN/WAN or just using the IP for my own machine
This should depend on the settings of your network and database. This is what i do :
Class.forName("com.informi
{ I am using informix db so here, specify the drivers }
DriverManager.getConnectio
127.0.0.1 = your db server ip
:1234 = the port number set. (how to get this? ask your db admin)
This is set in your db server (DBMS). Remember it is a db server, not just a database.
mydb = your db name
INFORMIXSERVER = enviroment set for your db. This is specific to informix only.
user , password = login information to your db. u must have access to the db in order to use it, again ask your db admin for it.
The connection string depends largely on the db server and the network. It will not work for you, but hopefully it would help you to figure out whats your conn- string should be.
But if your are not running a dbms, i would suggest you try ravindra76's method..
Hi,
IF you want to go for RMI - JDBC solutioin, a sample code.
In sun site, you will get sample examples
//Remote Interface
import java.rmi.*;
public class RMIJdbc extends Remote
{
public String getDataFromServerDatabase( ) throws RemoteException;
}
// Implementation class (SERVER)
import java.rmi.*;
import java.rmi.server.*;
public class RMIJdbcImpl extends UnicastRemoteObject implements RMIJdbc
{
Connection con;
PreparedStatement pst;
ResultSet rs;
String hostname="localhost"; // where you run the rmi registry
int port = 1099; // the port of the rmiregistry (by default it takes 1099)
String objectname="server"; // your object name in the naming service(rmiregistry)
public RMIJdbcImpl()
{
try
{
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
con = DriverManager.getConnectio n("jdbc:od bc:yourdsn ");
}
catch ( Exception ee)
{
ee.printStackTrace();
}
public String getDataFromServerDatabase( ) throws RemoteException
{
String toReturn = "";
try
{
pst = con.prepareStatement("sele ct ename from your table where empno=?)";
pst.setInt(1,100) //Assume yourtable contains like 100 as empno and //Rameshaa as ename
rs = pst.executeQuery();
if ( rs.next())
toReturn = rs.getString(1);
retrun toReturn;
}
public static void main(String args[])
{
RMIJdbcImpl server = new RMIJdbcImpl();
server.bindInNamingService ();
}
public void bindInNamingService(){
try
{
Naming.rebind("rmi://"+hos tname+":"+ hostpor+"/ "+objectna me,this);
}
Catch ( Exception eee)
{
eee.printStackTrace();
}
}
}
// CLient class
import java.rmi.server.*;
import java.rmi.*;
public class RMIClient
{
public static void main(String args[])
{
try
{
RMIJdbc obj = Naming.lookup("rmi://"+arg s[0]+":"+a rgs[1]+"/" +args[2]);
System.out.println(getData FromServer Database() );
}catch(Exception e){
e.printStackTrace();
}
}
}
Compiling:
javac RMIJdbc.java
javac RMIJdbcImpl.java
rmic RMIJdbcImpl
javac RMIClient.java
Running:
SERVER:
start rmiregistry ( you should keep the window open until you need your server)
java RMIJdbcImpl
CLIENT:
java RMIClient localhost 1099 server
( you can give the hostname where rmiregistry is running for your server the port is rmiregistry port
these params must be the same as you give in your server);
Best of luck
IF you want to go for RMI - JDBC solutioin, a sample code.
In sun site, you will get sample examples
//Remote Interface
import java.rmi.*;
public class RMIJdbc extends Remote
{
public String getDataFromServerDatabase(
}
// Implementation class (SERVER)
import java.rmi.*;
import java.rmi.server.*;
public class RMIJdbcImpl extends UnicastRemoteObject implements RMIJdbc
{
Connection con;
PreparedStatement pst;
ResultSet rs;
String hostname="localhost"; // where you run the rmi registry
int port = 1099; // the port of the rmiregistry (by default it takes 1099)
String objectname="server"; // your object name in the naming service(rmiregistry)
public RMIJdbcImpl()
{
try
{
Class.forName("sun.jdbc.od
con = DriverManager.getConnectio
}
catch ( Exception ee)
{
ee.printStackTrace();
}
public String getDataFromServerDatabase(
{
String toReturn = "";
try
{
pst = con.prepareStatement("sele
pst.setInt(1,100) //Assume yourtable contains like 100 as empno and //Rameshaa as ename
rs = pst.executeQuery();
if ( rs.next())
toReturn = rs.getString(1);
retrun toReturn;
}
public static void main(String args[])
{
RMIJdbcImpl server = new RMIJdbcImpl();
server.bindInNamingService
}
public void bindInNamingService(){
try
{
Naming.rebind("rmi://"+hos
}
Catch ( Exception eee)
{
eee.printStackTrace();
}
}
}
// CLient class
import java.rmi.server.*;
import java.rmi.*;
public class RMIClient
{
public static void main(String args[])
{
try
{
RMIJdbc obj = Naming.lookup("rmi://"+arg
System.out.println(getData
}catch(Exception e){
e.printStackTrace();
}
}
}
Compiling:
javac RMIJdbc.java
javac RMIJdbcImpl.java
rmic RMIJdbcImpl
javac RMIClient.java
Running:
SERVER:
start rmiregistry ( you should keep the window open until you need your server)
java RMIJdbcImpl
CLIENT:
java RMIClient localhost 1099 server
( you can give the hostname where rmiregistry is running for your server the port is rmiregistry port
these params must be the same as you give in your server);
Best of luck
as a data source in the windows control panel.