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.
          posted on 2006-04-09 10:42 飛揚 閱讀(289) 評論(0)  編輯  收藏 所屬分類: Sound Code

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          統計

          FAQ網站

          JSP/Java/Servlet開發工具

          JSP/Java/Servlet技術著名網站

          JSP/Servlet應用程序站點

          優選博客

          主站蜘蛛池模板: 荣昌县| 武山县| 阆中市| 永兴县| 马尔康县| 灵宝市| 临潭县| 那坡县| 德令哈市| 搜索| 包头市| 金阳县| 会泽县| 金堂县| 久治县| 定安县| 自治县| 汝阳县| 即墨市| 锦州市| 徐水县| 马边| 宜川县| 南昌市| 怀安县| 象州县| 黄浦区| 永春县| 潞西市| 三河市| 太康县| 沽源县| 余姚市| 宁阳县| 玉林市| 华亭县| 双城市| 屯留县| 乌拉特前旗| 南康市| 东阿县|