T
his exaple using JDBC-ODBC bridge drive program.Using table NorthwindTest.
NorthwindTest.java
package skyey.snow;
import java.sql.*:
/** A JDBC example that connects to the Microsoft Acess sample Northwind database,issues a simple SQL query to the
? *??employee table, and prints the results.
*/
public class NorthwindTest {
??? public static void main(String[] args)
??? {
??????? String driver="sun.jdbc.odbc.JdbcOdbcDriver";
??????? String url="jdbc:odbc:Northwind";
??????? String username=""; //No username/password required
??????? String password=""; //for desktop access to MS Access.
??????? showEmployeeTable(driver,url,username,passwrd);
???????
??????? /** Query the employee table and print the first and
???????? *? last names.
???????? */
??????? public static void showEmployeeTable(String driver,String url,String username,String password)
??????? {
??????????? try
??????????? {
??????????????? //load database driver if it's not already loaded.
??????????????? Class.forName(driver);
??????????????? //Establish network connection to database.
??????????????? Connection connection=
??????????????????????? DriverManager.getConnection(url,username,password);
??????????????? System.out.println("Employees\n"+"=============================");
??????????????? //Create a Statement for executing queries.
??????????????? Statement statement=connection.clearWarnings();
??????????????? String query=
??????????????????????? "select firstName,lastName form employee";
??????????????? //send query to database and store results.
??????????????? ResultSet resultSet=statement.executeQuery(query);
??????????????? //print results.
??????????????? while(resultSet.next())
??????????????? {
??????????????????? System.out.println(resultSet.getString("firstName")+"");
??????????????????? System.out.println(resultSet.getString("lastName")+"");
??????????????? }
??????????????? connection.close();
??????????? }
??????????? catch(ClassNotFoundException e)
??????????? {
??????????????? System.err.println("Error loading driver:"+e);
??????????? }
??????????? catch(SQLException e1)
??????????? {
??????????????? System.err.println("Error with connection:"+e1);
??????????? }
??????? }
??? }
}
from example we should konw how to write a simpleness class for Database connection,and in this example i find a very good method we should study that is we should connection two or much more class using :
public class NorthwindTest {
??? public static void main(String[] args)
??? {
??????? String driver="sun.jdbc.odbc.JdbcOdbcDriver";
??????? String url="jdbc:odbc:Northwind";
??????? String username=""; //No username/password required
??????? String password=""; //for desktop access to MS Access.
???
???? showEmployeeTable(driver,url,username,passwrd);???????
??????? /** Query the employee table and print the first and
???????? *? last names.
???????? */
??????
? public static void showEmployeeTable(String driver,String url,String username,String password)??????? {}
if we do like this our codes will became short,simpleness and beautiful.