Newbie - a question about import and .jar statement

Z

zalek

Hello,

I wrote a java application that is using "import
com.microsoft.sqlserver.jdbc.*;" , compiled it and created a .jar
file.
Program is working OK from my IDE.
Here is my question:
I want to execute this .jar file an a different PC. Do I need to put
on this PC also a jar file with classes used for "import
com.microsoft.sqlserver.jdbc.*"?

Thanks,

Zalek
 
A

Arne Vajhøj

zalek said:
I wrote a java application that is using "import
com.microsoft.sqlserver.jdbc.*;" , compiled it and created a .jar
file.
Program is working OK from my IDE.
Here is my question:
I want to execute this .jar file an a different PC. Do I need to put
on this PC also a jar file with classes used for "import
com.microsoft.sqlserver.jdbc.*"?

Yes. You need to put the MS jar file as well. Unless
you extract the class files from the MS jar file and
put them in your jar file, which would be a bad idea
and may even violate license terms.

Arne

PS: You should not need to import the MS classes. JDBC
should work with pure reflection and interfaces.
 
Z

zalek

Yes. You need to put the MS jar file as well. Unless
you extract the class files from the MS jar file and
put them in your jar file, which would be a bad idea
and may even violate license terms.

Arne

PS: You should not need to import the MS classes. JDBC
     should work with pure reflection and interfaces.

Thanks Arne for you answer.
Now I have more questions:
1. which import statement should I use instead of
com.microsoft.sqlserver.jdbc.*;
2. If I will move the .jar with MS or JDBC classes to the same
directory as a jar with my application - do I need to add this
directory to a classpath?

Thanks,

Zalek
 
A

Arne Vajhøj

Now I have more questions:
1. which import statement should I use instead of
com.microsoft.sqlserver.jdbc.*;
java.sql.*

2. If I will move the .jar with MS or JDBC classes to the same
directory as a jar with my application - do I need to add this
directory to a classpath?

Put a Class-Path directive in your jar file's manifest that
point to the teh MS jar file.

Arne
 
M

Mark Space

zalek said:
Thanks Arne for you answer.
Now I have more questions:
1. which import statement should I use instead of
com.microsoft.sqlserver.jdbc.*;

Normally, none. Use "Class.forName()" and supply the driver name as a
parameter to the script or application.


2. If I will move the .jar with MS or JDBC classes to the same
directory as a jar with my application - do I need to add this
directory to a classpath?

Sort of. To use the classpath, you would add the directories that
..class files are in. Jar files however have to be added themselves.
I.e., add "C:\mydir\subdir\msfiles.jar" to the classpath. The .jar file
has to be named explicitly.

But this doesn't matter if you are running from a Jar file. The
classpath is ignored. Set the class path attribute in the Jar's
manifest file.

<http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html>

You could also use a custom class loader and set the location of the Jar
or other resource files via a supplied parameter (property, config file,
user input, etc.). That's a lot more advanced however.

<http://java.sun.com/docs/books/tutorial/deployment/jar/jarclassloader.html>
 
A

Arved Sandstrom

Mark Space said:
Normally, none. Use "Class.forName()" and supply the driver name as a
parameter to the script or application.


<http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html>
[ SNIP ]

Other useful references are:

1)
http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/drivermanager.html
2) http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/datasource.html

Note that DataSource is preferred if you can use it. As the docs point out,
the advantages of this over using DriverManager are (a) not hardcoding
driver info, and (b) taking advantage of connection pooling and distributed
transactions etc. Of course, as far as (a) is concerned the hardcoding for
DriverManager is strings, not class imports, and these strings can be stored
externally according to best practices. Note that Mark's reference to
Class.forName() is in the context of DriverManager.

Using one of these approaches your application code is blissfully unaware of
driver specifics. It only needs to import java.sql.* for the "generic" stuff
like Connection.

AHS
 
A

Arne Vajhøj

Arved said:
Other useful references are:

1)
http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/drivermanager.html
2) http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/datasource.html

Note that DataSource is preferred if you can use it. As the docs point out,
the advantages of this over using DriverManager are (a) not hardcoding
driver info, and (b) taking advantage of connection pooling and distributed
transactions etc. Of course, as far as (a) is concerned the hardcoding for

DataSource is usually the right way, but it should be noted that:
- driver does not need to be hardcoded with DriverManager, it can be
made configurable as well
- you do not get connection pooling and distributed just by using
DataSource, some data sources (often those used in Java EE
contexts) *can* provide those features

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top