Display application name in Session Information when connecting via JDBC - java

I create a connection to a postgres 9 database using the stardard JDBC driver.
...
Connection myCon = DriverManager.getConnection("jdbc:postgresql://localhost/test?&user=test&password=test");
...
When I check the server status with PgAdmin and display all database sessions, I can see that the "Application Name" is not set for my Session. Is there a way to set the application name in the JDBC connection?

That is possible to set application name as connection parameter since Postgres JDBC 9.1dev-900:
Add support for setting application_name on both connection startup
and later through Connection.setClientInfo. (jurka)
ex: jdbc:postgresql://localhost:5432/DbName?ApplicationName=MyApp
or Connection.setClientInfo("ApplicationName", "My App")
For previous versions you could do this by setting application_name runtime parameter:
s.execute("SET application_name TO 'MyApp'");

Related

Unable to connect MySQL server at localhost 3306 using netbeans 11.3

In the Maven project. When I first set up XAMPP and MySQL work very well. But inside NetBeans I can't connect to the MySQL and can't create a connect to the mysql-connector-java-8.0.20.zip or mysql-connector-java-8.0.20.jar.
driver="com.mysql.cj.jdbc.Driver" and url="jdbc:mysql://localhost:3306/mysql"
When I going to add new connection to MySQL it display error message,
Cannot establish a connection to jdbc:mysql://localhost:3306/mysql using com.mysql.cj.jdbc.Driver (The server time zone value 'unknown' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver
 
"The server time zone value 'unknown' is unrecognized or represents more than one-time zone"
This is a common error in MySQL to fix this error.
1)Create a new connection to MySQL
2)Connect JDBC driver to using mysql-connector-java-8.0.20.jar
3)In JDBC URL add server time zone as follows make sure your database name is mysql otherwise change the database name to your database name.
jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC
NB:-If you want you can add server time zone as a connection property
set property as serverTimezone and value as UTC.
If this didn't work use MariaDb driver to connect MySQL
1)Create a new connection
2)Add mariadb-java-client-2.6.0 to the connection
3)In JDBC URL add as follows and you don't want to worry about server time zone when you use MariaDB
jdbc:mariadb://localhost:3306/mysql
It looks like you're running on windows but the solution for me involved customizing the mysql driver.
On the netbeans services tab open the drivers folder right-click the mysql connector/driver and then cutomize. Locate your driver on your system and add it to the class then click ok. Worked for me on ubuntu

Error when trying to open a JDBC connection to Google Cloud Spanner

I am trying to open a JDBC connection to a Google Cloud Spanner database, but I get the following error message:
java.lang.IllegalArgumentException: A project ID is required for this
service but could not be determined from the builder or the
environment. Please set a project ID using the builder.
My JDBC URL is as follows:
jdbc:cloudspanner://localhost;Project=project-id;Instance=instance-id;Database=database-name;PvtKeyPath=path-to-key-file
If I remove the Project property from the URL, I get the following exception:
java.sql.SQLNonTransientConnectionException: [Simba]JDBC
Connection Refused: [Simba]JDBC Required Connection Key(s):
Project; [Simba]JDBC Optional Connection Key(s): Language,
Mode
So it seems that the driver does pick up my Project ID, but somehow does not accept it. I have checked and double checked that my project id does equal the project id that I created on Google, I have also tried to change the value to the project name instead of the project id, but to no avail.
Does anyone have a URL example that works?
EDIT: It appears to be related to the reference to the private key file. If I make an environment variable GOOGLE_APPLICATION_CREDENTIALS pointing to my private key file, the connection can successfully be made. If I remove this environment variable, I get the above exception.
Which version of the driver are you using? In the latest version, if you are specifying the path to the credentials file in the URL then you need not set GOOGLE_APPLICATION_CREDENTIALS.
As the JDBC Driver supplied by Google is severely limited (does not support DML and DDL statemetns), I have written my own JDBC Driver. The driver is designed to work with JPA/Hibernate-enabled applications. The driver can be found here: https://github.com/olavloite/spanner-jdbc
This driver supports the same kind of URL's as the driver supplied by Google, including the PvtKeyPath property.

