Forums Register Login

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

+Pie Number of slices to send: Send
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();
}
}
}

+Pie Number of slices to send: Send
please use code tag.

share the error too
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1246 times.
Similar Threads
Java-Xml file not getting Connected to DataBase
Connection Pooling with Commons DBCP - spot the problem?
how to connect MS Access directly
SQLException
SQL Server 2008r2 Connection Failed
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:31:13.