I am new to JDBC and just made my first program in Eclipse. I am using Oracle 11g XE as database. I have added ojdbc6.jar
to my classpath by copying it in the lib folder of the jdk installation. This is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
/**
* @param args
*/
String url = "jdbc:oracle:thin:@Voldemort:1521:XE";
String username = "surender";
String password = "oracle";
Statement stmt;
String query;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test().work();
}
void work() {
try {
Connection conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
query = "SELECT * FROM employees";
ResultSet rset = stmt.executeQuery(query);
while(rset.next()) {
System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getString(3) + " " + rset.getString(4));
}
}
catch(SQLException se) {
System.out.println("Exception!!");
se.printStackTrace();
}
}
}
Now for some reason it doesn't works in the eclipse nor when I run it outside the IDE through the command prompt (I am on windows 7). But when I added the ojdbc6.jar
to the project build path in eclipse it runs fine in the eclipse but as expected still doesn't works from the prompt.
I followed a few tutorials on the net and they all mention that the driver files are needed to be added to the classpath but nothing else. (i added ojdbc6.jar
to the buildpath just on a whim, kind of like kiss and tell :p).
Now, why is this?
What kind of resources are needed to be added in the build path?
What if I wanted to run my project outside the IDE, how would i do that?
Thanx in advance!