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 飞扬 阅读(276) 评论(0)  编辑  收藏 所属分类: Sound Code

只有注册用户登录后才能发表评论。


网站导航:
 

统计

FAQ网站

JSP/Java/Servlet开发工具

JSP/Java/Servlet技术著名网站

JSP/Servlet应用程序站点

优选博客