JDBC's connection strings with CockroachDB? - cockroachdb

How do I connect to CockroachDB in Java using JDBC, e.g. what are the parameters and format of the connection string?

You can see a full example of Cockroach using jdbc here, but here's the code snippet including the connection string:
import java.sql.*;
public static void main(String[] args)...{
Class.forName("org.postgresql.Driver");
Connection db = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:26257/[your database]?sslmode=disable", "maxroach", "");

Related

How to set JDBC/JDBI connection for ClickHouse to be readonly

Tried following methods and none worked.
Calling setReadOnly() on connection or jdbi handle. setReadonly method is a no-op in jdbc client.
Executing "SET readonly = 1" before other queries using the same jdbi handle. BTW, "SET readonly=1" works as expected in clickhouse-cient cli.
I can use a user that only has readonly permissions but in my set up I need to execute write queries as well and don't want to maintain multiple user credentials. The need for setting readonly arises because the queries that are executed are from 3rd party.
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.2-patch3</version>
</dependency>
import com.clickhouse.client.ClickHouseRequest;
import com.clickhouse.jdbc.ClickHouseDataSource;
import com.clickhouse.jdbc.ClickHouseConnection;
import com.clickhouse.jdbc.ClickHouseStatement;
public class Main {
public static void main(String[] args) throws Exception {
String chURL = "jdbc:clickhouse://127.0.0.1/default";
ClickHouseDataSource dataSource = new ClickHouseDataSource(chURL);
ClickHouseConnection connection = dataSource.getConnection();
ClickHouseStatement stmt = connection.createStatement();
ClickHouseRequest<?> req = stmt.unwrap(ClickHouseRequest.class);
req.set("readonly", 1);
stmt.executeQuery("drop table if exists aaaa");
}
}
Exception in thread "main" java.sql.SQLException: Code: 164.
DB::Exception: default: Cannot execute query in readonly mode. (READONLY) (version 22.1.3.7 (official build))

Oracle Database JDBC driver cannot read wallet file from Spark

Objective
I'm trying to write to Oracle's ADWC (basically oracle database) from a Spark application running on Yarn. The only way to connect to this database is by using an Oracle Wallet file, which is basically a Java keystore.
Problem
The problem arises when the JDBC driver tries to read the wallet from HDFS. If I include the hdfs:// prefix the parser in the JDBC driver throws an error and if I don't then it cannot find the file.
Previous Attempts
including the directory in the connect string (prefixed and non) jdbc:oracle:thin:#luigi_low?TNS_ADMIN=/user/spark/wallet_LUIGI
including the directory as an spark.driver.extraJavaOptions with -Doracle.net.tns_admin and -Doracle.net.wallet_location
All the code is on GitHub, and specifically, the error messages are here https://github.com/sblack4/kafka-scala-jdbc/blob/master/ERROR.md
I've got a working example of the same connection here https://github.com/sblack4/scala-jdbc-adwc
help me StackOverflow. you are my only hope
If you need any more clarification don't hesitate :)
update (SparkFiles attempt)
the code is on a separate branch of the same repository, https://github.com/sblack4/kafka-scala-jdbc/tree/sparkfiles
This error message mystifies me as it seems my JDBC library has stopped trying to read the wallet files. It may be unrelated to the previous problem
Exception in thread "main" java.sql.SQLRecoverableException: IO Error: Invalid connection string format, a valid format is: "host:port:sid"
I've deleted the other JDBC libraries from my classpath through Ambari as this error could be related to spark picking up an older version of my JDBC library
Here's some code that will help diagnose what the issues is.
It checks and configures everything required to connect.
JDBC Driver version
JCE Installed
Classpath dependencies
Configures
tns_admin
ssl settings
trust/key stores
This is a slimmed down version of what's in sqldev/sqlcl
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Properties;
import javax.crypto.Cipher;
import oracle.jdbc.OracleConnection;
public class JDBCTest {
public static void fail(String msg){
System.err.println(String.join("", Collections.nCopies(20, "*")));
System.err.println(msg);
System.err.println(String.join("", Collections.nCopies(20, "*")));
System.exit(1);
}
public static void main(String[] args) throws SQLException {
System.out.println("JDBC Driver Version:" + oracle.jdbc.OracleDriver.getDriverVersion());
// Check JDBC Driver Version
if (!oracle.jdbc.OracleDriver.getDriverVersion().startsWith("18.")) {
fail(" DRIVER TOOO OLD!!!");
}
// Check JCE Installed
int maxKeySize = 0;
try {
maxKeySize = Cipher.getMaxAllowedKeyLength("AES");
} catch (NoSuchAlgorithmException e) {
}
if (maxKeySize < 129 ) {
fail(" JCE Policy not unlimited!!!");
}
// Check Classpath
String cp = System.getProperty("java.class.path");
String[] cpFiles = {"ojdbc8.jar","oraclepki.jar","osdt_cert.jar","osdt_core.jar"};
for (String file:cpFiles){
if ( cp.indexOf(file) == -1 ){
fail("CLASSPATH Missing:" + file);
}
}
// Wallet unziped location
String unzippedWalletLocation = "/Users/klrice/workspace/12.2JDBC/wallet";
String conString = "jdbc:oracle:thin:#sqldev_medium";
Properties props = new Properties();
props.setProperty("oracle.net.wallet_location",unzippedWalletLocation);
props.setProperty(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CONNECT_TIMEOUT, "2000");
// unzipped includes a tnsnames.ora
props.setProperty("oracle.net.tns_admin",unzippedWalletLocation);
props.setProperty("javax.net.ssl.trustStore","truststore.jks");
props.setProperty("javax.net.ssl.trustStorePassword","<password>");
props.setProperty("javax.net.ssl.keyStore","keystore.jks");
props.setProperty("javax.net.ssl.keyStorePassword","<password>");
props.setProperty("oracle.net.ssl_server_dn_match","true");
props.setProperty("oracle.net.ssl_version","1.2");
props.setProperty("user", "ADMIN");
props.setProperty("password", "<password>");
try {
// now Connect
Connection conn = DriverManager.getConnection(conString,props);
} catch (Exception e){
e.printStackTrace();
fail(e.getLocalizedMessage());
}
System.out.println("SUCCESS!!");
}
}
Are you using 18.3 JDBC drivers? Passing TNS_ADMIN as part of the connection URL requires 18.3 JDBC driver. Also, are you attempting to connect within the corporate network. In that case, you will need to pass HTTPS_PROXY and HTTPS_PROXY_PORT in the connection URL. Let us know. Happy to help with the problem.

phoenix jdbc doesn't work, no exceptions and stuck

I'm new to phoenix and hbase.hbase table and phoenix view works well and i can fetch data through phoenix. when I access jdbc to phoenix,it stucks.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Phoenix {
private static String driver = "org.apache.phoenix.jdbc.PhoenixDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Statement stmt = null;
ResultSet rs = null;
System.out.println("start...");
Connection con = DriverManager.getConnection("jdbc:phoenix:[my_cloud_server_ip]:2181");
System.out.println(con);
con.close();
}
}
(only one zookeeper server has public internet ip,so i write this ip there,does it matters?)
it prints "start..." and no response any more
BUT when when the url is "jdbc:phoenix:ip:2181" or "jdbc:phoenix:ip:2181/hbase"
i got no response
when i add some other words ,for example "jdbc:phoenix:ip:2181/balabala"
i got NULL POINTER EXCEPTION
why ?
I hope u can understand what i say:)
Reproducing your example, I am getting a java.net.SocketTimeoutException after 60 seconds, caused by java.net.UnknownHostException: unknown host: <hostname_of_my_zk_server>. But maybe that's what you called "no response" if you were in a hurry (or if you have a custom hbase-site.xml with a larger timeout client-side).
This error seems like the one explained in this article (section Zookeeper at the end) :
As in our case CDH was running in a test VM, we encountered this issue: http://stackoverflow.com/questions/18428722/hbase-java-client-unknown-host-localhost-localdomain
That was worked around by adding localhost.localdomain to the existing /etc/hosts entry for cluster1, which was already pointing to the right IP address.
This answer on SO summarizes the solution.
Basically, you need to add an entry to your /etc/hosts client-side :
<my_cloud_server_ip> <hostname_of_my_cloud_server_ip>
Moreover, you need to have an hbase-site.xml client-side (you can use the one on your server as a basis).

Connecting to Google cloud sql from Java stand alone application

I want to write a java stand alone application to connect to google cloud sql - MySql database. I could find samples for app client but not for a stand alone java application.
When I tried to do this, I get the following error.
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:google:mysql://IP:Instance_name?user=user_name");
Statement st = con.createStatement();
ResultSet rs = con.createStatement().executeQuery("SELECT 1 + 1");
It fails with error:
No suitable driver found for jdbc:google:mysql:..
I see some answers where they have asked to enabled mysql connector. But it is the case with app engine project. How do I do it with stand alone application?
You can just use the standard mysql connector to connect to cloud sql instance: e.g:
DriverManager.getConnection("jdbc:mysql://IP:Instance_name?user=user_name");
You can check this link https://cloud.google.com/sql/docs/external for more information.
We can simply use the standard mysql java connector to connect to cloud sql form a standalone java application.
Follow the below code snippet. This should help you to connect to database seamlessly.
class.forName(com.mysql.jdbc.Driver);
String connectionUrl = "jdbc:mysql//<public-ip>/<database>?useSSL=false";
Connection conn = DriverManager.getConnection(connectionUrl, user, password);
Before you do this make sure you get your ipaddress from internet and add the same under the path : google cloud -> sql -> sqlinstance -> connections -> networking -> Authorized networks.
Click here to see the google cloud mysql configuration
Edit inside <> before running your code
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.Statement;
/**
* A sample app that connects to a Cloud SQL instance and lists all available tables
in a database.
*/
public class Cloud_sql {
public static void main(String[] args) throws SQLNonTransientConnectionException
,IOException, SQLException {
String instanceConnectionName = <Your instanceConnectionName>;
String databaseName = <Database name from which u want to list tables>;
String IP_of_instance = <IP address of the instance>;
String username = <mysql username>;
String password = <mysql password>;
String jdbcUrl = String.format(
"jdbc:mysql://%s/%s?cloudSqlInstance=%s"
+ "&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
IP_of_instance,
databaseName,
instanceConnectionName);
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("SHOW TABLES");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}catch(Exception e){
e.printStackTrace();
}
}
}

Spring JDBC connection without dataSource

I had read several articles on obtaining Connection using Spring DataSource.
But in our Company Setup, connection object is obtained through already configured environment. Following the sample code:
String pool = PropertyFileReader.getPropertyValue("database.properties", "development.connectionPool");
Connection connection = RequestUtils.getConnection(pool);
Hence, After reading this tutorial
I am confused on using JDBCTemplate using connection object from above code.
I believe JdbcTemplate is not designed to work against a Connection as what you expected. As a workaround, if you are fine to create a separate JdbcTemplate for each connection you created, you may wrap your connection in a thin wrapper of DataSource, and feed it to JdbcTemplate.
I think it should work but I haven't tried it anyway...
class SingleConnectionDataSource implements DataSource {
private Connection connection;
public SingleConnectionDataSource(Connection connection) {
this.connection = connection;
}
public Connection getConnection() {
return this.connection;
}
public Connection getConnection(String username, String password) {
return this.connection;
}
}
// at the place you want to use JdbcTemplate
Connection conn = blablabla; // your own way to get it
JdbcTemplate jdbcTemplate = new JdbcTemplate(new SingleConnectionDataSource(conn));
Actually, Spring already provided SingleConnectionDataSource implementation (have seen in version 4.1.7).
It is even allows you to supress connection closing by template.

Resources