Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 2658
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Legend has it that if you rub the right tiny ad, a genie comes out.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic