Connection with oracle in java application - java

I have downloaded oracle express 11g edition and installed that.Now i want to connect it from java application. Here is my Connection code :-
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:example", "example","password123");
But when i am trying to connect it, it showing me following exception.
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:419)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:536)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:228)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at javaapplication3.JavaApplication3.main(JavaApplication3.java:40)
But when i am trying to connect with "xe" database then it is connected.
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe", "example","password123");
I dont know why this is happening?. Please give me some reference or hint.

I think, you are misunderstanding between database schema and database type. In Oracle, XE means Express Edition of oracle database. ORCL means Oracle Corp.
In mysql
DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "example","password123");
`test` is a database schema.
In Oracle XE
DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe", "example","password123");
DriverManager.getConnection("jdbc:oracle:thin:scott/tiger#myhost:1521:orcl","example", "password123");
`example`: database schema name and DB user name are the same.

The connection URLs for Oracle are in the format:
jdbc:oracle:thin:#HOST:PORT:SID
The SID is a site identifier. In a full oracle install you could have multiple SIDs, but for Oracle Express this will always be XE.
What you are refering to as a "database" equates to a "user" in Oracle ("example" in your code above). Tables etc... are created under that user.

In URL pattern XE and orcl are the service id's for the Oracle.
For Oracle expressed edition "xe" is used and for Oracle Enterprised edition
"orcl" is used..

Check which instances are known to listener by executing : lsnrctl services
Check your tnsnames.ora
Check your SID parameters for typo and invalid parameters value

Related

Unable to establish JDBC connection to Oracle DBMS in Eclipse

I'm using Oracle 18c Express edition and trying to connect to the same using the below code.
DriverManager.getConnection("jdbc:oracle:thin://#localhost:1521/XE", "system", "Root123");
And upon execution, there's an exception:
java.sql.SQLException: Invalid Oracle URL specified
I am unable to figure out what's wrong with the URL.
Kindly help resolve this issue.
TIA.
According to Oracle's documentation the URL should be:
jdbc:oracle:<drivertype>:<user>/<password>#<database>
Where user and password can be provided as connection properties:
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:#myhost:1521:orcl", "scott", "tiger");
You probably need to remove the // from the URL as well:
jdbc:oracle:thin:#localhost:1521:XE

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

how to set oracle JDBC database URL [duplicate]

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

Unable connect to Oracle 11g using JDBC - Invalid oracle URL specified

I'm struggling with establishing connection to my database using JDBC.
I've done already all necessary things mentioned in documentation.
I've got database working on my laptop - Oracle XE 11g rel. 2 with SID="xe", checked with SQL Developer
I have proper driver - ojdbc6.jar - and added it to my project in Eclipse's Java Build Path properties
I wrote few basic lines with try/catch block to establish connection:
Connection myConn = DriverManager.getConnection("jdbc:oracle:thin#localhost:1521:xe",
"system", "somepass");
Statement myStat = myConn.createStatement();
ResultSet myRe = myStat.executeQuery("SELECT * from PATIENTS");
while(myRe.next()){
System.out.println(myRe.getString("LAST_NAME"));
}
myConn.close();
myRe.close();
But after running my code i receive error "Invalid Oracle URL specified".
Everything looks fine but I am just starting with JDBC.. Did I miss something?
You are missing a colon - use
jdbc:oracle:thin:#localhost:1521:xe
^
instead of
jdbc:oracle:thin#localhost:1521:xe
^^^
as the connection string.
See also https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
... Where the URL is of the form:
jdbc:oracle:<drivertype>:#<database>

Connection from a Java program to a SQL Developer database

I would like to access to a Oracle database (SQL Developer) from a Java program. I never used JDBC before.
Here is what i wrote:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:host_name:port:database_name";
Connection con = DriverManager.getConnection(url, login, passwd);
I got an error:
[Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified
Host name, port, DB name and logins are good.
Is this driver OK to communicate with SQL Developer ?
I don't know what to do,
thanks for helping !
Try this
Class.forName ("oracle.jdbc.driver.OracleDriver");
for Oracle you can use ojdbc
Class.forName("oracle.jdbc.driver.OracleDriver");
for SQL Server u can use jtds
Class.forName("net.sourceforge.jtds.jdbc.Driver");
The JDBC driver sun.jdbc.odbc.JdbcOdbcDriver is bridge driver that wraps an ODBC driver as described here.
SQL Developer is an Oracle tool that acts as an IDE against the Oracle database.
To connect Java to an Oracle database you should obtain the Oracle JDBC driver and ensure the jar is on your classpath (as described in the documentation for java.sql.DriverManager, forcing the class to be loaded is no longer necessary).
The important bit is the connection string, which in its simplest form for Oracle should follow the structure:
jdbc:oracle:thin:#//host:port/service
Where:
host: the hostname of the machine running Oracle
port: the port that Oracle is listening for connections on
service: the database instance to connect to
The full docs are here.

Categories

Resources