sandesh
Posts: 10
Nickname: sandesh
Registered: Sep, 2003
|
|
Re: connecting to msaccess database?!
|
Posted: Sep 24, 2003 2:57 PM
|
|
this is what i wrote to get data from the msaccess database names northwind... i am assuming that u did create an mdb file of the database u r trying to work on..
public static void main (String args[]) { String url = "jdbc:odbc:Northwind"; String logInName = "user"; String password = "user";
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connect1 = DriverManager.getConnection(url,logInName,password); DatabaseMetaData data = connect1.getMetaData(); System.out.println ("Connected to " + data.getURL()); System.out.println("Driver " + data.getDriverName()); System.out.println("Version " + data.getDriverVersion()); Statement simpleStatement = connect1.createStatement(); String query = "SELECT * FROM Employees"; ResultSet myResultSet = simpleStatement.executeQuery (query); displayResultSet(myResultSet); myResultSet.close(); simpleStatement.close(); connect1.close(); }
catch (SQLException e) { } catch (java.lang.Exception e) { } } public static void displayResultSet (ResultSet myResultSet) throws SQLException { int count; ResultSetMetaData myData = myResultSet.getMetaData(); int numberOfColumns = myData.getColumnCount();
for (count=1; count <= numberOfColumns; count++) { System.out.print(myData.getColumnLabel(count) + "|"); }
System.out.println(); boolean rowState = myResultSet.next(); while (rowState) { for (count=1; count<=numberOfColumns; count++) { System.out.print(myResultSet.getString(count) + "|"); } System.out.println(); rowState = myResultSet.next(); } } }
|
|