2

While working with the JAVA jdbc i encountered the "Invalid escape sequence" error

Actual default connection string:

jdbc:sqlserver://localhost:1433;integratedSecurity=true;<more properties as required>;

But, the connection string I got from Visual Studio is:

Data Source=(LocalDB)\v11.0;Initial Catalog="C:\USERS\310213706\DOCUMENTS\VISUAL STUDIO 2013\PROJECTS\DATABASE102\DATABASE102\DATABASE102.MDF";Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False

Could please help me generate the connectionURL??

Any help much appreciated!

Thanks

Chetan Hireholi
  • 79
  • 3
  • 13
  • here is connection string that I use in my application to connect mysql database, maybe helps you `jdbc:mysql://192.118.110.10:3306/DatabaseName?characterEncoding=utf8&characterSetResults=utf8` – kadir Apr 22 '16 at 08:26
  • 1
    Backslashes need escaped in string literals, and they are not escaped in the connection string from Visual Studio above so the compiler is complaining that it does not recognize `\U` or `\3`, for example, as an escape sequence. To correct, escape the backslashes with another backslash. – hmjd Apr 22 '16 at 08:32
  • 1
    Also see [Building the Connection URL](https://msdn.microsoft.com/en-us/library/ms378428%28v=sql.110%29.aspx). – hmjd Apr 22 '16 at 08:32
  • Is the SQLServer running? What is the exception message you get? Show the Java code you use to connect. Maybe this post provide some help: http://stackoverflow.com/questions/8194547/hibernate-connection-url-for-sql-server-mdf-file/8195217#8195217 – SubOptimal Apr 22 '16 at 09:26
  • As far as I know, localdb is not supported by the SQL Server JDBC driver, see http://stackoverflow.com/questions/11345746/connecting-to-sql-server-localdb-using-jdbc – Mark Rotteveel Apr 22 '16 at 10:35

1 Answers1

1
String connectionUrl = "jdbc:sqlserver://(LocalDB)\\v11.0;user:xxx;password:xxx";                 
                  Connection con = null;
                  Statement stmt = null;
                  ResultSet rs = null;
                  try {                  
                     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                     con = DriverManager.getConnection(connectionUrl);                   
                     String SQL = "SELECT TOP 10 * FROM table";
                     stmt = con.createStatement();
                     rs = stmt.executeQuery(SQL);                    
                     while (rs.next()) {
                        System.out.println(rs.getString(4) + " " + rs.getString(6));
                     }
                  }               
                  catch (Exception e) {
                     e.printStackTrace();
                  }
Khan Abdulrehman
  • 816
  • 2
  • 10
  • 22
Chetan Hireholi
  • 79
  • 3
  • 13