桥连的
package loginch13;
import java.sql.*;
public class ConBean {
public static Connection getConBean(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver ") ;
} catch (ClassNotFoundException ex) {
}
try {
Connection conn = DriverManager.getConnection("jdbc:odbc:test");
return conn;
} catch (SQLException ex1) {
return null;
}
}
public static void main(String ars[]){
ConBean obj=new ConBean();
Connection conn=obj.getConBean();
if(conn!=null){
System.out.println("hello world");
}
}
}
直连的
package regist;
import java.sql.*;
public class DBConn {
public static Connection getCon(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException ex) {
System.out.println("加载失败") ;
}
try {
return DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;", "sa", "");
} catch (SQLException ex1) {
return null;
}
}
public static void main(String args[]){
if(DBConn.getCon() !=null){
System.out.println("连接成功") ;
}
}
}