Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I connect to a database using java?

 
Sylvia Graham
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 508
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Sylvia, hope you like it here.
 
Nam Ha Minh
Ranch Hand
Posts: 508
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic