Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connect to a database via JDBC-ODBC
12-21-2010, 08:29 AM
Post: #1
Connect to a database via JDBC-ODBC
You have to keep in mind that the bridge JDBC-ODBC is only useful in an Application, you can't use it with JAVA Applet because ODBC requires some DLL on the client machine (forbidden for security reason).

import java.net.URL;
import java.sql.*;

class JDBCapp {

static Connection theConn;

public static void main (String args[]) {
try {
// connection to an ACCESS MDB
theConn = MyConnection.getConnection();

ResultSet rs;
Statement stmt;
String sql;

sql = "select objet from Email";
stmt = theConn.createStatement();
rs = stmt.executeQuery(sql);

while (rs.next()) {
System.out.println(rs.getString("objet"));
}
rs.close();
stmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (theConn != null) theConn.close();
}
catch (Exception e) {
}
}

}
}

class MyConnection {
public static Connection getConnection() throws Exception {
Driver d = (Driver)Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/tech97.mdb"
);
return c;
/*
To use an already defined ODBC Datasource :

String URL = "jdbc:odbc:myDSN";
Connection c = DriverManager.getConnection(URL, "user", "pwd");

*/
}
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved