Here is sample code for a java program that connects to an Oracle database. This code compiles in the 244 Linux lab with minor modifications.
YOU WILL NEED TO CHANGE <username> AND <password> TO MATCH YOUR ORACLE DATABASE USERNAME AND PASSWORD. Your username is the part before '@cs' that you use for sqlplus.
Note that the class name is DBCon, so it would have to be in a file called DBCon.java, unless you want to change the name.
This program connects to the database, runs the query, and iterates through a ResultSet containing the query results and displays the first item of each result to the screen.
Make sure to have the appropriate ojdbc library in your CLASSPATH, whether it's ojdbc5.jar or ojdbc6.jar. They are available from http://students.cs.ndsu.nodak.edu/javadownloads/.
If you're using the CS Dept Linux lab, you can find these jar files in /opt/lablibs/
import java.io.*; import java.sql.*; public class DBCon { public static void main(String[] args) throws SQLException, ClassNotFoundException { //load the Oracle driver Class.forName("oracle.jdbc.driver.OracleDriver"); //create a connection to the database Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@db1.chpc.ndsu.nodak.edu:1521:cs", "<username>", "<password>") ; //create a query statement Statement stmt = conn.createStatement(); String query = "<QUERY>"; //replace this with your query //run the query, return a result set ResultSet rset = stmt.executeQuery(query); //for each result while(rset.next()) { //print the first item in each result System.out.println(rset.getString(1)); } //clean up database classes stmt.close(); rset.close(); conn.close(); } }
This code should compile in the lab once the username, password, and query are replaced. Compiling on another computer will require you to download the ojdbc JAR and including the JAR in your classpath or project. Details on including libraries in your Netbeans and Eclipse project are located here.
Given the file above saved as DBCon.java, with username and password changed, you can compile in the lab using the following:
Make sure to set the classpath variable to point to both the current directory and file 'ojdbc6.jar' so that your program can use it at run time.
The following example assumes that I have DBCon.java and ojdbc6.jar in my home directory.
helsene@lab18:~$ export CLASSPATH="./:/opt/lablibs/ojdbc6.jar" helsene@lab18:~$ javac DBCon.java helsene@lab18:~$ java DBCon 123 234 helsene@lab18:~$ exit
Should that not work, you can explicitly set the classpath for the `javac` and `java` commands as such:
javac -classpath "./:/opt/lablibs/ojdbc6.jar" DBCon.java - and - java -classpath "./:/opt/lablibs/ojdbc6.jar" DBCon
You use the -classpath switch to set the classpath. The classpath parameter is then a colon-delimited list of directories and .jar files that you would like to have included. The directory that your program resides in needs to be specified as well as the path to the ojdbc6.jar file. Using './' refers to the current directory.