469,326 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

How do you connect a Java program to a database?

how to connect java with database?
Jan 23 '08 #1
8 1390
JosAH
11,448 Expert 8TB
Google for 'jdbc'.

kind regards,

Jos
Jan 23 '08 #2
BigDaddyLH
1,216 Expert 1GB
Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

MODERATOR
Jan 23 '08 #3
BigDaddyLH
1,216 Expert 1GB
Here is Sun's JDBC tutorial:

http://java.sun.com/docs/books/tutorial/jdbc/index.html
Jan 23 '08 #4
Here is Sun's JDBC tutorial:

http://java.sun.com/docs/books/tutorial/jdbc/index.html
Hi,

First decide which database u want to connect it?

Only the three steps:

1.If it is in the Oracle then..

String query="Select * from emp_table";
class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:<dataso urcename>");
PreparedStatement ps=con.prepareStatement(query);
ResultSet rs=ps.execute();

2.If it is for MsAcess then

the above steps are same only the driver and class.forName changes ..
now i forget it later i will send it.

Pls try the above coding and reply is must.If it is wrong Pls send the correct answer...

bye takecare!

by
ela
Jan 24 '08 #5
import java.sql package
Load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

the above method throws ClassNotFoundException

establish the connection
Connection con=DriverManager.getConnection("URL","userid","pw d")
the method in the above will throw SQLException

PreparedStatement ps=con.prepareStatement("sql query");

the method above throws SQLException

AND

after doing with the database operations

close the connections
con.close();
this throws SQLException
Jan 24 '08 #6
import java.sql package
Load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

the above method throws ClassNotFoundException

establish the connection
Connection con=DriverManager.getConnection("URL","userid","pw d")
the method in the above will throw SQLException

PreparedStatement ps=con.prepareStatement("sql query");

the method above throws SQLException

AND

after doing with the database operations

close the connections
con.close();
this throws SQLException

Hi,

Pls do the above code within the try block
try{

<code>
}
catch(SQLException exception){
}


try this.......
Jan 24 '08 #7
ajos
283 100+
Hi,

First decide which database u want to connect it?

Only the three steps:

1.If it is in the Oracle then..

String query="Select * from emp_table";
class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:<dataso urcename>");
PreparedStatement ps=con.prepareStatement(query);
ResultSet rs=ps.execute();

2.If it is for MsAcess then

the above steps are same only the driver and class.forName changes ..
now i forget it later i will send it.

Pls try the above coding and reply is must.If it is wrong Pls send the correct answer...

bye takecare!

by
ela
Yuo should add the jar file in the class path(in the lib folder of your application). The error you are getting states that.
Jan 24 '08 #8
JosAH
11,448 Expert 8TB
As a side note: I see everone going Class.forName("your.driver") but doesn't anyone
use the more versatile DataSource method for obtaining (pooled) connections?
It's been here since Java 1.4. Read all about it in its API documentation ...

kind regards,

Jos
Jan 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.