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.
Related
what are the drivers which are used to connect with java for Desktop application. i have found many types like tiny and all but was not able to understand.
Please help me out
To make your life easier, I would recommend just using Oracle's Thin Driver.
First, download the driver from Oracle's site:
http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
Then add JAR to your project and connect to database using:
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:#//localhost:1521/orcl", "scott", "tiger");
Of course, replace these parameters with the ones corresponding to your DB.
Have you looked into official site
See Also
JDBC kick start
for oracle 10g the JDBC driver is "ojdbc10_g.jar"
it is available on your system inside %ORACLE_HOME\jdbc\lib
No need to download.
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:#//localhost:1521/orcl", "scott", "tiger");
conn.setAutoCommit(false);
The "thin" driver is a 100% java implementation. The OCI calls the C implementation. There might even be a JDBC to ODBC bridge, allowing you to use your system's ODBC driver. Suggested reading: Oracle Instant Client
The easiest one to deploy is probably the type 4 driver, or the thin driver. I say it is the easiest because it does not rely on any Oracle native libraries or client install to operate. It is readily available from Oracle.
I'm trying to deploy a JBoss webapp that requires selectMethod=cursor in the jdbc driver connection string.
But when I try connecting to my mssql (2008) database with this in the string, it just timesout when connecting. When I remove selectMethod=cursor from my connection string, it works/connects, but my app doesn't work and gives me this error: [SQLServer JDBC Driver]Can't start a cloned connection while in manual transaction mode.
I have already updated my jdbc driver - tested all versions.
Here is my connection string:
jdbc:microsoft:sqlserver://127.0.0.1:1434;DatabaseName=xxxx;user=xxxx;password=xxxxx;selectMethod=cursor
Thanks.
I switched from using a microsoft jdbc driver to jtds jdbc driver and it works wonderfully again.
Do the Microsoft docs on the issue shed any light on this?
This error occurs when you try to execute multiple statements against a SQL Server database with the JDBC driver while in manual transaction mode (AutoCommit=false) and while using the direct (SelectMethod=direct) mode. Direct mode is the default mode for the driver.
Resolution is:
When you use manual transaction mode, you must set the SelectMethod property of the driver to Cursor, or make sure that you use only one active statement on each connection as specified in the "More Information" section of this article.
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
what are the drivers which are used to connect with java for Desktop application. i have found many types like tiny and all but was not able to understand.
Please help me out
To make your life easier, I would recommend just using Oracle's Thin Driver.
First, download the driver from Oracle's site:
http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
Then add JAR to your project and connect to database using:
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:#//localhost:1521/orcl", "scott", "tiger");
Of course, replace these parameters with the ones corresponding to your DB.
Have you looked into official site
See Also
JDBC kick start
for oracle 10g the JDBC driver is "ojdbc10_g.jar"
it is available on your system inside %ORACLE_HOME\jdbc\lib
No need to download.
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:#//localhost:1521/orcl", "scott", "tiger");
conn.setAutoCommit(false);
The "thin" driver is a 100% java implementation. The OCI calls the C implementation. There might even be a JDBC to ODBC bridge, allowing you to use your system's ODBC driver. Suggested reading: Oracle Instant Client
The easiest one to deploy is probably the type 4 driver, or the thin driver. I say it is the easiest because it does not rely on any Oracle native libraries or client install to operate. It is readily available from Oracle.
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","");