14 Replies Latest reply on Sep 12, 2003 4:56 PM by 843798

# crazy java.sql.SQLException. I coldn't solve!

hi i tried to connect database (mdb file) but it gives a SQLException
Connection con=null;
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.setLogStream(System.out);

con=DriverManager.getConnection("jdbc:odbc:db1");
}

catch(Exception ex)
{System.out.println (ex);
System.out.println(con);
}
it says that con is null and the exception is:

DriverManager.getConnection("jdbc:odbc:db1")
trying driver[className=sun.jdbc.odbc.JdbcOdbcDriver,sun.jdbc.odbc.JdbcOdbcD
river@19134f4]
*Driver.connect (jdbc:odbc:db1)
JDBC to ODBC Bridge: Checking security
No SecurityManager present, assuming trusted application/applet
JDBC to ODBC Bridge 2.0001
Current Date/Time: Thu Sep 11 01:36:37 EEST 2003
Allocating Environment handle (SQLAllocEnv)
hEnv=47256224
Allocating Connection handle (SQLAllocConnect)
hDbc=47256392
Connecting (SQLDriverConnect), hDbc=47256392, szConnStrIn=DSN=db1
RETCODE = -1
ERROR - Generating SQLException...
SQLException: SQLState(IM002) vendor code(0)
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Veri kayna???? ad?? bulunam
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:3
23)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at p2p.Tools.main(Tools.java:114)
Closing connection (SQLFreeConnect), hDbc=47256392
Closing environment (SQLFreeEnv), hEnv=47256224
getConnection failed: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Ve
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Veri kayna???? ad?? bulunam
null
Press any key to continue...
• ###### 1. Re: crazy java.sql.SQLException. I coldn't solve!
I believe you are incorrectly using the getConnection() method. At a very minimum, the argument passed to the method should be the URL to the database, ie., dbServer.spawnn.com, not "jdbc:odbc:db1".

See http://java.sun.com/j2se/1.4.2/docs/api/java/sql/DriverManager.html and look at the allowable values for getConnection().
• ###### 2. Re: crazy java.sql.SQLException. I coldn't solve!
"jdbc:odbc:db1" is fine as long as you have 'db1' set up as a valid working DSN...

getConnection() is often called with the user name and password as the 2nd and 3rd arguments...

• ###### 3. Re: crazy java.sql.SQLException. I coldn't solve!
I didn't know you could have a hostname with colens (the : symbol) in it. Wow!

Or are you suggesting that db1.odbc.jdbc is equivalent to jdbc:odbc:db1?

Err... really, I thought the driver manager requires something that's either an IP address or a name that can be resolved to an IP address by the available DNS resources - but I didn't know that DNS itself can resolve names that are in formats other than X.Y.Z where Z is the top-most domain.

Do you have an RFC I could look at?
• ###### 4. Re: crazy java.sql.SQLException. I coldn't solve!
You probably didn't set up the DSN.

You can also do this without a DSN if you write your URL like this (for M\$ Access database):
String url = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\foo\\bar\\baz.mdb";
The DBQ should be the full path to your Access database. Check the JDBC forum for the exact syntax. I learned it there. - MOD
• ###### 5. Re: crazy java.sql.SQLException. I coldn't solve!
He's correct, TheDavid - that URL is perfectly acceptable for an ODBC DSN. Read the JDBC Short Course under the Tutorials link to the left of this forum. It'll spell it out for you. - MOD
• ###### 6. Re: crazy java.sql.SQLException. I coldn't solve!
Another question: are you trying to do this from an applet? I see "applet" in your log stream.

You realize, of course, that an applet can only connect back to the server it was downloaded from. I think getting an applet to connect to a database is a special case. I'd do a search on the JDBC forum if that's what you're doing. - MOD
• ###### 7. Re: crazy java.sql.SQLException. I coldn't solve!
Thanks!

The exact section is http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/connection.html#997649.

This is my fault, I didn't read the API closely enough either. Interestingly enough, the Oracle database that I use does allow for an Internet-style URL but as Duffymo's guide points out, "Driver writers are the ones who actually determine what the JDBC URL that identifies a particular driver will be." It goes on to say that "...URL may refer to a logical host or database name that is dynamically translated to the actual name by a network naming system."

So both systems are valid, but it appears that jdbc:<subprotocol>:<subname> is actually the preferred convention.

Sorry Spawnn. I was wrong.
• ###### 8. Re: crazy java.sql.SQLException. I coldn't solve!
it looks like the ODBC database db1 is not set. the connection printed out as null is correct as it is from the catch block. i think the op is playing a trick, those strange letters are made up by him/her, the original text should state what the problem is, and like i said before, it is probably because the odbc database not set properly.
• ###### 9. Re: crazy java.sql.SQLException. I coldn't solve!
thank you people i have solved my exception.
i am using JFrame not Applet.
i write is as
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=database/db1.mdb"); 
and it works fine!!!

but is there a solution for accesing the database on the web with JFrame? my new question is: i want to access a databese on the web from my pc with JFrame. (not with applet!)
and i write as
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
DriverManager.setLogStream(System.out);

