Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using ms-access as back end.pl hlp

 
Ram Ganesh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i intend to use ms-access as backend for my jdbc. i have got access2000 installed in my machine.if any one of u can pl. send me a small java program with basic set of commands for connecting to an access database it would be of great use to me.
thnx in advance
ram ganesh
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of using the JDBC-ODBC bridge with a Microsoft Access database. You need to define a system data source name (DSN) first; I've defined one for the USDA nutrient database (USDA).
There is a "Getting Started" document in the JDK that is quite useful for getting up to speed on JDBC.
import java.io.*;
import java.sql.*;
import java.util.*;
public class JDBC_Sample
{
public static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver";
public static final String DSNAME = "usda";
public static void main(String[] args)
throws Exception
{
new JDBC_Sample();
}
public JDBC_Sample() throws ClassNotFoundException, SQLException
{
// Load the JDBC driver class
Class.forName(DRIVER_CLASS);
// Connect to the database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc dbc:" + DSNAME);
stmt = con.createStatement();
// Run a query
rs = stmt.executeQuery("select * from fd_groups");
while (rs.next()) {
String code = rs.getString(1);
String description = rs.getString(2);
System.out.println(code + " " + description);
}
}
finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (con != null)
con.close();
}
}
}

------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets
[This message has been edited by Thomas Paul (edited April 14, 2001).]
 
Phil Hanna
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danged smilies! That should be
con = DriverManager.getConnection("jdbcdbc:" + DSNAME);


------------------
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the edit button to go in to your post and turn off smilies.
 
James LX
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Phil,
I am running into a similar problem. I alwasy got 'ClassNotFoundException' message when I run my program. I am using the driver "sun.jdbc.odbc.JdbcOdbcDriver". Where can i find this driver?
Thanks
James

Originally posted by Phil Hanna:
Here is an example of using the JDBC-ODBC bridge with a Microsoft Access database. You need to define a system data source name (DSN) first; I've defined one for the USDA nutrient database (USDA).
There is a "Getting Started" document in the JDK that is quite useful for getting up to speed on JDBC.
import java.io.*;
import java.sql.*;
import java.util.*;
public class JDBC_Sample
{
public static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver";
public static final String DSNAME = "usda";
public static void main(String[] args)
throws Exception
{
new JDBC_Sample();
}
public JDBC_Sample() throws ClassNotFoundException, SQLException
{
// Load the JDBC driver class
Class.forName(DRIVER_CLASS);
// Connect to the database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc dbc:" + DSNAME);
stmt = con.createStatement();
// Run a query
rs = stmt.executeQuery("select * from fd_groups");
while (rs.next()) {
String code = rs.getString(1);
String description = rs.getString(2);
System.out.println(code + " " + description);
}
}
finally {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (con != null)
con.close();
}
}
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic