Cannot connect to Redshift database with a driver even though play.ap.db.DB can do this for the same driver - database

I am trying to connect to a redshift server and run some sql commands. Here is the code that I have written:
Class.forName("org.postgresql.Driver")
val url: String = s"jdbc:postgres://${user}:${password}#${host}:${port}/${database}"
val connection: Connection = DriverManager.getConnection(url, user, password)
val statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
val setSearchPathQuery: String = s"set search_path to '${schema}';"
statement.execute(setSearchPathQuery)
But I am getting the following error:
java.sql.SQLException: No suitable driver found for jdbc:postgres://user:password#host:port/database
But when I am using play framework's default database library with the same configuration, then I am able to connect to database successfully. Below is the configuration for the default database:
db.default.driver=org.postgresql.Driver
db.default.url="postgres://username:password#hostname:port/database"
db.default.host="hostname"
db.default.port="port"
db.default.dbname = "database"
db.default.user = "username"
db.default.password = "password"

The problem was with the url. The correct format for the url is:
jdbc:postgresql://hostname:port/database

Related

How to connect MS SQL Database with Azure Databricks and run command

I want to connect Azure MS SQL Database with Azure Databricks via python spark. I could do this with pushdown_query if I run Select * from.... But I need to run ALTER DATABASE to scale up/down.
I must change this part
spark.read.jdbc(url=jdbcUrl, table=pushdown_query, properties=connectionProperties)
otherwise I get this error Incorrect syntax near the keyword 'ALTER'.
Anyone can help me. Much appreciated.
jdbcHostname = "xxx.database.windows.net"
jdbcDatabase = "abc"
jdbcPort = 1433
jdbcUrl = "jdbc:sqlserver://{0}:{1};database={2}".format(jdbcHostname, jdbcPort, jdbcDatabase)
connectionProperties = {
"user" : "..............",
"password" : "............",
"driver" : "com.microsoft.sqlserver.jdbc.SQLServerDriver"
}
pushdown_query = "(ALTER DATABASE [DBNAME] MODIFY (SERVICE_OBJECTIVE = 'S0')) dual_down"
df = spark.read.jdbc(url=jdbcUrl, table=pushdown_query, properties=connectionProperties)
display(df)

How to execute a command: SET IDENTITY_INSERT <table> ON on SQL Server table from Spark/Databricks?

I have been able to read/write from Databricks into SQL Server table using JDBC driver. However this time I have to execute a command before I write to a SQL Server.
I need to execute this command on SQL server: SET IDENTITY_INSERT <sqlserver_table_name> ON
How to do this from Databricks ? Any help/pointers are appreciated. Thanks.
You can't do this with the JDBC Spark Connector (or the SQL Server Spark Connector), but it's trivial when using JDBC directly in Scala or Java. When using JDBC directly you have explicit control of the session, and you can issue multiple batches in the same session, or multiple statements in the same batch. EG
%scala
import java.util.Properties
import java.sql.DriverManager
val jdbcUsername = dbutils.secrets.get(scope = "kv", key = "sqluser")
val jdbcPassword = dbutils.secrets.get(scope = "kv", key = "sqlpassword")
val driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
// Create the JDBC URL without passing in the user and password parameters.
val jdbcUrl = s"jdbc:sqlserver://xxxxxx.database.windows.net:1433; . . ."
val connection = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword)
val stmt = connection.createStatement()
val sql = """
SET IDENTITY_INSERT <sqlserver_table_name> ON
"""
stmt.execute(sql)
//run additional batches here with IDENTITY_INSERT ON
connection.close()
And you can always use the Spark Connector to load a staging table, then use JDBC to run a stored procedure or ad-hoc SQL batch to load the staging data into the target table.

How to connect to Local MS SQL Express Edition using Python 3

I am trying to connect to local MS SQL Express Edition. I am using canopy for Python editing.
Code:
import pymssql
conn = pymssql.connect(server='******\SQLEXPRESS',user = 'MEA\*****',password='*****',database='BSEG')
cursor = conn.cursor()
cursor.execute('SELECT * FROM Table')
print(cursor.fetchone())
conn.close()
Error::
pymssql.pyx in pymssql.connect (pymssql.c:10734)()
_mssql.pyx in _mssql.connect (_mssql.c:21821)()
_mssql.pyx in _mssql.MSSQLConnection.init (_mssql.c:5917)()
ValueError: too many values to unpack (expected 2)
user = 'MEA\*****',password='*****'
MEA\***** seems to be Windows login, in this case you shouldn't pass in any password, your user name is enough, but you also should use Integrated security or Trusted parameter in your connection string
It should be smth like this:
server='******\SQLEXPRESS',Trusted_Connection=yes,database='BSEG'

Coldfusion 9 DSN less Connection MSSQL 2008 Windows Authentication

Update:
Though the original question asked about windows authentication, any connection method would be fine. As long as it does not require creating a DSN.
I'm trying to connect my database with Windows Authentication. Here is the code.
Please advise. Thanks.
<cfscript>
classLoader = createObject("java", "java.lang.Class");
classLoader.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dm = createObject("java","java.sql.DriverManager");
dburl = "jdbc:sqlserver://192.168.3.150:1433;databasename=RsDB;integratedSecurity=true";
con = dm.getConnection(dburl,"", "");
st = con.createStatement();
rs = st.ExecuteQuery("SELECT top 10 * FROM pa (nolock)");
q = createObject("java", "coldfusion.sql.QueryTable").init(rs);
</cfscript>
<cfoutput query="q">
email = #email#<br />
</cfoutput>
ERROR :
Error Occurred While Processing Request
This driver is not configured for integrated authentication.
(Ignoring your question for a moment, ... )
While it is possible to do it with a manual connection, is there a specific reason you cannot just use a standard DSN? That would be far more efficient and require a lot less code/cleanup. Not to mention that it avoids some of the common pitfalls of manual connections.
AFAIK, you can set the DSN to use windows authentication, by entering AuthenticationMethod=Type2 under "Show Advanced Settings > Connection String". Leave the "username" and "password" fields blank.
<cfquery name="qTest" datasource="YourDSN">
SELECT SYSTEM_USER AS LoginName, DB_NAME() AS DatabaseName
</cfquery>
<cfdump var="#qTest#" label="Show Connection Settings">
*NB: The DSN will use the CF Service user account, so adjust as needed
Update:
If you absolutely must use a manual connection, try using the built in driver instead of the JDBCODBC bridge. However, be sure to always close the database objects. Otherwise, open connections will start to pile up and bad things will happen.
Keep in mind, opening database connections is expensive. By not using a DSN, you lose out on the benefits of connection pooling. So overall this method will be less efficient than simply using a DSN.
<cfscript>
classLoader = createObject("java", "java.lang.Class");
classLoader.forName("macromedia.jdbc.MacromediaDriver");
dm = createObject("java","java.sql.DriverManager");
dburl = "jdbc:macromedia:sqlserver://127.0.0.1:1433;databasename=RsDB;AuthenticationMethod=Type2";
con = dm.getConnection(dburl);
st = con.createStatement();
rs = st.ExecuteQuery("SELECT getDate() AS TestValue");
q = createObject("java", "coldfusion.sql.QueryTable").init(rs);
// ALWAYS close all objects!
rs.close();
st.close();
con.close();
writeDump(q);
</cfscript>
You could also use the SQL Server JDBC Driver instead. See also Connecting with Integrated Authentication On Windows. However, that requires adding the driver jars and a DLL (sqljdbc_auth.dll) to the server, which it sounds like you cannot do.
Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver
URL: jdbc:sqlserver://localhost:1433;integratedSecurity=true;
Found an example here that shows the username and password in the URL.
<cfset connection = driverManager
.getConnection("jdbc:mysql://localhost:3306/demodb?user=demouser&password=demopass")>
Your URL for SQL Server doesn't pass in any credentials, give that a try.

Error when connecting to MSSQL Server with Google Apps Script via JDBC

I'm trying to connect to Microsoft SQL Server using Google Apps Script. I'm using SQL Server 2008 R2 and I am using one of the suggested scripts that's supposed to read data and put it into a Spreadsheet:
https://developers.google.com/apps-script/jdbc#reading_from_a_database
The error message is:
Failed to establish a database connection. Check connection string, username, and password
Username and password are OK, the user is DBowner. The port is also correct, I tried to connect to the server via Telnet using:
o IP-address 1433
and it works.
Here's the code:
function foo() {
var conn = Jdbc.getConnection("jdbc:sqlserver://IP-adress:1433/DBName","user","password");
var stmt = conn.createStatement();
stmt.setMaxRows(100);
var start = new Date();
var rs = stmt.executeQuery("select * from person");
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange('a1');
var row = 0;
while (rs.next()) {
for (var col = 0; col < rs.getMetaData().getColumnCount(); col++) {
cell.offset(row, col).setValue(rs.getString(col + 1));
}
row++;
}
rs.close();
stmt.close();
conn.close();
var end = new Date();
Logger.log("time took: " + (end.getTime() - start.getTime()));
}
Do you have any idea of what can be wrong? Do I have to make some configuration on my Server? Or in the database? The instructions mentioned above says to ensure that Google's Ip-addresses can reach the database. But instead of listing all of Google's IP-addresses I granted access to all on that port. I also enabled TCP/IP Protocol in SQL Server Configuration Manager. And I granted "Remote connections" to the Server in MSSMS. Any other idea, please?
Well, I found the answer here:
Google Apps Scripts/JDBC/MySQL
Obviously, the connection string has to look like this:
var conn = Jdbc.getConnection("jdbc:sqlserver://IP-address:1433;" + "databaseName=DBName;user=username;password=password;");
I don't understand why connection string differs from the one in Google Documentation but this one works for me...
The Issue is in the Connection string.
It should be like so
address = '%YOUR SQL HOSTNAME%';
user = '%YOUR USE%';
userPwd = '%YOUR PW%';
dbUrl = 'jdbc:sqlserver://' + address + ':1433;databaseName=' + queryDb;
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
I have an entire tool on GitHub where you are able to connect to MySQL and SQL Servers. I you will be able to use it to help you. I will be constantly updating with more features overtime! You can find it here.
GOOGLE SPREADSHEET JDBC CONNECTOR TOOL

Resources