wood burning stoves*
The moose likes JDBC and the fly likes How do I connect to a database using java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How do I connect to a database using java?" Watch "How do I connect to a database using java?" New topic
Author

How do I connect to a database using java?

Sylvia Graham
Greenhorn

Joined: Aug 15, 2012
Posts: 1
Howdy all ya'll at the Java Ranch. I have a new position at Texas Tech and I am LOST!!!
I have been trying for 3 weeks to get a database connection so I can develop apps for my department. I have been trying to use a jdbc connection, but obviously I'm not doing something right. I have the jdk installed and can compile and run simple programs.
My problem is, no matter how I try, I can't get a database driver to load. I have downloaded ojdbc14.jar file and have tried passing the path to it as -cp but I always get the ClassNotFoundException.
I looked in the jar and it has several Oracle jdbc components. Do I need to unpack the jar or something to get this to work?
here is what I have:
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (Exception e){
System.out.println("ARRG " + e.getMessage());
}

I am passing the javac compiler and the jre the flag -cp C:\ORACLE\ojdbc14.jar but I still get the exception output.
Can anyone help?
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

When using javac, you don't need to use -cp flag. But when running the program, you mus specify the -cp flag to let Java know where to load the driver class.

Here is an example of how to connect to a database via JDBC.


PM Me If You Want to get Urgent Help on Java Programming
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Sylvia Graham wrote:
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (Exception e){
System.out.println("ARRG " + e.getMessage());
}


Welcome to the Ranch!

You can try "oracle.jdbc.driver.OracleDriver", maybe that will work for you.

Ohh, and please use code tags to make your code easier readable when you post here
jasonhu hu
Greenhorn

Joined: Aug 01, 2012
Posts: 6
D. Ogranos wrote:
Sylvia Graham wrote:
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (Exception e){
System.out.println("ARRG " + e.getMessage());
}


Welcome to the Ranch!

You can try "oracle.jdbc.driver.OracleDriver", maybe that will work for you.

Ohh, and please use code tags to make your code easier readable when you post here


it is so late,don't you go to bed
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
No, never, why?

(JK, its early morning here)

In addition to my post above, this might also be helpful: Oracle JDBC API.

You can see there that you could also use "oracle.jdbc.OracleDriver" in the call to Class.forName(). Note that "oracle.jdbc.driver.OracleDriver" does not seem to be contained in that version of the driver anymore, but it still works with the (older) version I'm using in my example code. If in doubt, check which version of the driver you're using.
BalaMurali dhar
Ranch Hand

Joined: Apr 14, 2012
Posts: 60
The code is Here table name is Student.
import java.sql.*;
public class SelectTest {
public static void main(String[] args)throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection('jdbc: odbc:oradsn","scott","tiger");
ResultSet rs = st.executeQuery("select * from student");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
st.close();
con.close();
}
}
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Welcome to the ranch Sylvia, hope you like it here.
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Also note that, from JDBC 4.0 (since Java 6), there is no need to load the driver explicitly. The driver manager will load the suitable driver class available in classpath automatically. See how.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I connect to a database using java?
 
Similar Threads
Connecting to an Oracle DB
thin driver / classpath / jdk1.4
thin driver / classpath / jdk1.4
Java to Display Data from Database
once again: classpath and ojdbc14.jar