Unable to establish JDBC connection to Oracle DBMS in Eclipse - java

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

Related

Where exactly should JDBC URL value be changed?

I'm trying to run some application and get the following error:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
JDBC]Can't start a cloned connection while in manual transaction mode.
I know that I should add the parameter ;SelectMethod=Cursor to your JDBC URL
But I'm having problem understanding where exactly should I change it? Should it be some conf file in JDBC driver folder somewhere? Or can I do it from sql management studio?
Also is there some easy way to determine if and what version of JDBC driver I have?
Help is very much appreciated!
You specify the URL when creating your JDBC connection, e.g.:
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]",
username,
password);
Of course you have to replace the stuff in the brackets with your values.
Quite the same is true for every other tool (e.g. IntelliJ, Eclipse) I know of that connects to a DB via JDBC. See e.g. attached screenshot. Here you also specify the connection parameters via the JDBC URL.

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.

Connection with oracle in java application

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

Connecting to MS SQL DB from Java Netbeans

How do I connect to MS Sql database using Java.
I am using the jtds jdbc driver but keep getting the "No Suitable Driver" error. I have checked the jdbc URL and it conforms to the URL format specified in the documentation.
I am running on JRE6.
Have you actually loaded the driver class?
Example for MySQL:
Class.forName("com.mysql.jdbc.Driver");
before connecting:
Connection con = DriverManager.getConnection(url,"user", "pw");
I have actually found a solution. One can use the microsoft SQL server jdbc driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = " jdbc:microsoft:sqlserver://serverjag:1433";
Connection conn = DriverManager.getConnection(url,"sa","");

Categories

Resources