This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sam liya
Ranch Hand
Posts: 1240
  • 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 = "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
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please use code tag.

share the error too
 
sam liya
Ranch Hand
Posts: 1240
  • 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)
 
Jan Cumps
Bartender
Posts: 2584
11
C++ Linux Netbeans IDE
  • 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.
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic