How to fix the The connection string contains a badly formed name or value

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

Code example:

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();         }     } } 

Then I got the below error:

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

Please help me.

回答1:

Your connection string looks like it's not in line with what's specified in the documentation.

Try changing your connection string from:

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

To:

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


回答2:

try this it will definitely work

Connection con = DriverManager                     .getConnection("jdbc:sqlserver://localhost:1433;databaseName=<name of your database>;user=sa;password=VMADMIN#123");   


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!