How to connect to Apache Hive Using SSL certificate from Java JDBC? - java

I am currently trying to make a simple connection between a Java class and an Apache Hive server.
However, I have not been able to do it due to a javax.net.ssl.SSLPeerUnverifiedException error.
This is the Java code that I am executing:
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
Connection con = null;
try {
String conStr = "jdbc:hive2://{{domain}}:{{port}}/default;ssl=true;sslTrustStore=C:/DBVisualization/gateway.jks;trustStorePassword=Password1;transportMode=http;httpPath=/gateway/default/hive";
Class.forName("org.apache.hive.jdbc.HiveDriver");
con = DriverManager.getConnection(conStr, "user", "password");
if(con != null)
System.out.println("Connected");
} catch (Exception ex) {
Logger.getAnonymousLogger().severe("There was an error while connecting to Hive");
}
}
}
And this is the error I am getting:
Could not open client transport with JDBC Uri: jdbc:hive2:{{url}}:{{port}}/default;ssl=true;sslTrustStore=C:/DBVisualization/cacerts;trustStorePassword=changeit;transportMode=http;httpPath=/gateway/default/hive: Could not establish connection to jdbc:hive2://{{url}}:{{port}}/default;ssl=true;sslTrustStore=C:/DBVisualization/cacerts;trustStorePassword=changeit;transportMode=http;httpPath=/gateway/default/hive: javax.net.ssl.SSLPeerUnverifiedException: Certificate for <IP> doesn't match any of the subject alternative names: []
As you can see I am pointing to the file where the certificate is, I used that same certificate for set up a DB visualizer connection and it worked. I am just do not know if for Java a different certificate is required. What am I doing wrong?

Related

Connect to SQL server from Java

I'm trying to connect from SSMS(SQL Server Management Studio) to Eclipse(Java), but I keep getting this error message:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:6132)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2609)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2346)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:16)
The code I do have is:
import java.sql.DriverManager;
import java.sql.SQLException;
//import com.sun.jdi.connect.spi.Connection;
public class JavaConnect2SQL {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=students"; // This is where I think the error is
String user = "sa";
String password = "123";
try {
java.sql.Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Succesfully connected to Microsoft SQL Server");
}catch (SQLException e) {
System.out.println("Oops! There was an error: ");
e.printStackTrace();
}
}
}
I think the problem is in the connection URL, but I don't know the host name/instance name. If you can tell me how to get my host name/instance name, that would be appreciated. But if the problem is something else, please tell me!
Edit: I have changed the code, and I'm now getting a different. Here is the new code:
package net.codejava.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JavaConnect2SQL {
public static void main(String[] args) {
// Create a variable for the connection string.
String url = " jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students";
String user= "sa";
String password = "123";
try {
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Connection succesful!");
}
// Handle any errors that may have occurred.
catch (SQLException e) {
System.out.println("Here is your error message: ");
e.printStackTrace();
}
}
}
The new error message is this:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:40)
I think the problem is that I installed the wrong version of JDBC Driver. My JRE version is 12.0.1. What is the correct JDBC Driver to install.
You can check in SQL server configuration manager and Microsoft SQL server management studio. The state must be running in server configuration manager and you can try to connect your database in Microsoft SQL server management with your password.
if you connect successfully then there is another problem but it looks like the database is stopped.

Connect to RServe from JAVA using authentication

I am running RServe from Server machine using cmd
Rserve.exe --RS-conf Rserv.conf --RS-port 12306
Rserv.conf file has following content:
pwdfile RserveAuth.txt
auth required
remote enable
plaintext disable
RserveAuth.txt has following contents:
Admin 123456
I am connecting to R Server from JAVA
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.*;
public class ConnecttoR
{
...
...
public void connectR()
{
try
{
RConnection connection = new RConnection("172.16.33.242",12306); // Works if authentication is not required in Rserv.conf
}
catch (RserveException e) {
e.printStackTrace();
}
catch(REXPMismatchException e){
e.printStackTrace();
}
catch(REngineException e){
e.printStackTrace();
}
}
}
Connection to Rserve is open to all without username & Password. How shall I add security and allow connection only with valid credentials to access Rserve
As you have enabled the authentification after creating the connection as a first command you need to execute the login command. The Java library has a special wrapper for it.
See code below for example use case.
RConnection connection = new RConnection("127.0.0.1",12306);
connection.login("Admin", "123456");
REXP x = connection.eval("R.version.string");
System.out.println(x.asString());
Also, I would recommend using full path as the pwdfile value.

Connecting to SQL Server 2014 from Android Studio

I have a problem connecting to SQL-server database through from my android project. I have added sqljdbc41.jar file to my /app/libs directory and I have added it to dependencies in my android studio project.
I use following code:
package com.konrad.rezerwacje1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Database_Console {
public static void openConnection(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"‌​);
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
Connection con = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
openConnection();
}
}
yet i still get this error
java.sql.SQLException: No suitable driver found for jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
Instead of this :
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"‌​);
String url = "jbdc:sqlserver://127.0.0.1:1433;databaseName=my_db";
You have to use this :
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=my_db";
Connection con = DriverManager.getConnection(url, "username", "password");
Note the different classname, and the fact that prefix jbdc in the URL has been changed to jdbc.
If it is not a requirement to go with sqljdbc41.jar, then you might consider using the jtds driver for your requirement to connect to SQL Server 2014 with Android Studio. There are tons of articles that can help you start with this set of technologies.
For a primer, here are the details:
Download the JTDS driver from here
Then import this jar into your Android Studio, eg: jtds-1.2.5.jar
Use the following details in your code:
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/DATABASE;user=sa;password=p#ssw0rd");

Java console application connection with mysql database

I am creating a java console application that connects to a MySQL database. I have outlined below what I've done but the connection is not being established:
Things I have done:
1) I have added the mysql-connector-java-5.1.36.jar to my project bulid path.
2) I have written the code that tries to establish a connection
Can anyone suggest what I'm doing wrong?
The code is given below:
import java.sql.Connection;
import java.sql.DriverManager;
public class main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1234/first","root","");
System.out.println("Success fully Connected");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

Java connection with sql server 2008 r2 type 4

Hello iam using eclipse and sql server 2008 r2 as database i get a error like this
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host RITHISHABINAV, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.serv.sqlservertype4.fun(sqlservertype4.java:17)
at com.serv.sqlservertype4.main(sqlservertype4.java:39)
And my code is this
package com.serv;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class sqlservertype4 {
void fun()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl="jdbc:sqlserver://RITHISHABINAV\\SQLEXPRESS;database=tempdatabase;integratedSecurity=true;";
Connection conn=DriverManager.getConnection(connectionUrl);
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * from customer");
while(rs.next())
{
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
sqlservertype4 b= new sqlservertype4();
b.fun();
}
}
I think that you have a wrong connectionURL, you need to create a vURL` like:
jdbc:sqlserver://HOST_SERVER:SQL_SERVER_PORT;databaseName=DATABASE_NAME;
But If you are using Named Instances to connect to your database (instead of PORT numbers), then use the following URL template
jdbc:sqlserver://DATABASE_HOST\\INSTANCE_NAME;databaseName=DATABASE_NAME;

Categories

Resources