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!!!
Related
I am trying to connect this small Java program to my MySQL Database instance
on GCP for educational purposes.
This code works well with the instance I have locally on localhost and port 3306.
However, this is not very practical or useful.
Scenarios that work:
Connecting Java client program to MySQL database served locally.
Connecting MySQL Workbench client to MySQL database served locally.
Connecting MySQL Workbench client to MySQL database served on Google Cloud Platform.
My objective:
Connect very simple Java client program to MySQL database served on Google Cloud Platform.
Jar files used:
mysql-connector-java-8.0.29.jar
mysql-socket-factory-1.6.1-jar-with-dependencies.jar
IDE used:
Eclipse
The code:
package test;
import java.sql.*;
public class Test
{
public static void main(String[] args)
{
String CREDENTIALS = "jdbc:mysql://google/emexdb?cloudSqlInstance=ethereal-shine-354205:us-central1:emexdb-mysql&socketFactory=google.cloud.sql.mysql.SocketFactory&useSSL=false&user=****&password=***********";
try
{
//Get a connection to database
Connection myConn = DriverManager.getConnection(CREDENTIALS);
//Create a statement
Statement myStmt = myConn.createStatement();
//Execute SQL query
ResultSet myRs = myStmt.executeQuery("select * from Test;");
//Process the result set
while(myRs.next())
{
System.out.println(myRs.getNString("id")+" "+myRs.getString("_name"));
}
myConn.close();
}
catch(Exception eSQL)
{
eSQL.printStackTrace();
}
}
}
This is the error message Eclipse is giving me:
java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on google:3,306.
Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:462)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:253)
at test.Test.main(Test.java:12)
Caused by: java.lang.NullPointerException: Cannot invoke "com.mysql.cj.protocol.a.NativeProtocol.getSocketConnection()" because the return value of "com.mysql.cj.NativeSession.getProtocol()" is null
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:972)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
... 5 more
Any help would be much appreciated.
Thank you!
Have you whitelisted your public IP in the settings of your GCP MySQL database?
You can do that here:
Your GCP Project ⇾
SQL (Press the three horizontal bars in the left corner) ⇾
Connections ⇾
Authorised networks ⇾ ADD NETWORK
You can use this Website to get your public IP.
I'm trying to connect with hive via jdbc but it gives me the following exception
java.sql.SQLException: org.apache.thrift.transport.TTransportException
I already have running hadoop and derby. By the console of hive (in linux) I can work but once I try it for the project in java it does not work
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
Connection co = DriverManager.getConnection("jdbc:hive://localhost:9000/almacendb", "","");
I am trying to write a Java desktop app that can connect to my database made with Microsoft SQL Server Manager to allow me to view and update it. But, I am having trouble getting the connection to work. I've read through a bunch of tutorials and threads here on Stack Exchange of similar problems, and I'm not sure what I'm doing wrong.
The server is called "SQLEXPRESS" using Windows authentication. I downloaded the JDBC driver found here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 installed it in NetBeans by going to "Services-Databases(right click)-New Connection-Add", but I also added it as a library in my project.
When I try this code, I get the exception that the TCP/IP connection failed either because the server isn't running or port 1433 is locked:
try{
String
URL="jdbc:sqlserver://sqlexpress:1433;DatabaseName=GreenhouseManagement";
Connection conn = DriverManager.getConnection(URL,"","");
System.out.println("connected");
}catch(Exception e){
System.out.println("Oops\n"+e);
}
What do I need to change to fix this?
You might need to reconfigure your connection string into this format.
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE
HOST in this case is most likely to be "localhost" since you are connecting on a local machine.
DATABASE will be the name of your database
Reference: http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver
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);
I wrote a simple code to connect to SQL Server database and to does a simple select statement, this code runs successfully on my local machine over VPN but when I deploy the same code in the servers, then my code is stopping at getting the DB connection object.
It is not even throwing any timeout exception, so I thought of a port issue and checked using telnet which is successful. Can anyone please suggest why this might happen? I am able to get connection object of the same DB from my local machine over VPN.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = props.getProperty("DBURI");
Connection conn=null;
System.out.println(connectionUrl.trim());
conn = DriverManager.getConnection(connectionUrl.trim());
Problem with the sqljdbc jar file , Used any 32 bit one , After replacing the jar , everything worked fine. Thanks