您的位置:首页 >> 数据库 >> Sybase >> 正文
Sybase RSS
 

JConnect编程连接ASA示例

http://www.rdxx.com 06年12月03日 22:43 iTbulo 我要投稿

关键词: 示例 , jConnect , 连接 , 编程 , NEC
This is document will assist newcomers in writing java code to perform simple connectivity to an ASA database using the jConnect JDBC driver.

This article discusses a simple Java program which connects to an ASA database using the Sybase jConnect JDBC driver. Although simple, this code is commonly requested. Having this code at hand will prove as a handy reference. Further research into JDBC and the java.sql package is highly recommended to better understand JDBC programming.

Here is the code necessary to connect to an ASA database via jConnect. This sample retrieves a result set consisting of all the rows and columns of the employee table.

The myConnection code:

import java.sql.*;

public class myConnection {

  public static void main( String[] args ) {

    try {
       // Using Sybase jConnect 4.2
       Class.forName("com.sybase.jdbc.SybDriver");

       // Using Sybase jConnect 5.2
       //Class.forName("com.sybase.jdbc2.jdbc.SybDriver");

       Connection conn = DriverManager.getConnection("jdbc:sybase:Tds:testmachine-pc:2638", "dba", "sql");
       Statement stmt = conn.createStatement( );
       ResultSet rs = stmt.executeQuery( "SELECT * FROM EMPLOYEE");
       while ( rs.next( ) ) {
         System.out.println( rs.getInt( 1 ) );
       }
     } catch ( Exception e ) {
       System.out.println( "An exception occurred." );
     }

  } // end of main

} // end of MyConnection class


Brief explanation of the code:

In order to understand how this code works, important sections of code will be discussed.

Section 1:
import java.sql.*;

This line of code is necessary to have access to the database connectivity classes. The java.sql package contains the connection, statement, and result set classes necessary to retrieve and display data from a database.

Section 2:
// Using Sybase jConnect 4.2
Class.forName("com.sybase.jdbc.SybDriver");

// Using Sybase jConnect 5.2
//Class.forName("com.sybase.jdbc2.jdbc.SybDriver");

These two lines of code are used to dynamically load the Sybase jConnect driver. Change the commenting of this code according to which version of jConnect in use. Often people wonder why such code is used, and why is it not better to simply import the necessary packages. Depending on the logic of your application, it may be necessary to connect to your database from a variety of different ways. Dynamically loading a driver class with the Class.forName() method prevents us from having to import every single driver that could possibly be used. This is especially important when an application can be configured to connect to a variety of databases.

Section 3:
Connection conn = DriverManager.getConnection("jdbc:sybase:Tds:testmachine-pc:2638", "dba", "sql");

共2页  第1页 第2页


 
 
标签: 示例 , jConnect , 连接 , 编程 , NEC 打印本文
 
 
  相关资讯
RSS
 
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站