Skip to content

Instantly share code, notes, and snippets.

@madan712
Created December 25, 2012 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madan712/4372177 to your computer and use it in GitHub Desktop.
Save madan712/4372177 to your computer and use it in GitHub Desktop.
JDBC example using MS Access
/* TestJDBC.java */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestJDBC {
public static void main(String[] args) {
try {
Connection con;
Statement stat;
// Step 1: Loading Drivers
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Step 2: Making Connection
con = DriverManager.getConnection("jdbc:odbc:EmployeeDSN");
// Step 3: Creating JDBC Statement
stat = con.createStatement();
String query = "select empId, empName from employee";
// Step 4: Execute the statement
ResultSet rset = stat.executeQuery(query);
// Step 5: Looping through the ResultSet
while (rset.next()) {
System.out.println(rset.getString(1)+" "+rset.getString(2));
}
// step 6: Close the Connection and Statement
stat.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment