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應用程序站點

          優選博客

          主站蜘蛛池模板: 福海县| 丰都县| 阿坝| 鄂托克旗| 游戏| 广宁县| 张家界市| 双鸭山市| 镇坪县| 遵义市| 清徐县| 广平县| 连城县| 灵璧县| 吉水县| 彭州市| 济宁市| 柳江县| 蒙城县| 宁河县| 会东县| 卫辉市| 巴彦县| 上犹县| 宣武区| 竹北市| 东兴市| 浮山县| 西畴县| 昌图县| 凤冈县| 铁岭市| 德兴市| 楚雄市| 凌源市| 虎林市| 乐陵市| 汶上县| 淮阳县| 宿松县| 寿阳县|