import java.sql.*;
public class TestSqlServerJDBC {
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=bd_web";
String userName="sa";
String userPwd="sa";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(dbURL, userName, userPwd);
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from news");
while(rs.next()){
System.out.println(rs.getString("title"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
if (rs != null){
rs.close();
rs = null;
}
if (stmt != null){
stmt.close();
stmt = null;
}
if (conn != null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
使用的驅動包只有一個sqljdbc4.jar ,而另一個sqljdbc.jar 則是用在JDK5.0之前版本。所以看你用的JDK是哪個版本了。這個很好配置,你就直接選擇一個,如果運行報SQLExcption 那麼你選另一個就行了。sqlserver數據庫啓動包下載地址爲:http://www.microsoft.com/zh-cn/ 。在裏面搜索 sql server jdbc 就可以找到。具體配置部署,看這個網站上的說明,和下載下來的幫助文檔,相當簡單。windows和unix系統都有說明。