SQL server 连接 参数 求解 2C

configuration.setProperty("DBDriver",
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
configuration.setProperty("DBUrl",
"jdbc:sqlserver://localhost:1433;databaseName=Robot");
configuration.setProperty("DBUsername", "sa");
configuration.setProperty("DBPassword", "1234");

            我电脑刚安装的SQL server2014   服务器名称是mengxianqian   登录名是sa 
            创建的数据库Robot   怎么连接

3个回答

public class ConnSQL {

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

    setConfig();
    Connection conn =  getConn();

    if(conn != null){
        System.out.println("success");
        conn.close();
    }
}
/**
 * 获取sqlservice数据库连接
 * @return
 * @throws ClassNotFoundException
 * @throws SQLException
 * @throws IOException
 */
public static Connection getConn() throws ClassNotFoundException, SQLException, IOException{

    //从配置文件中读取信息
    FileInputStream fis = new FileInputStream("configuration.prop");
    Properties config = new Properties();
    config.load(fis);

    String DBDriver = config.getProperty("DBDriver");
    String DBUrl = config.getProperty("DBUrl");
    String DBUsername = config.getProperty("DBUsername");
    String DBPassword = config.getProperty("DBPassword");

    //获取连接
    Class.forName(DBDriver);
    Connection conn =  DriverManager.getConnection(DBUrl, DBUsername, DBPassword);

    return conn;
}

/**
 * 设置sqlservice的配置信息
 * @throws IOException
 */
public static void setConfig() throws IOException{

    Properties configuration = new Properties();
    FileWriter fw = new FileWriter("configuration.prop");

    //设置配置信息
    configuration.setProperty("DBDriver", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
    configuration.setProperty("DBUrl","jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=supermarket");
    configuration.setProperty("DBUsername","log1");
    configuration.setProperty("DBPassword", "123");
    configuration.store(fw,"sql's configuration");
    System.out.println("success");
}

}

如果是本计算机,并且密码是1234,则不用修改,否则localhost修改为mengxianqian。密码修改为你的密码。
sql server 2014上启用tcp/ip协议和远程连接,本地防火墙关闭或者允许1433端口。

java的话,先加sql.jar 还得区分是哪个版本的jar包 ,你这个是2014的还得找到2014版本的jar包,然后写一个jdbc注册驱动,连接数据库的方法就行

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