Connecting to a database

Uhmm.. I need to connect to a database by calling the ODBC jar file and getting the password from a textfile..

I'm not sure on how I can do that since I am a newbie with database in Java.. I'm using an access database.. I can retrieve the password and even print it but its the database i'm going to have a problem with..

Any ideas on how I should go about doing this?

Thanks in advance..

-ArchBytes-

[434 byte] By [Arch_Bytesa] at [2007-11-27 4:04:15]
# 1

First, make a DSN ( DataSource Name ) for the particular .mdb ( Access ) file that you want to connect to ( Data Sources ( ODBC ) under Control Panel in Windows ).

Assuming you give it the name 'MyDSN',

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = null;

try

{

con = DriverManager.getConnection("jdbc:odbc:MyDSN","username","password");

Statement st = con.createStatement();

ResultSet rs = st.executeQuery("select * from mytable");

while ( rs.next() )

{

System.out.println(rs.getString(1));

}

}//end of try

catch ( Exception e )

{

System.out.println('Encountered exception " + e );

}//end of catch

finally

{

if ( rs != null )

rs.close();

if ( st != null )

ss.close();

if ( con != null )

con.close();

}//end of finally

That ought to get you started....

More here:

http://www.javacoffeebreak.com/articles/jdbc/

nogoodatcodinga at 2007-7-12 9:09:06 > top of Java-index,Java Essentials,Java Programming...
# 2

ok so i was able to do those stuff.. when I ran it in eclipse it worked perfectly..

but when I ran it in the command prompt.. it had an exception..

Exception in thread "main" java.lang.NoClassDefFoundError: JarFileCaller

what does this mean..? why won't it run from the command prompt..?

Arch_Bytesa at 2007-7-12 9:09:06 > top of Java-index,Java Essentials,Java Programming...
# 3

It's working fine on mine....though there are lots of errors in the code I posted!

I think you need to set the classpath correctly. Are you using some other packages? Some JAR files? They may not be in the classpath. Have you tried running just the database connection code?

import java.sql.*;

public class testDB

{

public static void main ( String args[] )

{

testDB tdb = new testDB();

tdb.trythis();

}

public void trythis()

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = null;

Statement st = null;

ResultSet rs = null;

try

{

con = DriverManager.getConnection("jdbc:odbc:mytable","username","password");

st = con.createStatement();

rs = st.executeQuery("select * from users");

while ( rs.next() )

{

System.out.println(rs.getString(1));

}

}//end of try

catch ( Exception e )

{

System.out.println("Encountered exception " + e );

}//end of catch

finally

{

try

{

if ( rs != null )

rs.close();

if ( st != null )

st.close();

if ( con != null )

con.close();

}

catch( Exception e )

{

System.out.println("Exception while closing: " + e );

}

}//end of finally

}

catch ( Exception e )

{

}

}

}

If even just this doesn't work, then you're missing the JDBC-ODBC drivers, you'll have to add the path of the JARs to the classpath.

nogoodatcodinga at 2007-7-12 9:09:06 > top of Java-index,Java Essentials,Java Programming...
# 4
ok.. I fixed that problem.. I was compiling it in a different java version then I ran in a different one.. thanks..^_^So I fixed that.. now i have a new problem..Supposing the text file I was using was placed in a different directory.. How am I supposed to find it?
Arch_Bytesa at 2007-7-12 9:09:06 > top of Java-index,Java Essentials,Java Programming...
# 5
You will need to know the path of the file, I guess you'll have to use the absolute path. I'm not sure where the JVM would look if you just the filename but I'm guessing it would probably be in the current working working directory or something.
nogoodatcodinga at 2007-7-12 9:09:06 > top of Java-index,Java Essentials,Java Programming...