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

JDBC and MS Access

 
Shuaib Gill
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to write JDBC code to query data from an MS Access database. Now I have the database set up, the database is called sag.mdb, the table in this database is called emp( with fields 'first','last','age'). I am trying to run a select * from emp query, but I get errors. Here is the code:
import java.sql.*;
import java.io.*;
import java.util.*;
//import jdbc.obbc.JdbcOdbcDriver;
public class trySQL
{
public static void main(String args[])
{
String url, user, password;
url="jdbc dbc:emp";
user = null;
password = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url,user,password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from demo");
while(rs.next())
{
String s1 = rs.getString(1);
System.out.println(s1);
}

}
catch(Exception e)
{System.out.println(e);}
}
}
The error reads : No data found.
Why? I have the database set up, the table is correct and it is on my localhost, the server is apache web server.
please help.
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your URL should be url="jdbc dbc:sag".
I am not sure what demo is here
stmt.executeQuery("select * from demo");
You want to select from your table name.

Bosun
 
Shuaib Gill
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bosun, I got it connected to the db and it is working now.
------------------
programmer77
 
Nasser Aboobaker
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Shoaib,
Could you plese explain where in a webserver we should store the database(mdb). I am unable to connect to my Access database. It says noClassfound error.
Thanks in advance..
I am getting this message...
The error is java.lang.ClassNotFoundException: sun.jdbc.odbc.jdbcOdbcDriver
Do we need a web server to query from a data base??
nasser
[This message has been edited by Nasser Aboobaker (edited October 22, 2001).]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nasser, the problem could be the fact that you had sun.jdbc.odbc.jdbcOdbcDriver instead of sun.jdbc.odbc.JdbcOdbcDriver
The 'J' needs to be capitalized. (if this is what you have in your code) As long as your DSN is specified correctly, it should be able to find it.

Bosun
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic