Connect to SQL Server with JDBC - java

I have this program
SqlConnection myConnection = new SqlConnection("Data Source=IGOR-PC;Initial Catalog=Prueba;Integrated Security=True");
myConnection.Open();
And I connect to this SQL server IGOR-PC (SQL 10.50.4044 - Igor-PC\Igor)
My question is if I could connect to this server with JDBC with c#?
Can I connect to a SQL server with different ways or can only one way?

There are many ways to connect to SQL depending on what kind of security your SQL database have. You can use OLEDB or JDBC ODBC bridge. Weather your SQL server is installed as a named instance or not.
Checkout different ways to connect to SQL Server here : https://www.connectionstrings.com/sql-server-2008/

here is a code that allow you to connect sql server and java :
String url = "jdbc:sqlserver://IGOR-PC:1433;databaseName=Prueba;integratedSecurity=true";
Connection conn = DriverManager.getConnection(url);

Related

Azure SQL database connection issue from an Azure VM by Java JDBC

We have a java project in an Azure virtual machine (VM), and need connect to Azure SQL db by JDBC connection, so we use the JDBC connection string provided by Azure SQL db as follows:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
but we got an exception:
"java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization.
The server name is *.database.windows.net, the name in certificate is cr2.eastus1-a.control.database.windows.net."
then we updated the JDBC connection string to:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=cr2.eastus1-a.control.database.windows.net;loginTimeout=30;"
but we got another exception:
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Cannot open server "ZZZ1" requested by the login. The login failed.)
where "ZZZ1" is our username for the Azure VM.
Any idea to solve this issue?
By default, Azure VM has not blocked the outbound connection to the Internet. You could first verify the network connectivity from your VM to the Azure SQL database via running the command telnet ZZZdbserver.database.windows.net 1433 in CMD or Test-NetConnection -computer ZZZdbserver.database.windows.net -port 1433 in Powershell. If it fails, you may check if there is any firewall blocking this database connection or port or your application is listening on that port or your SQL database is online.
Additionally, if the network connectivity has succeeded, you could verify if the username or password is correct when you are using to connect to your database. Make sure there is not any typo. Also, the username should have enough privileges to access this database. You can try to access this database via Server admin login in the properties of the SQL database. Moreover, you could refer to this sample code to use Java to connect to access the Azure SQL database.
When using the Microsoft JDBC Driver for SQL Server to connect to an Azure SQL Database. You should note this:
Appending the server name to the userId in the connection string
Prior to the 4.0 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you were required to append
the server name to the UserId in the connection string. For example,
user#servername. Beginning in version 4.0 of the Microsoft JDBC Driver
for SQL Server, it's no longer necessary to append #servername to the
UserId in the connection string.
Using encryption requires setting hostNameInCertificate
Prior to the 7.2 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you should specify
hostNameInCertificate if you specify encrypt=true (If the server name
in the connection string is shortName.domainName, set the
hostNameInCertificate property to *.domainName.). This property is
optional as of version 7.2 of the driver.
Hope this helps.
If I need set encrypt=true, and hostNameInCertificate=cr2.eastus1-a.control.database.windows.net. Where do I need get the certificate for cr2.eastus1-a.control.database.windows.net from Azure SQL DB's service?

Connect MSSQL DB as different Windows user in JAVA

I need to establish connection to MSSQL database from java using Windows Authentication as a different user
I getting SQL login failiure because windows authentication is using server username and password. I would like to change those.
this.connectionString = "jdbc:sqlserver://SERVER;DatabaseName=DATABASE;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
this.conn = DriverManager.getConnection(this.url);
this.statement = conn.createStatement();
I expect to log into remote database as a different windows user.
Many thanks in Advance

JDBC connection to hive fails

I'm trying to establish a JDBC connection to Hive so that I can query Hive tables using java API for hive 0.13.1. I've tried connecting to HiveServer1 and HiverServer2 but unable to make connection.
However locally within the box I am able to connect to the server using beeline command following it up with the command:
!connect jdbc://hive2://
once connection is established I am able to view the tables using command:
show tables;
However the same thing when I try from a remote m/c using java api it fails. Here is the code snippet that is failing.
Connection con = DriverManager.getConnection("jdbc:hive2://RHDCN05.rmsi.com:10000", "", "");
Statement stmt = con.createStatement();
Hope somebody can help in figuring out the problem.
Any help would be appriciated!!!

Setting up a connection to Online MySQL database

all of the examples, unfortunately on the net are consumed of downloading a MySQL database to your computer rather than taking advantage of Online Database.
What should I replace the following code to get data from an online db rather than a 3306 typical desktop MySQL;
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME", "usrname", "pswd") ;
Since not too much information was given, we will use an example - let's say the database is located at user3129325.com, and is hosted by a web hosting provider that allows remote SQL. If you wanted to connect to the database named "DBNAME" (using the same name as in your example), you would want to use the following command:
Connection conn = DriverManager.getConnection("jdbc:mysql://user3129325.com:3306/DBNAME", "usrname", "pswd") ;
Just simply replace localhost with the URL of the remote MySQL database.

Connection from a Java program to a SQL Developer database

I would like to access to a Oracle database (SQL Developer) from a Java program. I never used JDBC before.
Here is what i wrote:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:host_name:port:database_name";
Connection con = DriverManager.getConnection(url, login, passwd);
I got an error:
[Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified
Host name, port, DB name and logins are good.
Is this driver OK to communicate with SQL Developer ?
I don't know what to do,
thanks for helping !
Try this
Class.forName ("oracle.jdbc.driver.OracleDriver");
for Oracle you can use ojdbc
Class.forName("oracle.jdbc.driver.OracleDriver");
for SQL Server u can use jtds
Class.forName("net.sourceforge.jtds.jdbc.Driver");
The JDBC driver sun.jdbc.odbc.JdbcOdbcDriver is bridge driver that wraps an ODBC driver as described here.
SQL Developer is an Oracle tool that acts as an IDE against the Oracle database.
To connect Java to an Oracle database you should obtain the Oracle JDBC driver and ensure the jar is on your classpath (as described in the documentation for java.sql.DriverManager, forcing the class to be loaded is no longer necessary).
The important bit is the connection string, which in its simplest form for Oracle should follow the structure:
jdbc:oracle:thin:#//host:port/service
Where:
host: the hostname of the machine running Oracle
port: the port that Oracle is listening for connections on
service: the database instance to connect to
The full docs are here.

Categories

Resources