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驅動程序
@SuppressWarnings("unused")
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();
}
}
}