在java程序中用JDBC连接SQLServer的示例代码

fmms 13年前
     代码如下,不多做解析:    <pre class="brush:java; toolbar: true; auto-links: false;">import java.sql.*;  public class DBConnectionManager {     //SQLServer      private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//加载驱动程序     private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";//设置数据库连接串     private String user = "sa";//数据库登录用户名     private String password = "root";//数据库登录密码     private static String message = "恭喜,数据库连接正常!";     public void setDriverName(String newDriverName) {         driverName = newDriverName;     }     public String getDriverName() {         return driverName;     }          public void setUrl(String newUrl) {         url = newUrl;     }     public String getUrl() {         return url;     }     public void setUser(String newUser) {         user = newUser;     }     public String getUser() {         return user;     }     public void setPassword(String newPassword) {         password = newPassword;     }     public String getPassword() {         return password;     }     public Connection getConnection() {         try {             Class.forName(driverName);             return DriverManager.getConnection(url, user, password);         } catch (Exception e) {             e.printStackTrace();             message = "数据库连接失败!";             return null;         }     }     public static void main(String[] args) {         try{             DBConnectionManager dcm = new DBConnectionManager();             Connection conn = dcm.getConnection();             System.out.println(message);         }catch(Exception e){             e.printStackTrace();         }     } }</pre>来自:http://xp9802.iteye.com/blog/1254698