how to set oracle JDBC database URL [duplicate] - java

This question already has answers here:
How to connect to Oracle using Service Name instead of SID
(8 answers)
Closed 6 years ago.
I am using an application which needs to access my oracle database to do some tasks with my data.
That application needs two properties, which are:
d2rq:jdbcDSN
The JDBC database URL. This is a string of the form jdbc:subprotocol:subname. For a MySQL database, this is something like jdbc:mysql://hostname:port/dbname. Examples for other databases #
d2rq:jdbcDriver
The JDBC driver class name for the database. Used together with d2rq:jdbcDSN. Example: com.mysql.jdbc.Driver for MySQL. #
I want to connect to an Oracle database that has this version
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
"CORE 11.2.0.4.0 Production"
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
Normally I connect to my oracle database using a SQL developer application where I just set:
Host Name
Username and password
Service Name
Schema
Now to connect to my database from this new app (now the sql developer), i am required to do (from this page http://www.devx.com/tips/Tip/28818)
jdbc:oracle:thin:#<HOST>:<PORT>:<SID>
oracle.jdbc.driver.OracleDriver
so I changed that template to:
jdbc:oracle:think:#HostName:port:ServiceName
and I copied the ojdbc6.jar to the bin folder
when I run, i get this error:
Database connection to jdbc:oracle:thin:#HostName:Porto:ServiceName failed (user: UserName): Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect description
It sounds like the SID should be be replaced by service name. (i am not sure) do you have any idea about how to solve it and if what I did is correct?
off course i hide the real database and host and password for privacy reasons so please execuse me

I found the error
it should be like this
jdbc:oracle:thin:#hostname:porto/servicename
not
jdbc:oracle:thin:#hostname:porto:servicename

Related

Azure SQL database connection issue from an Azure VM by Java JDBC

We have a java project in an Azure virtual machine (VM), and need connect to Azure SQL db by JDBC connection, so we use the JDBC connection string provided by Azure SQL db as follows:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
but we got an exception:
"java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization.
The server name is *.database.windows.net, the name in certificate is cr2.eastus1-a.control.database.windows.net."
then we updated the JDBC connection string to:
"jdbc:sqlserver://ZZZdbserver.database.windows.net:1433;database=ZZZ;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=cr2.eastus1-a.control.database.windows.net;loginTimeout=30;"
but we got another exception:
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Cannot open server "ZZZ1" requested by the login. The login failed.)
where "ZZZ1" is our username for the Azure VM.
Any idea to solve this issue?
By default, Azure VM has not blocked the outbound connection to the Internet. You could first verify the network connectivity from your VM to the Azure SQL database via running the command telnet ZZZdbserver.database.windows.net 1433 in CMD or Test-NetConnection -computer ZZZdbserver.database.windows.net -port 1433 in Powershell. If it fails, you may check if there is any firewall blocking this database connection or port or your application is listening on that port or your SQL database is online.
Additionally, if the network connectivity has succeeded, you could verify if the username or password is correct when you are using to connect to your database. Make sure there is not any typo. Also, the username should have enough privileges to access this database. You can try to access this database via Server admin login in the properties of the SQL database. Moreover, you could refer to this sample code to use Java to connect to access the Azure SQL database.
When using the Microsoft JDBC Driver for SQL Server to connect to an Azure SQL Database. You should note this:
Appending the server name to the userId in the connection string
Prior to the 4.0 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you were required to append
the server name to the UserId in the connection string. For example,
user#servername. Beginning in version 4.0 of the Microsoft JDBC Driver
for SQL Server, it's no longer necessary to append #servername to the
UserId in the connection string.
Using encryption requires setting hostNameInCertificate
Prior to the 7.2 version of the Microsoft JDBC Driver for SQL Server,
when connecting to an Azure SQL Database, you should specify
hostNameInCertificate if you specify encrypt=true (If the server name
in the connection string is shortName.domainName, set the
hostNameInCertificate property to *.domainName.). This property is
optional as of version 7.2 of the driver.
Hope this helps.
If I need set encrypt=true, and hostNameInCertificate=cr2.eastus1-a.control.database.windows.net. Where do I need get the certificate for cr2.eastus1-a.control.database.windows.net from Azure SQL DB's service?

