Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginner needs help

 
John Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to retrieve something from MS Access database but having trouble. DSN name is MyAddress. It appears that the driver has been loaded but the table can not be found, shouldn't tableName be the same the the DSN name? Please help!! Thanks!
My code:

import java.sql.*;
public class AnsiLevelAccess
{ public static void main(String[]args)
{
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc dbc:MyAddress");
DatabaseMetaData dbMetaData = c.getMetaData();
System.out.println(dbMetaData.getDatabaseProductName() +" "+dbMetaData.getDatabaseProductVersion());
System.out.println("Entry Level " +dbMetaData.supportsANSI92EntryLevelSQL());
System.out.println("Intermediate " +dbMetaData.supportsANSI92IntermediateSQL());
System.out.println("Full Level " +dbMetaData.supportsANSI92FullSQL());

Statement stm = c.createStatement();
String query = "SELECT * FROM MyAddress";

ResultSet rset = stm.executeQuery(query);

while(rset.next())
{System.out.println (" " + rset.getString(1));}

}catch(ClassNotFoundException cnfe)
{System.err.println("error loading driver:"+ cnfe);}

catch(SQLException sqle)
{System.err.println("Error connecting:"+sqle);}

}
}

Error message when execute:
ACCESS 04.00.0000
Entry Level true
Intermediate false
Full Level false
Error connecting:java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot find the input table or query 'MyAddress'. Make sure it exists and that its name is spelled correctly.
Press any key to continue . . .

 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
dsn name and table name are two diffrent things. dsn represents your whole database to which u'll be connecting. this database may consist of a number of tables.
we connect to a specific database using a dsn name.once the connection is established we can write and execute different queries involving any of the tables present in the database and in our query we need to specify the name of the specific table (and not the dsn ) from which we need to retrieve data.
hope this helps
regards
deeksha
 
Geoff Tate
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DSN is just a name given for your path to your .mdb file. DSN and table names within the .mdb file are not related.
 
John Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic