java - 如何修复连接字符串包含格式错误的名称或值

标签 java sql-server jdbc

代码示例:

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement;  
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test;   

public class SQLserverConnection  
{   
    // Connection object
    static Connection con = null;
    // Statement object
    private static Statement stmt;
    // Constant for Database URL
    public static String DB_URL = "jdbc:sqlserver://localhost:1433";        

    // Constant for Database Username
    public static String DB_USER = "sa";      
    // Constant for Database Password
    public static String DB_PASSWORD = "VMADMIN#123";

    @BeforeTest
    public void setUp() throws Exception 
    {
        try{
            // Make the database connection
            String dbClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";                   Class.forName(dbClass).newInstance();                   

            // Get connection to DB
            Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;sa;VMADMIN#123;");                   

            // Statement object to send the SQL statement to the Database
            stmt = con.createStatement();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Test
    public void test() {
        try{
            String query = "select * from userinfo";            

            // Get the contents of userinfo table from DB
            ResultSet res = stmt.executeQuery(query);            

            // Print the result untill all the records are printed            

            // res.next() returns true if there is any next record else returns false
            while (res.next()) {
                System.out.print(res.getString(1));
                System.out.print("\t" + res.getString(2));    
                System.out.print("\t" + res.getString(3));  
                System.out.println("\t" + res.getString(4));     
            }
        } catch(Exception e) {
           e.printStackTrace();
        }
    }

    @AfterTest
    public void tearDown() throws Exception {
        // Close DB connection
        if (con != null) {
            con.close();
        }
    }
}

然后我得到以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: The connection string contains a badly formed name or value.

请帮帮我。

最佳答案

您的连接字符串看起来不符合 documentation 中指定的内容.

尝试更改您的连接字符串:

"jdbc:sqlserver://localhost:1433;sa;VMADMIN#123;" 

收件人:

"jdbc:sqlserver://localhost:1433;user=sa;password={VMADMIN#123};"

关于java - 如何修复连接字符串包含格式错误的名称或值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35219172/

相关文章:

java - 从 Spring Data Jpa 查询返回自定义对象

java - 忽略代码覆盖率中的类和方法 (jacoco)

java - 如何通过WebStart部署JDBC客户端应用程序?

java.lang.IllegalArgumentException : the bind value at index 1 is null

java - 请求 Web API 的 GET 方法

java - 是否可以在 Candlestick jfreechart 中显示空心蜡烛?

sql-server - 如何在 'xp_cmdshell' DIR 命令中使用超过 128 个字符的路径

mysql - SSIS MySQL 将表复制到 SQL Server

java - Hibernate 根据日期添加序列

postgresql - 为什么我的 Clojure 应用程序需要_分钟_才能连接到 Postgres?