How to connect to H2 database using jmeter?

I am using Jmeter with JDBC to connect to h2 database. I have created JDBC connection configuration in the Jmeter Test Plan and given variable name as 'mydb' and my JDBC configuration is as follows:
database connection URL: jdbc:h2:tcp://localhost/d:/h2/sample
Driver class: org.h2.Driver
Username: sa
I have created a JDBC request under the Thread Group and am using the same variable name 'mydb' for the jdbc request. The jmeter throws me an exception.. Cannot create PoolableConnectionFactory (Connection is broken: "unexpected status 16777216" [90067-176])
however, i am able to access the database on my browser.
I am using the below link as a reference for what i am doing:
https://www.coursehero.com/file/p6a7t19/1-Set-up-an-H2-database-Download-an-H2-database-distribution-from/
Any hints on what i am missing??

java.sql.SQLException: Single-Sign-On is only supported on Windows. Please specify a user name

I am using the jTDS driver in order to connect to an SQL Server database from my Android application, which uses the Windows Authentication. As advised in the FAQs, I read the READMESSO file and as told, I placed the native SPPI library (ntlmauth.dll) in the system path (defined by the PATH system variable)
However, when I try to connect to the database using the following code:
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
String connString = "jdbc:jtds:sqlserver://192.168.56.1/MyMovies;";
Conncection conn = DriverManager.getConnection(connString);
I get the following exception:
java.sql.SQLException: Single-Sign-On is only supported on Windows. Please specify a user name.
Since you are connecting from an android device, you would not be able to get the SSO credentials required by the driver to connect to SQL server. The setting you referred to works only if the java program trying to connect to the DB is on a windows machine, which is clearly mentioned by the error message.
Unless your application has authorization based on the SSO user connecting to the DB, you should have an SQL Server user-based authentication mechanism to connect to the server and all authorization procedures should be tied to this user.
You might have to give the username also.
"jdbc:jtds:sqlserver://192.168.56.1/MyMovies;instance=SQLEXPRESS;user=foo"

How can I configure persistence.xml for SQL server

How can I configure persistence.xml file to connect to my SQL Server. Some information is connected as: Host, DatabaseName, UserName, Password, Port, ...
Setting up a connection pool to MySQL database using a configuration .xml file can be pretty troublesome to someone who has just started like you.
In my opinion, you should take a look at this short guide. You simply need to start Glassfish server, open the browser and surf to http://yourdomain.com:4848 to access the Admin panel. After that, just follow the instruction in that article and to create the JDBC Connection pool and JDBC Resource. At this point, you only need to open the persistence.xml file with your IDE (NetBeans, etc.) and set the Data Source property with name of your JDBC Resource and you're done.
One thing to note is that you must download the latest MySQL Connector/J and copy the file
mysql-connector-java-<version>-bin.jar
into the folder
<GlassFish-install-folder>\glassfish\domains\domain1\lib\ext
Otherwise, you will run into the expcetion Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource if you try to ping the database after creating the JDBC Connection pool.
First of all donwload jdbc driver for sqlserver and put that in glassfish lib directory and boot that.
Using Glassfish admin console:
define a jdbc connection pool and specify Host, DatabaseName,
UserName, Password, Port, ...
define a jdbc resource and select connection pool name that you have specified in step 1
then grab the name of jdbc resource and put it in your persistence.xml file : <jta-data-source>YOUR-JDBC-RESOURCE-NAME</jta-data-source>
and now you can connect to data base.
If you are using Netbeans, it has some nice wizards to produce connection pool and jdbc resource in glassfish.
Since you are using JEE6 this is another way using annotation:
import javax.annotation.sql.DataSourceDefinition;
#DataSourceDefinition(name = "java:app/env/myDatasource",
minPoolSize = 0,
initialPoolSize = 0,
className = "your.driver.class",
serverName="localhost",
user = "admin",
password = "admin",
databaseName = "test"
)
public class DbConfiguration {
}
For more info take a look at here.

Categories