Is there a difference in the way hibernate connects to db and the way a simple java program does?

I have used the exact same Driver Name, Connection URL , User Name and Password for connecting to my Oracle 11g Express edition database running on my local(same) machine from 2 codes.
Simple Java code to connect to DB and read values from a table.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:xe",
"SYSTEM","Platz#123"
);
In Hibernate persistence.xml
The Simple java connection works as expected, However Hibernate gives me the error "java.sql.SQLException: ORA-01017: invalid username/password; logon denied".
Does this has to do anything with the oracle installation configurations on my machine? Or if it is something else.
Could you please provide and explanation and the way out.
I believe the standard JPA property name for the user name is:
javax.persistence.jdbc.user
Not:
javax.persistence.jdbc.username

Oracle DB connection issue

When I try to connect java application to oracle db using jdbc thin client, my system name was sent to db server without host name (eg. 102XXXXX) But when I try with oracle client(PL/SQL developer), my system name was sent to db server with domain name appended (eg. North_America/102XXXXX). And in oracle server, my machine is whitelisted with domain name (eg. North_America/102XXXXX). So I got account locked exception when try to run the java application but have successful connection from PL/SQL developer. I am not sure how to solve this issue from my side. I need both connection. Anyone has any idea?
Note that you can also customize what the Oracle JDBC thin driver sends for the system name during authentication by setting this property "v$session.machine".

MyEclipse Professional on connecting with ms sql server is giving ssl error through db browser

I am using MyEclipse professional and trying to connect with the MS SQL SERVER 2016, i am using the jar file sqljdbc4-4.0 and driver
“class name= com.microsoft.sqlserver.jdbc.SQLServerDriver”
My connection URL is jdbc:sqlserver://hamdan:1433;databaseName=DWProject
where hamdan is the SQL server name, after giving the credentials i got the error of error while performing database login with the mysqlserver driver: the driver couldn't establish a secure connection to SQL server by using secure socket layer(SSL) encryption. Error java.lang.runtimeException, could not generate DH keypair.
However i have also set Force Encryption to "NO" in the protocol for sql server's properties in sql server configuration.
when i am changing the jar with the jar present on this link https://www.microsoft.com/en-us/download/details.aspx?id=11774 which is jdbc 6.0 and using jar for jre 8 which is sqljdbc42, i get error of driver class not found while using new database connection driver.
Any help will be highly appreciated, i am on this from more than one day,
I am new to eclipse.
This is the problem in windows 10, it encrypts the link between eclipse and SQL Server, so you should use latest myeclipse or try in different OS

How to connect to Oracle Database 12c HR schema from IntelliJ IDEA 14

I am new in Oracle Database. I have successfully done these:
Installed and configured Oracle Database 12c in Windows 10 VMware virtual machine.
Connected to HR schema from SQL*PLUS, SQL Developer, Navicat.
Created a ASP.NET web application project and connected to HR schema from Visual Studio 2015.
Now I want to connect to HR schema through JDBC from IntelliJ IDEA 14. Unfortunately I cannot connect though I have installed JDBC drivers. I have read the reference here but it does not work. I am getting the following error:
Connection to Oracle - HR#localhost failed
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:489)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:553)
...
For better demonstration, a screenshot is attached. Notice that I cannot define the service name (in this case "pdborcl") because there is no text field for this in Data Sources and Drivers window. Also notice that I can still connect to HR schema through SQL*PLUS after the error.
How to resolve this issue? Or, is it an IntelliJ IDEA bug or a JDBC bug?
You should be using "pdborcl" in the Database field. That is your service name, HR is just a schema in it.

Categories

Resources