Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instantiating ODBC Data Source from Java

Status
Not open for further replies.

LIAMMCKENNA

Technical User
Aug 14, 2003
1
GB
As part of college project I am writing a java application that connectd to a MS Access 2000 Database on Windows XP platform.

Is there any other way of setting up the data source than through the ODBC Data Source Administrator in the control panel?

The reason is that I would like to automatically create databases that are ODBC compliant, so can be fed table data or SQL commands.

Any advice would be appreciated

Liam
 
You can dynamically set the DSN up, here is an example:

import java.sql.*;
import java.io.*;

public class DBSample
{

public static void main(String args[])
{

Connection aConnection; // Create a connection object.

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

//make sure that this is a space between "MicroSoft Access Driver" and "(*.mdb)"
//After the &quot;DBQ=<PATH OF THE DATABASE>&quot; you have the option to send a user name
//and password.

aConnection = DriverManager.getConnection(&quot;jdbc:eek:dbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/testDB.mdb&quot;,&quot;&quot;,&quot;&quot;);

Statement stmt = aConnection.createStatement();

ResultSet rs = stmt.executeQuery (&quot;Select * FROM aTable;&quot;);
while (rs.next())
{
String s1 = rs.getString(&quot;Field1&quot;);
String s2 = rs.getString(&quot;Field2&quot;);
String s3 = rs.getString(&quot;Field3&quot;);
int s4 = rs.getInt(&quot;Field4&quot;);


System.out.println(&quot;Value of Field1 is:&quot; + s1);
System.out.println(&quot;Value of Field2 is:&quot; + s2);
System.out.println(&quot;Value of Field3 is:&quot; + s3);
System.out.println(&quot;Value of Field4 is:&quot; + s4);

}
}

catch(SQLException e)
{

System.out.println(e);
}
catch (ClassNotFoundException c)
{
System.out.println(c);
}
}

}


Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top