472,992 Members | 3,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

How do you connect a Java program to a database?

how to connect java with database?
Jan 23 '08 #1
8 1690
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

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

Similar topics

6
by: Jeff Sandler | last post by:
I have a database I created in mySQL. I've been entering data every day into the database using a Java application that I wrote. The database and the Java program are on the same Win 98 SE...
0
by: Mike Chirico | last post by:
I found the following interesting and wanted to pass it along Reference (TIP 12): http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download Connect and Select from Java //...
30
by: aka | last post by:
Hi I have a DB2 v8.1 on AIX and DB2 Connect EE on Solaris wich is connected to OS/390 DB2 subsystems via APPC / SNA. I have cataloged the DB2 Connect instance as tcpip node and then the Host DB...
11
by: Mikael Arhelger | last post by:
Hello, This has been posted a few times but still I could not find a way to connect to our database. We run DB2 Express on WIN2K server with XP clients. I can ping inside network and to the...
3
by: arasub | last post by:
ep 20, 2007 11:25:57 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.