I've got an Azure SQL Server database that I'm connecting to via JDBC, but want to connect instead to my SQL Server "localhost". In SSMS, I connect to localhost without needing a password. So, do I still need to enter a password in Java?
I have a code like this :
String connectionUrl =
"jdbc:sqlserver://etcetc.database.windows.net:1433;"
+ "database=med;"
+ "user=windersan#salemimed;"
+ "password=********;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
// + "hostNameInCertificate=*.database.windows.net;"
+ "loginTimeout=30;";
How do I change this to connect instead to localhost?
Just replace the etcetc.database.windows.net by localhost and replace the port number 1433 by the number that you are using.
I have used SQLServerDataSource class to make the work easier. You can also create a string URL and set it in the DriverManger.getConnection().
Try with this code :
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setUser("windersan#salemimed");
dataSource.setPassword("********");
dataSource.setServerName("localhost");
// set the port number of your system below.
dataSource.setPortNumber(1433);
dataSource.setDatabaseName("med");
dataSource.setEncrypt(true);
dataSource.setHostNameInCertificate("*.database.windows.net");
dataSource.setTrustServerCertificate(false);
Connection connection = dataSource.getConnection();
Please refer to this links down below for more info.
Microsoft Docs - ISQLServerDataSource Interface - This contains the list of methods that you can use to set the various properties in the datasource.
Microsoft Docs - How to work with the connection - This contains examples of the possible ways to connect to a SQL Server database.
the first line of your concatenated string contains the url etcetc.database.windows.net:1433 this is the location of the database server, and the bit you should change.
Also, it might be worth doing a google search on connecting to SqlServer with JDBC to see if there are any examples out there.
Related
I have some code that connects to a database using JDBC using a basic URL:
String url = "jdbc:mysql://" + getHostname() + ":" + getPort();
I am using the following arguments:
setProperty("connectTimeout", "2000");
setProperty("autoReconnect", "true");
I don't include the database name, since it might not exist yet. I check whether one exist upon connection, and create it as needed. Then I select the database.
I use two queries:
CREATE DATABASE IF NOT EXISTS <name>
then
USE <name>
So the database is selected here upon connection.
The issue I'm having is that I saw an error in the log:
...
Caused by: java.sql.SQLException: No database selected
This doesn't make sense, because the database is always selected upon connection, otherwise an exception would have been thrown. However, this exception about a missing selection occurs hours later.
My guess is that, upon reconnecting, it uses the URL, which doesn't include the database name. So it reconnects, but now the database isn't selected.
Can someone confirm whether this is the cause of the behavior I am experiencing?
Yup, hence, don't connect without a database name.
Try to connect with the name. In the catch block, check if this is the problem (SQLException has a .getState() method that returns a specific error code, check it against mysql's table, or just run the code passing a DB URL with a non-existent dbname and just print it out, now you know) - and if so, run a method that will create a new connection (without a dbname), creates the database, closes that connection, and returns. Then you retry with the database name. Remember, exceptions can be caught.
String url = "jdbc:mysql://" + getHostname() + ":" + getPort();
You should include your database name. Your URL followed by a "/DBNAME".
USE database is for subsequent statements. The named database remains the default until the end of the session. Your session expires.
If you create the database on the fly, you can connect again using the complete URL with the database name at the end, not with USE database
There is an option createDatabaseIfNotExist exactly to do that.
Connector will then connect to server without database, create database is not existing and connect to that database.
database must then be explicitly set in connection string, like
"jdbc:mysql://myHost:3306/myDB?createDatabaseIfNotExist=true"
The advantage is that if connection fails, autoreconnect will connect to the database directly.
I have been trying to connect to this Oracle database with JDBC thin driver with the following syntax:
var URL = "jdbc:oracle:thin:#//16.161.286.56:1522/Service_Name";
var USER = "user";
var PASS = "password";
var conn = Jdbc.getConnection(URL, USER, PASS);
I keep on having the same response when I execute this:
We're sorry, a server error occurred. Please wait a bit and try again. [87a99af]
Would anyone have an idea of what I am doing wrong ?
Also, when I change the IP address to its 'string version':
var URL = "jdbc:oracle:thin:#//mydomain.com:1522/Service_Name";
Then I get the error response:
Failed to establish a database connection. Check connection string, username and password.
Which does not make sense to me as both expressions are supposed to be equivalent ...
The actual format of Oracle JDBC connection using service name is:
#//host_name:port_number/service_name
Make sure that is service name is ok.
You can try with TNSNameListener
jdbc:oracle:thin:#(description=(address=(host=<HOSTADDRESS>)(protocol=tcp)(port=<PORT>))(connect_data=(service_name=<SERVICENAME>)(server=<SHARED>)))
The TNSNameListener file location:
<ORACLE_HOME>\network\admin\tnsnames.ora
example: /home/oracle/oracle/product/10.2.0/db_1/network/admin/tnsnames.ora
I am not sure which class is that Jdbc is. Take a look at the JDBCUrlSample.java and DataSourceSample.java
I’m trying to connect to a MySQL database on my website from java.
Currently I’m getting a exception that says
Must specify port number after:”
I Google stack overflow and found the default MySQL port is 3306.
But I can't find any information about how I add it to my url, which now looks like
jdbc:mysql://http://www.findmeontheweb.biz/database name"+
“user=findmeon_bitcoin&password=password
code:
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
Connection connect = DriverManager.getConnection("jdbc:mysql://http: //www.findmeontheweb.biz//findmeon_bitcoin//"+ "user=findmeon_bitcoin&password=oreo8157");
} catch (Exception e) {
System.out.println("Exception...." );
}
1) Hope the space in your code was only a copy/paste error here
2) You need to remove the http in the mysql uri
Connection connect = DriverManager.getConnection("jdbc:mysql://http: //www.findmeontheweb.biz//findmeon_bitcoin//"+ "user=findmeon_bitcoin&password=oreo8157");
will be
Connection connect = DriverManager.getConnection("jdbc:mysql://findmeontheweb.biz/findmeon_bitcoin/"+ "user=findmeon_bitcoin&password=oreo8157");
And just in case you are running it on a custom port, you can specify port by
Connection connect = DriverManager.getConnection("jdbc:mysql://findmeontheweb.biz:3306/findmeon_bitcoin/"+ "user=findmeon_bitcoin&password=oreo8157");
replace 3306 with your custom port.
Also I hope that is not your real username and password!
Last time I used SQL with Java/Eclipse I had a SQL script linked to the Project.
This time, I connected to a different server using jdbc format (not localhost) and it connects correctly.
Now I need to get into this specific database named WInfo but I don't know how to do that.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
c = DriverManager.getConnection("jdbc:sqlserver://serverName:1433/;user=UserName;password=******;");
Use below connection string:
c = DriverManager.getConnection("jdbc:sqlserver://SERVERNAME:PORTNO;databaseName=DATABASENAME;
user=MyUserName;password=*****");
OR
c = DriverManager.getConnection("jdbc:sqlserver://SERVERNAME:PORTNO;databaseName=DATABASENAME",
MyUserName, MyPassword);
I am new to Oracle connection manager. Can some help me with a Java Client code example to talk to a oracle database through Oracle connection manager.
Quote from oracle docs.
The Web server on which the Connection Manager is running is on host webHost and is listening on port 1610. The database to which you want to connect is running on host oraHost, listening on port 1521, and SID ORCL. You write the URL in TNS keyword-value format:
String myURL =
"jdbc:oracle:thin:#(description=(address_list=
(address=(protocol=tcp)(port=1610)(host=webHost))
(address=(protocol=tcp)(port=1521)(host=oraHost)))
(connect_data=(INSTANCE_NAME=orcl))
(source_route=yes))";
OracleDataSource ods = new OracleDataSource();
ods.setURL(myURL);
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
The first element in the address_list entry represents the connection to the Connection Manager. The second element represents the database to which you want to connect. The order in which you list the addresses is important.
When your applet uses a URL such as the one above, it will behave exactly as if it were connected directly to the database on the host oraHost.