• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = "jdbc:odbc:";
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();
}
}
}

 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please use code tag.

share the error too
 
shawn peter
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic