i am trying to execute a sql command from a java program..i dont have any errors regarding this code..but i am facing connection refusals from the database..
import java.sql.*;
public class DBCreateTable
{
public static void main(String args[]) throws Exception
{
DriverManager.registerDriver (new Oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:xe","lms","abc");
Statement stmt=con.CreateStatement();
stmt.executeUpdate("create table emp(eno number(5),name varchar2(20))");
}
}
the errors encountered are:
Exception in thread "main" java.sql.SQLException: Io exception: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=185599488)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4))))
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:333)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:404)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.ja
va:468)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:314)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at DBCreateTable.main(DBCreateTable.java:7)
In my sql commands i have done the following..
SQL> connect system/tiger;
SQL> create user lms identified by abc;
SQL> grant connect,resource to lms;
and plz tell me what is scott tiger..i am messing a lot there..what users are there..what to unlock and how?? plz thanks..
Add an oracle driver jar to the project build path, and it should work.
(eg. http://mirrors.ibiblio.org/pub/mirrors/maven/mule/dependencies/maven1/oracle-jdbc/jars/ojdbc14.jar)
Your package name is wrong.
DriverManager.registerDriver (new **Oracle**.jdbc.driver.OracleDriver());
In java, package name always start by uncapitalize letter. Your program failed in compilation time.
The oracle.jdbc.driver.* classes, theojdbc4.jar file, and theOracleConnectionCacheImpl class are no longer supported or available from Oracle 11g onwards. So use oracle.jdbc.oracledriver instead.
karjala
did you dowload Oracle JDBC Drivers first download them, You can download Oracle JDBC drivers from here
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
Choose the version appropriate for your database version.
Java Program to Connect to Oracle:
The following Java program uses Oracle JDBC driver to connect to a running Oracle database instance. You can use this program on any Oracle database as this example uses Oracle’s built-in dummy table DUAL for fetching system date. DUAL enables us to get values such as system date using a normal SQL query.
1 // Example Java Program - Oracle Database Connectivity
2
import java.sql.Connection;
3
import java.sql.Date;
4
import java.sql.DriverManager;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
import java.sql.Statement;
8
9
public class OracleSample {
10
11
public static final String DBURL = "jdbc:oracle:thin:#localhost:1521:XE";
12
public static final String DBUSER = "system";
13
public static final String DBPASS = "manager";
14
15
public static void main(String[] args) throws SQLException {
16
17
// Load Oracle JDBC Driver
18
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
19
20
// Connect to Oracle Database
21
Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
22
23
Statement statement = con.createStatement();
24
25
// Execute a SELECT query on Oracle Dummy DUAL Table. Useful for retrieving system values
26
// Enables us to retrieve values as if querying from a table
27
ResultSet rs = statement.executeQuery("SELECT SYSDATE FROM DUAL");
28
29
30
if (rs.next()) {
31
Date currentDate = rs.getDate(1); // get first column returned
32
System.out.println("Current Date from Oracle is : "+currentDate);
33
}
34
rs.close();
35
statement.close();
36
con.close();
37
}
38
}
i hope this will help you guys....... :-)
Related
This question already has answers here:
Replacement for JDBC-ODBC Bridge
(2 answers)
JDBC ODBC Driver Connection
(2 answers)
Closed 10 months ago.
I am developing a Spring Boot application that polls data from a legacy ODBC data source and inserts it into a MS SQL Server database.
I need to connect to DSN that is using Progress OpenEdge driver.
My R&D code to connect to DSN looks like this:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JdbcDemo
{
public static void main(String args[]) throws Exception
{
try
{
String query = "SELECT Name,Description,Qty,Cost FROM Stock";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:DSN_Name");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
String name = rs.getString("Name");
String desc = rs.getString("Description");
int qty = rs.getInt("Qty");
float cost = rs.getFloat("Cost");
System.out.println(name + ", " + desc + "\t: " + qty + "\t# $" + cost);
}
con.close();
}
catch (Exception e)
{
System.err.println(e);
}
}
}
But it throws java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver error when run. I did some Googling and found this is no longer supported. How can then I connect to this ODBC data source? I am using Java 17.
java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver
exception comes in Java 8 because it has removed the JDBC ODBC bridge
driver class "sun.jdbc.odbc.jdbcodbcdriver" from JDK and JRE. This
class is required to connect any database using Object database
connectivity driver e.g. Microsoft Access, but unfortunately, you
cannot use it from JDK 8 onward. In order to solve this error, just
use the Jackcess library or a commercial driver like HXTT. Normally,
in pre-Java 8 world, java.lang.classnotfoundexception
sun.jdbc.odbc.jdbcodbcdriver error comes when you try to connect to
the Microsoft Access database from Java using JDBC and JDBC ODBC
bridge driver is not available in the classpath.
Read more:
https://javarevisited.blogspot.com/2015/07/how-to-solve-javalangclassnotfoundexception-sun.jdbc.odbc.jdbcodbcdriver.html#ixzz7TTDxvBZp
We can still use JDBC-ODBC Bridge in Java 8 (Java 17 not tested) too, we can always pull the driver from JDK 7: Removal of JDBC ODBC bridge in java 8
I’m trying to connect a Java program to a remote Oracle DB. After doing some research online, I decided that the easiest way to do this was with the Oracle JDBC driver. I downloaded and ran the jar file and got the message “***** JCE UNLIMITED STRENGTH IS INSTALLED *****.” The problem is that when I try to add the driver to my classpath (javac -classpath ojdbc8.jar Connect.java), I keep getting an error message saying “package oracle.jdbc.driver does not exist.” I’ve been researching how to fix this online, but I’m only getting confused. Any ideas on what I did wrong?
import java.sql.*;
public class Class1 {
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You must put a database name after the # sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as `<host>`:`<port>`:`<sid>`. The example uses the short cut syntax.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:thin:hr/hr#myhostname:1521:orcl",
"myUsername", "myPassword");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
conn.close(); // ** IMPORTANT : Close connections when done **
}
}
The error is:
java: package oracle.jdbc.driver does not exist
Can you try to run the sample DataSourceSample.java? Make sure you have the JDBC driver in the classpath. You can also refer to this quickstart for detailed instructions.
So I am building a java stored procedure on an Oracle Database 11g Enterprise Edition. I want my Java stored procedure to connect to a MySQL database, grab records and update my Oracle database. I can connect using
private Connection getConnection() throws SQLException {
// Create an OracleDataSource instance and set properties
OracleDataSource ods = new OracleDataSource();
ods.setUser(user);
ods.setPassword(passwd);
ods.setURL(url);
return ods.getConnection();
}
I am connecting using the following lib which works fine
import oracle.jdbc.pool.OracleDataSource;
But when I try to open a connection to a MySQL database in the Java stored procedure I get the following error
ORA-29532: Java call terminated by uncaught Java exception:
java.sql.SQLException: No suitable driver
ORA-06512: at "OPS.FN_JOB_UPDATE_ORACLE_LINE", line 1
ORA-06512: at "OPS.JOBS_PKG", line 13
ORA-06512: at line 2
I am assuming it has something to do the MySQL driver not being in the classpath on the Oracle server itself but I am not sure how to get it within the classpath.
Here is an example of how I am trying to connect to the MySQL database from the java stored procedure
public Connection setMysqlConnection(EntityDBSettings entity) throws
SQLException {
String connectionStr = String.format("jdbc:mysql://%s/%s?user=%s&password=%s", entity.getHost_uri(),
entity.getName(), entity.getUsername(), entity.getPaswrd());
this.connect = DriverManager.getConnection(connectionStr);
return this.connect;
}
So is it possiable to connect to a MySql database from a oracle java stored procedure? Or do I need another solution?
im using a embedded hsql database in my java programm.
I want to write a hsql statement like this:
statement.executeQuery("SELECT sum(Points) FROM Table");
At first i tried this one:
String column = "Points";
statement.executeQuery("SELECT sum(\""+column+"\") FROM \""+table+"\"");
java.sql.SQLException: Column not found: Points
Next one:
statement.executeQuery("SELECT sum(POINTS) FROM \""+table+"\"");
java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: POINTS
Next try, should never been working but only for you :-)
statement.executeQuery("SELECT sum(\'"+column+"\') FROM \""+table+"\"");
java.sql.SQLSyntaxErrorException: incompatible data type in operation
if i try this one:
statement.executeQuery("SELECT \""+column+"\" FROM \""+table+"\"");
runs perfectly
Just to show you that my column exist in my table.
This statement:
SELECT sum("Points") as test FROM "MyTable"
runs in SQuirrel Client Version 3.7
Any idea with my problem?
The same statement that works in SQuirrel should work. This is the same statement with Java quoting:
statement.executeQuery("SELECT sum(\"Points\") FROM \"MyTable\"");
It looks like you are connecting to a different database from your program and from SQuirrel. Try using the same absolute path to the database file in SQuirrel and in your program. You cannot connect at the same time, so you need to shutdown the database in SQuirrel before you connect from your program.
These are my imports
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
This Statment also not run:
statement.executeQuery("SELECT sum(\"Points\") FROM \"MyTable\"");
SOLUTION for maybe others:
"SELECT sum(\"Punkte\") as TEST FROM \"Match_Stats\"");
while(table_01.next()){
players.get(i).setPoints(table_01.getInt("TEST"));
}
Alias TEST was the key.
I am using eclipse and java to connect to an oracle 12c database. But every time I run through the process, I get a connection fail error that reads:
java.sql.SQLException: Invalid Oracle URL specified
Here is what my code looks like:
import java.sql.*;
public class JdbcConnection {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin#localhost:1521:xe", "username", "password");
Statement statement = con.createStatement();
String sql = "select * from employees";
ResultSet rs = statement.executeQuery(sql);
while (rs.next())
System.out.println(rs.getInt(3) + " " + rs.getString(2));
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
I am using a windows 7 sp1 Ultimate (64 bit)
CPU: i5 2.85 ghz
RAM: 8GB
Oracle is installed in a separate hard drive (in the same computer).
EDIT:I have tried the first suggestion from this post: How do you find out the Oracle database's URL?
and I get an error that says:
ORA-00928: SELECT KEYWORD MISSING...
This error occurs when I run the oracle "SQL DEVELOPER" application on the startup menu and type the suggestions from post above.
Note: I want to connect to the database from eclipse and query it programatically.
Is there a way to find out what the correct connection URL is?
Please help.