Connection con = DriverManager.getConnection("jdbc:odbc:Driver=Microsoft Access Driver *.mdb)};DBQ=http://www.ug.bcc.bilkent.edu.tr/~yenely/db1.mdb"); 
how can i solve the problem and the exception is:
DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};
DBQ=http://www.ug.bcc.bilkent.edu.tr/~yenely/db1.mdb")
trying driver[className=sun.jdbc.odbc.JdbcOdbcDriver,sun.jdbc.odbc.JdbcOdbcD
river@19134f4]
*Driver.connect (jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=http://w
ww.ug.bcc.bilkent.edu.tr/~yenely/db1.mdb)
JDBC to ODBC Bridge: Checking security
No SecurityManager present, assuming trusted application/applet
JDBC to ODBC Bridge 2.0001
Current Date/Time: Thu Sep 11 13:23:46 EEST 2003
Allocating Environment handle (SQLAllocEnv)
hEnv=49353376
Allocating Connection handle (SQLAllocConnect)
hDbc=49353544
Connecting (SQLDriverConnect), hDbc=49353544, szConnStrIn=Driver={Microsoft Acce
ss Driver (*.mdb)};DBQ=http://www.ug.bcc.bilkent.edu.tr/~yenely/db1.mdb
RETCODE = -1
ERROR - Generating SQLException...
SQLException: SQLState(S1000) vendor code(-1044)
java.sql.SQLException: [Microsoft][ODBC Microsoft Access S�r�c�s�] Ge&#63676;ersiz dosy
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:3
23)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at p2p.Tools.main(Tools.java:111)
Closing connection (SQLFreeConnect), hDbc=49353544
Closing environment (SQLFreeEnv), hEnv=49353376
getConnection failed: java.sql.SQLException: [Microsoft][ODBC Microsoft Access S
java.sql.SQLException: [Microsoft][ODBC Microsoft Access S�r�c�s�] Ge&#63676;ersiz dosy
at p2p.Tools.main(Tools.java:121)
Press any key to continue...

• ###### 10. Re: crazy java.sql.SQLException. I coldn't solve!
Your problem has NOTHING to do with JFrame. It's that database URL. The path you use to get to the database is still incorrect. You can't make an HTTP connection to the database. This isn't right:
DBQ=http://www.ug.bcc.bilkent.edu.tr/~yenely/db1.mdb
If you want to connect to a database on a machine that's not your own desktop, you're going to have to map a network drive to it and use THAT as your full path:
DBQ=Z:\\db1.mdb
Where you've mapped the Z drive to www.ug.bcc.bilkent.edu.tr/~yenely or wherever this Access database lives.

Is this database on your LAN? Why are you giving an HTTP URL? - MOD
• ###### 11. Re: crazy java.sql.SQLException. I coldn't solve!
i am writing a P2P program and i dont want a server which collects databease (shared files)

instead all peers write their shared files to a database in the web site
http://www.ug.bcc.bilkent.edu.tr/~yenely/db1.mdb

does anybody know how to access database on the web from PC?
• ###### 12. Re: crazy java.sql.SQLException. I coldn't solve!
There are two approaches:

1) Choose a database that is capable of accepting connections via the network such as Oracle, SQL Server, MySQL but not Access. I don't think Microsoft Access can do this. I may be wrong though and I have been wrong before (see above).

Once you have, your program can connect to it via the IP address and port (or alternatively, the Internet name and port). This is the second acceptable form of getConnection() that I mentioned before.

The problem with specifying a string such as jdbc:odbc:db1 is that the computer attempts to find db1 on your specific machine - which is appropriate for an Access database. As mentioned elsewhere, you can configure DSN to redirect jdbc:odbc:db1 requests to another machine.

Incidently, you do not need to specify a protocol such as http. Simply doing DriverManager.getConnection( "dbServer.spawnnn.com", "spawnnn", "leetG0d" ); should work.

2) Alternatively, you can code a mechanism that listens for and accepts "requests" from your application, then subsequently passes it along to the database.

For example, it would be trivial to write a web page that consists of a form and offers text fields corresponding to the SELECT, FROM, and WHERE clauses of an SQL query. Once you understood how that worked, you can fake an http request, i.e., something like...

http://dbServer.spawnnn.com/db.jsp?select="*"&from="passwds"&where="id=spawwn"

Getting the results back into your application would be difficult though, particularly if the data has to be in binary format (such as images).
• ###### 13. Re: crazy java.sql.SQLException. I coldn't solve!
david you are right.
it can be done by asp,php,jsp
by that way i can write or delete something from database
but how can i take the search results?
• ###### 14. Re: crazy java.sql.SQLException. I coldn't solve!
One idea that occurs to me is to have the database... portal for lack of a better word, generate a basic HTML table with the search results. Send that table back to the application with a bare minimum of tags.

Your application can receieve the "file" sent back by the portal - look at some of the code for basic web clients which can be found in any Java programming book - parse the table tags and convert it into a two dimensional array, i.e., a recordset.

The disadvantages are a) in setting up the http, so many things can go wrong, and b) all of the data in the array would have to be of type string by necessity.

I really do recommend that you either research how to make MS Access accept network connections via a hostname and a port, or switch to a free alternative such as MySQL. Once you do so, then your application can directly connect to the database and you are spared having to code file transfer protocols.