package net.xiaoxiang.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author 逍湘
*/
public class Java_Odbc_Sql
{
/**
* drivename odbc驱动 url中的username为数据源名
*/
private static final String drivename = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String url = "jdbc:odbc:username;user=sa;password=123";
/**
* odbc连接数据库方法: getCon()
* @return con
* @throws Exception
*/
public static void getCon ( ) throws Exception
{
try
{
Class.forName ( drivename );// 在JVM中注册JDBC驱动程序
Connection con = DriverManager.getConnection ( url );// 建立到DBMS的连接
}
catch ( SQLException e )
{
System.err.println ( e.getMessage ( ) );
throw e;
}
catch ( Exception e )
{
System.err.println ( e.getMessage ( ) );
throw e;
}
}
/**
* 连接测试
* @param agrs
*/
public static void main ( String agrs[] )
{
try
{
getCon ( );
System.out.println ( "连接成功!" );
}
catch ( Exception e )
{
System.out.println ( "连接失败!" );
e.printStackTrace ( );
}
}
}