Tuesday, May 01, 2007

Java code snippet for ODBC connections.

Java code snippet for ODBC connections.



import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement ;

public class JDBCDemo {
public static void main( String[] args ) {
try {
// Connect to the database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// JDBC:ODBC:
String database = "jdbc:odbc:DRIVER={Microsoft Visual Foxpro Driver};SourceType=DBF;"
database+= "SourceDB=J:\\data\\;Exclusive=No;Collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";
// USERNAME/PASSWORD (If Needed)
Connection con = DriverManager.getConnection(database, "", "");
// Execute the SQL statement
Statement stmt = con.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * from claim");
// Loop thru all the rows
while( resultSet.next() ) {
String data = resultSet.getString( "field" );
System.out.println( data );
}
stmt.close();
}
catch( Exception e ) {
System.out.println (e.getMessage());
e.printStackTrace();
}
}
}

No comments: