how to connect java with database?
How do you connect a Java program to a database?
Collapse
X
-
Tags: None
-
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.
MODERATORComment
-
Hi,Originally posted by BigDaddyLH
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.J dbcOdbcDriver") ;
Connection con=DriverManag er.getConnectio n("jdbc:odbc:<d atasourcename>" );
PreparedStateme nt ps=con.prepareS tatement(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
elaComment
-
import java.sql package
Load the driver
Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
the above method throws ClassNotFoundEx ception
establish the connection
Connection con=DriverManag er.getConnectio n("URL","userid ","pwd")
the method in the above will throw SQLException
PreparedStateme nt ps=con.prepareS tatement("sql query");
the method above throws SQLException
AND
after doing with the database operations
close the connections
con.close();
this throws SQLExceptionComment
-
Originally posted by shaileshkumarimport java.sql package
Load the driver
Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
the above method throws ClassNotFoundEx ception
establish the connection
Connection con=DriverManag er.getConnectio n("URL","userid ","pwd")
the method in the above will throw SQLException
PreparedStateme nt ps=con.prepareS tatement("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(SQLExcept ion exception){
}
try this.......Comment
-
Yuo should add the jar file in the class path(in the lib folder of your application). The error you are getting states that.Originally posted by 2008serelaHi,
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.J dbcOdbcDriver") ;
Connection con=DriverManag er.getConnectio n("jdbc:odbc:<d atasourcename>" );
PreparedStateme nt ps=con.prepareS tatement(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
elaComment
-
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,
JosComment
Comment