473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1708
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.