import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author 逍湘
*
*/
public class Java_Odbc_Sql {
/**
* drivename odbc驅(qū)動(dòng)
* url中的username為數(shù)據(jù)源名
*/
private static final String drivename="sun.jdbc.odbc.JdbcOdbcDriver";
private static final String url="jdbc:odbc:username;user=sa;password=123" ;
/**
* odbc連接數(shù)據(jù)庫(kù)方法: getCon()
* @return con
* @throws Exception
*/
public static void getCon() throws Exception{
try {
Class.forName(drivename);//在JVM中注冊(cè)JDBC驅(qū)動(dòng)程序
@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;
}
}
/**
* 連接測(cè)試
* @param agrs
*/
public static void main(String agrs[]){
try{
getCon();
System.out.println("連接成功!");
}catch(Exception e){
System.out.println("連接失敗!");
e.printStackTrace();
}
}
}