JDBC and Jar

how do you do a DB connection through a jar file?
[56 byte] By [Consideratea] at [2007-11-27 4:13:18]
# 1
If I had not seen your other thread ( http://forum.java.sun.com/thread.jspa?threadID=5171994) I would have great difficulty following your question.I am still not sure what exactly your problem is. What exactly is the error message?
cotton.ma at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

The JDBC jar file is generally downloaded from the DB manfacturer's site, for example http://www.mysql.com , http://www.microsoft.com , http://www.ibm.com , etcetera. A self-respected manfacturer usually also have extensive documentation at their site how to use this jar, how to connect, how to query and how to process results. Look around at their sites.

For generic JDBC stuff, just read the Sun's official JDBC tutorial which you can find here: http://java.sun.com/docs/books/tutorial/jdbc/

BalusCa at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Sorry for not explaining my problem more precise.

Well I'm using SQL Server 2005 from MS and I'll show you a program that works when it isn't in a jar file but when it is it doesn't work at all.

import java.sql.*;

import java.io.*;

public class SQL

{

public static void main(String[] args)

{

try

{

String strKnapp = "Hej";

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

String connectionUrl = "jdbc:sqlserver://localhost;user=Villex;password=viktor;databaseName=dbMusik";

Connection con = DriverManager.getConnection(connectionUrl);

String sql = "INSERT INTO FileName VALUES (?) ";

PreparedStatement stmnt = con.prepareStatement(sql);

stmnt.setString(1, strKnapp);

stmnt.executeUpdate();

con.close();

}

catch(Exception exc)

{

}

}

}

Consideratea at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

> Sorry for not explaining my problem more precise.

> Well I'm using SQL Server 2005 from MS and I'll show

> you a program that works when it isn't in a jar file

> but when it is it doesn't work at all.

There a number of problems with your code like not closing your statement and having an empty catch block.

But your real problem is classpath related.

cotton.ma at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Ok so the JDBC Driver can't be found is that it?
Consideratea at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Don't suppress exceptions and add at least e.printStackTrace() to the catch block. Big change that you will see a ClassNotFoundException for the com.microsoft.sqlserver.jdbc.SQLServerDriver then. If this is the case, you need to specify the JDBC driver in the class-path of the manifest.mf of the JAR file.

BalusCa at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

So this is my error message...

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:169)

at SQL.main(SQL.java:14)

Consideratea at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
Clear. Add the driver to the classpath of the JAR file.
BalusCa at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
> Clear. Add the driver to the classpath of the JAR> file.How?
Consideratea at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
Specify the relative path to the jar containing the driver in the Class-path entry of the manifest.mf of your jar.
BalusCa at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11
in your manifest.mf file add this:Class-Path: sqljdbc.jar
java_2006a at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12

Hello, not sure if this is similar, but I'm having problems with running my java applications once they have been packet into jar files.I am using an external jar file as a library... all is fine within the compiler, but once exported it doesn't want to work...

I think I know the problem... the problem is that the packet jar file can't see the libraries within the external jar file... I have checked the classpath file and its pointing to the correct place, but it doesn't see it...

Do you have any idea on what I'm doing wrong?

Coopera at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 13
Probably you're checking the wrong classpath and/or interpreting it wrongly? That is a common mistake of most new Java developers. Read the answers right above here.
BalusCa at 2007-7-12 9:19:30 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...