aspose file tools*
The moose likes JDBC and the fly likes below code work in command line. but it doesn't work in eclipse. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "below code work in command line. but it doesn Watch "below code work in command line. but it doesn New topic
Author

below code work in command line. but it doesn't work in eclipse.

sam liya
Ranch Hand

Joined: Nov 25, 2008
Posts: 1204
below code work in command line. but it doesn't work in eclipse.
why?


import java.sql.*;
import java.util.Properties;
import java.io.InputStream;


public class NewConnectMe {
public static void main (String args[]) {

String driverPrefixURL = "jdbcdbc:";
String username = null;
String password = null;
String dataSource = null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
System.out.println("Failed to load JDBC/ODBC driver.");
return;
}


try {
// Look for resource file 'odbc.datasource'
InputStream is = ClassLoader.getSystemResourceAsStream ("odbc.datasource");
String s = is.toString();
System.out.println(s);

Properties p = new Properties();
p.load (is);
dataSource = p.getProperty("datasource.name");
if (dataSource == null)
throw new Exception ();
username = p.getProperty("datasource.username", "");
password = p.getProperty("datasource.password", "");
} catch (Exception e) {
System.out.println("Unable to read resource to get data source");
e.printStackTrace();
return;
}


try {
Connection con = DriverManager.getConnection(driverPrefixURL+dataSource, username, password);
System.out.println("Connected.");
DatabaseMetaData dmd = con.getMetaData();
if (dmd == null) {
System.out.println ("No Database Meta Data");
} else {
System.out.println ("Database Product Name : " + dmd.getDatabaseProductName());
System.out.println ("Database Product Version: " + dmd.getDatabaseProductVersion());
System.out.println ("Database Driver Name : " + dmd.getDriverName());
System.out.println ("Database Driver Version : " + dmd.getDriverVersion());
}
con.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}

harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
please use code tag.

share the error too


SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
sam liya
Ranch Hand

Joined: Nov 25, 2008
Posts: 1204
This is the error message i got when it run in eclipse.


Unable to read resource to get data source
java.lang.NullPointerException
at NewConnectMe.main(NewConnectMe.java:25)



i had copied the "odbc.datasource" file to the same directory i have working in.
(and sorry for the don't using code tags. its bcos of i don't know how to use it)
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

Put the location of odbc.datasource in your project's classpath, or put the file in a location that is in the project's classpath..

Eclipse does not use your CLASSPATH variable. You have to set it in your project properties.


OCUP UML fundamental and ITIL foundation
youtube channel
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

aruna sameera wrote:
(and sorry for the don't using code tags. its bcos of i don't know how to use it)

http://faq.javaranch.com/java/UseCodeTags
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: below code work in command line. but it doesn't work in eclipse.