I am trying to connect to a database when I run my application I am getting the error "com.microsoft.sqlserver.jdbc.SQLServerException: The connection string contains a badly formed name or value".
I have tried changing some of the variables around but cannot figure out where my error is. Can anyone help?
try{
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
String dbURL = "jdbc:sqlserver://[myservername];databaseName=[databasename];user=[enteruserdbhere];password[enterpasswordhere];";
Connection conn = DriverManager.getConnection(dbURL);
Statement statement = conn.createStatement();
conn.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
Try removing the '[]' brackets from the connection string.
The link below shows examples of connection strings for jdbc https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15
You should remove the brackets [ ] for your connection URL.
String server = "localhost";
String dbName = "myDB";
String username = "root";
String pwd = "";
String dbURL = "jdbc:sqlserver://server;databaseName=dbName;user=username;password=pwd;";
Also import your jdbc driver to the project as well us in the Connection.java file
Related
I wrote a little app to test connection to a remote SQL server using the following code:
SqlConnection myConnection = new SqlConnection();
SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();
bu.DataSource = #"<server>";
bu.InitialCatalog = "<database>";
bu.IntegratedSecurity = false;
bu.UserID = "<user id>";
bu.Password = "<password>";
myConnection.ConnectionString = bu.ConnectionString;
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
I am getting the unhelpful message "login failed for user...". I can login using that user on the server itself so the credentials are correct. I have also set the database to accept remote logins. I can ping back and forth to the server.
Is there a way to further test this to try and diagnose the problem?
This is connection string
<add name="dbdatabase"
connectionString="Data Source=.\SQLTSRV02;Initial Catalog=movies;Integrated Security=True"
providerName="System.Data.SqlClient" />
Class connect:
protected SqlConnection sqlcon;
public bool open_connection(string connection = "dbdatabase")
{
sqlcon = new SqlConnection(#WebConfigurationManager.ConnectionStrings[connection].ToString());
try
{
if(sqlcon.State.ToString()=="open")
{
sqlcon.Open();
}
return true;
}
catch(Exception ex)
{
return false;
}
}
I can't get a connection to the database. Maybe I've made a mistake. Please help me find it
Thanks in advance
Looking from the connection string, i believe you are using a Local DB .
Add your DB to the visual studio-server explorer( under data connections) and then connect it. Then try debugging the code.
This line:
sqlcon = new SqlConnection(#WebConfigurationManager.ConnectionStrings[connection].ToString());
should be this:
sqlcon = new SqlConnection(#WebConfigurationManager.ConnectionStrings[connection].ConnectionString);
i.e. .ToString() is the wrong thing.
However, like #Guillaume, I'm confused by the #WebConfigurationManager part of it. It looks like it shouldn't compile...
Hi I have a problem with my Java program, when I run the application within JDeveloper, the program works fine, it connects to the derby database properly.
When I create a executable jar i get the following error no suitable driver found for jdbc:derby://localhost:1527/c:..../dbcam
i have tried the following
{ public void DoConnect3( ) {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
// conenct to the database
// String host ="jdbc:derby://localhost:1527/dbCam";
//String host ="jdbc:derby://localhost:1527/C:/Users/nasir/SkyDrive/Java/db_Backup/dbCam";
String host ="jdbc:derby://localhost:1527/C:/Users/nasir/.netbeans-derby/dbCam";
String uName ="userCam";
String uPass ="cam";
con = DriverManager.getConnection(host,uName,uPass);
stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
//String sql = "SELECT * FROM tableCustomer";
String sql = "SELECT * FROM tableTroubleshooting";
//String tsss = Integer.toString(tss);
//String sql = "SELECT * FROM tableTroubleshooting where ProbID "+"'"+tsss+"'";
rs = stmt.executeQuery(sql);
rs.next();
}
That still doesn't do much only shows me message saying org.apache.derby.clientdriver.
Any suggestions would be gratefully appreciated.
Thanks
The url you're using is a ClientDriver url, not an EmbeddedDriver url. You need to add the correct jar and load the correct driver.
HTH
D
This question has been addressed all over the web and I tried a lot of things without success. The SQL EXPRESS service is setup to accept local system account but the problem still exists.
This is my connection string:
<add name="PhoneTemplateChange" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Database=PhoneTemplateChange;Integrated Security=SSPI" />
I created a class to do database operations in the constructor I have
_connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["PhoneTemplateChange"].ConnectionString;
and a method in this class to insert data
public void AddNewChangeOrder(int operation, int targetExt)
{
using (SqlConnection con = new SqlConnection(_connectionString))
{
string sql = "INSERT into [dbo].ChangeOrder (operation, targetExt, dtrequested) VALUES (#operation, #targetExt, #dtrequested)";
using (SqlCommand cmd = new SqlCommand(sql))
{
try
{
cmd.Parameters.AddWithValue("#operation", operation);
cmd.Parameters.AddWithValue("#targetExt", targetExt);
cmd.Parameters.AddWithValue("dtrequested", DateTime.Now);
//con.CreateCommand();
con.Open();
//cmd.InitializeLifetimeService();
int rows = cmd.ExecuteNonQuery();
con.Close();
}
catch (SqlException e)
{
throw new Exception(e.Message);
}
}
}
I have played around with the connection string trying all different suggestions, also the commented code in the method above is what I tried to solve the problem. Still no luck!
I also changed the connection string I get two different exceptions this way
Database=PhoneTemplateChange
The above gives the exception in the title.
And the following gives the Exception "Cannot open Database PhoneTemplatechange.mdf requested by the login. Login failed for user 'mydomain\myusername'"
Database=PhoneTemplateChange.mdf
Any ideas?
You are missing the line of code where you specify that cmd uses con as it's connection. As a result the Command (cmd) has no connection, and con isn't associated with any command at all.
Add this line before executing:
cmd.Connection - con;
Alternatively (and better IMO) change your using statement as follows:
using (SqlCommand cmd = new SqlCommand(sql, con))
How i could check if i have connection to an SQL Server with the Connection String known in C#?
using (var connection = new SqlConnection("connectionString"))
{
try
{
connection.Open();
Console.WriteLine("Connection Ok");
}
catch (SqlException)
{
Console.WriteLine("Connection Not Ok");
}
}
I"m not sure if you are asking how to validate a connection string or check to see if a current connection is open If you are trying to check if a current connection is open you can use.
connection.State
ConnectionState Enumeration Values
You can also test it outside of your code by making an UDL file (a text file with extension .UDL). Then right-click it, get the properties and enter the connectionstring details.
Press the "Test Connection" button and then you'll know.
Check the state of the connection.
if (conexion.State == ConnectionState.Closed)
{
conexion.Open();
}