public class DBConnection {
public Connection getConnection()
{
Connection conn=null;
//链接SQLServer2000
//String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
// String SERVANDDB="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=tempdb";
//连接SQLServer2005和2008
String CLASSFORNAME="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String SERVANDDB="jdbc:sqlserver://localhost:1433;DatabaseName=advertisement";
//SQL2008
String USER="sa";
String PWD="123";
try
{
Class.forName(CLASSFORNAME);
conn=DriverManager.getConnection(SERVANDDB,USER,PWD);
}catch(Exception ex)
{
ex.printStackTrace();
}
return conn;
}
public static void main(String[] args)
{
ResultSet rs=null;
DBConnection db=new DBConnection();
System.out.println(db.getConnection());
}
}