Get your CodeRanch badge!*
The moose likes JDBC and the fly likes example of jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "example of jdbc" Watch "example of jdbc" New topic
Author

example of jdbc

mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
i need an example of a simple class which shows me how to make a jdbc connection to an ms access database and use of a select query ....
i will be grateful...
thanks in advance
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

There are a bunch of pages out there that will help, but basically the steps are

  1. Create you MS Access file
  2. Register your file with ODBC
  3. Start making calls to it!

  4. Dave.
kim jungil
Greenhorn

Joined: Sep 27, 2001
Posts: 27
Originally posted by mansoor iqbal:
i need an example of a simple class which shows me how to make a jdbc connection to an ms access database and use of a select query ....
i will be grateful...
thanks in advance

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection mycon = DriverManager.getConnection("jdbc dbc:dsn_name","user_name","password");
PreparedStatement pstmt = mycon.prepareStatement
("select * from tablename",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
} catch(Exception e) {
System.out.println(e);
}
kim jungil
Greenhorn

Joined: Sep 27, 2001
Posts: 27

[This message has been edited by kim jungil (edited October 15, 2001).]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
import java.sql.*;
public class DBAccess{
public static void main(String[] args)
{
// Load the driver
try
{
// Load the driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Define the data source for the driver
String yourURL = "jdbc dbc:yourDSName";
// Create a connection through the DriverManager
Connection dConnection =
DriverManager.getConnection(yourURL);
Statement stmt = dConnection.createStatement();
ResultSet RS = stmt.executeQuery(
"SELECT some columns from your DB"
// Output the resultset data
while(RS.next())
System.out.println("your selected columns));
}
catch(ClassNotFoundException c)
{
System.err.println(c);
}
catch(SQLException s)
{
System.err.println(s);
}
}
}
------------------
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
ty ty ty ty all!!!
but
i tried the code and it works fine. the only problem is that i am using swing to display the results......and as soon as i scroll the table by pressing the down-arrow key , i get the dreaded "illegal operation ..."...
i have run the the program 10 times and it does the same thing every time...
HELP!
kim jungil
Greenhorn

Joined: Sep 27, 2001
Posts: 27
Originally posted by mansoor iqbal:
ty ty ty ty all!!!
but
i tried the code and it works fine. the only problem is that i am using swing to display the results......and as soon as i scroll the table by pressing the down-arrow key , i get the dreaded "illegal operation ..."...
i have run the the program 10 times and it does the same thing every time...
HELP!

if it is possible, you can paste your code here or we do not know the problem exactly.
 
Don't get me started about those stupid light bulbs.
 
subject: example of jdbc
 
Similar Threads
How to provide willdcard search
Use of J2EE
jdbc-trigger creation
java database connectivity
Example drivers for all the 4 types of JDBC Drivers