Created
December 25, 2012 07:59
-
-
Save madan712/4372177 to your computer and use it in GitHub Desktop.
JDBC example using MS Access
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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