Java-oracle connection Exception - java

Environment: Eclipse(luna)
I am not able to retrive data from the oracle database 11g (11.2.0.2) used jdbe 6
I am getting the following two exception
java.sql.SQLRecoverableException
oracle.net.ns.NetException
Here is the code
import java.sql.*;
public class JavaConnection {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","******","*******");
Statement st=con.createStatement();
String sql="select * from emp";
ResultSet rs=st.executeQuery(sql);
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
con.close();
}
}
the exception thrown is
Exception in thread "main" java.sql.SQLRecoverableException: IO Error: Got minus one from a read call
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:489)
at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:553)
at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:254)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at emodatabase.JavaConnection.main(JavaConnection.java:9)
Caused by: oracle.net.ns.NetException: Got minus one from a read call
at oracle.net.ns.Packet.receive(Packet.java:311)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:300)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1140)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:340)
... 7 more
What is cause that is causing it and how to resolve it?
Additional Information about listner

Related

Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver In netbeans

I'm trying to connect the database with Netbeans with the help of oracle 10g. I have downloaded odjbc7.jar file and added it in the ORACLE THIN driver in db services. Still, I'm getting this class not found exception. (I am using Apache netbeans).
enter code here
import java.sql.*;
public class jdbclass {
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
String url="jdbc:oracle:thin:#localhost:1521:orcl";
String uname="sh";
String passwd="ara";
String query="select pizza_type from pizza";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(url,uname, passed);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(query);
String name=rs.getString("pizza_type");
rs.next();
System.out.print(name);
st.close();
con.close();
}
}
You should use ojdbc14.jar as mentioned at https://docs.oracle.com/cd/E19830-01/819-4721/beanh/index.html
Make sure you have added the jar in the classpath as mentioned at How do I set the classpath in NetBeans?

how to connect intelliJ to oracle

I have installed oracle and created table in it. now I want to connect intellij idea to oracle. I have added classes12.jar to libraries, but I can't connect my code to oracle. what should I do? my code is:
package example;
import java.sql.*;
public class first {
private Connection connection;
private Statement statement;
public first()throws Exception
{
Class.forName("oracle.jdbc.driver.oracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:ORCL","maryam","myjava123");
statement = connection.createStatement();
}
public void insert() throws Exception
{
statement.executeUpdate("INSERT INTO T1 (ID,NAME) VALUES (1,'ALI')");
}
public void close() throws Exception
{
statement.close();
connection.close();
}
public static void main(String[] args)throws Exception {
first mari=new first();
mari.insert();
}
}
and the Error is:
Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.driver.oracleDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at example.first.<init>(first.java:9)
at example.first.main(first.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Change
Class.forName("oracle.jdbc.driver.oracleDriver");
//--------------------------------^-- here is the issue
to
Class.forName("oracle.jdbc.OracleDriver");
or
Class.forName("oracle.jdbc.driver.OracleDriver");
Additionally, check if the driver jar file is really present in the classpath.
Use ojdbc6.jar or ojdbc7.jar in classpath. And also change oracleDriver to OracleDriver.

Not able to establish connection with Progress Database using JDBC

I am getting the following exception :-
java.sql.SQLException: [DataDirect][OpenEdge JDBC Driver][OpenEdge] Access denied(Authorisation failed)
at com.ddtek.jdbc.openedge.client.ddd.aw(Unknown Source)
at com.ddtek.jdbc.openedge.client.ddd.j(Unknown Source)
at com.ddtek.jdbc.openedge.OpenEdgeImplConnection.j(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.b(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.k(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.b(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseConnection.a(Unknown Source)
at com.ddtek.jdbc.openedgebase.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.ncr.sj250216.JdbcTest.main(JdbcTest.java:18)
There is no compile time error in my code, i am using the right jars as referenced libraries.
package com.ncr.sj250216;
import java.sql.*;
public class JdbcTest
{
public static String
URL="jdbc:datadirect:openedge://sun3899.daytonoh.ncr.com:28409;DatabaseName=dispatch";
public static String username="CSMSRUP";
public static String password=null;
public static void main(String [] args)
{
try
{
Class.forName ("com.ddtek.jdbc.openedge.OpenEdgeDriver");
System.out.println("Driver loaded");
Connection conn = DriverManager.getConnection(URL,username,password);
System.out.println("Connected");
Statement stmt = conn.createStatement();
System.out.println("Statement created");
ResultSet rs = stmt.executeQuery("select remark from pub.dupdate_remarks");
System.out.println("ResultSet : \n");
while (rs.next())
{
System.out.println(rs.getString(1));
}
System.out.println("AFTER CRUISING THROUGH THE RESULT SET");
rs.close();
stmt.close();
conn.close();
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
Access denied and Authorization failed mean that you either have the wrong userid & password or you are attempting to connect to the wrong db.
If you have access to the server that the db runs on you can test your credentials using Progress' sqlexp command line tool:
sqlexp -user userName -password passWord -db dnName -S servicePort
Or you could use any popular Windows based SQL querying tool to try the connection...

HSQLDB connection throws SQLException and HsqlException at the same time

I'm doing an app for my high school and I have the following situation:
I will use an embedded HSQL database for storing my application data. When I open the connection, I try to connect to the database only if exists, so it will not be created if it doesn't exist. Basically, if an HsqlException is thrown, functions to create the database, tables and constraints will be called.
The point is that this line:
Connection c = DriverManager.getConnection(url, user, pwd);
Throws an SQLException AND an HsqlException. But if I try to catch and control them, the output keeps being like this:
run:
2014-11-19T20:15:25.142+0100 SEVERE could not reopen database
Catched an Hsql or SQL Exception. //This is my 'control' over the Exceptions.
org.hsqldb.HsqlException: La base de datos no existe: C:/Databases/DB
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.persist.Logger.open(Unknown Source)
at org.hsqldb.Database.reopen(Unknown Source)
at org.hsqldb.Database.open(Unknown Source)
at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
at org.hsqldb.DatabaseManager.newSession(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DBManager.DBManager.main(DBManager.java:20)
BUILD SUCCESSFUL (total time: 0 seconds)
And finally this is my code. I hope someone call tell me why is this happening.
package DBManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DBManager {
static Connection c;
static Statement stm;
public static void main(String[] args){
try {
Class.forName("org.hsqldb.jdbc.JDBCDriver");
c = DriverManager.getConnection("jdbc:hsqldb:file:C:/Databases/DB;create=false", "SA", "");
} catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException has been catched.");
} catch (SQLException | org.hsqldb.HsqlException ex) {
System.out.println("Catched an Hsql or SQL Exception.");
//TODO call connection where the database is created.
//TODO create schema CreateSchema.run(c);
//TODO create constraints
} finally {
//TODO
}
}
private static class CreateSchema{
public static void run(Connection con){
//TODO: Create table queries.
}
}
}
Assuming you do not already have a HSQLDB database in C:/Databases/DB then this is behaving how I would expect. Your connection string tells it not to create the database if it does not exist, so it can't return an exception so it throws an exception which you are catching and logging.
Because the exception has been caught the run is considered successful because the main method did not throw an exception or call System.exit() with a non-zero value.
The exception you are catching is actually the SQLException and not the HsqlException (which is a RuntimeException) - the SQLException has a nested HsqlException. If you print the stack trace of the exception (ex.printStackTrace()) you can see this. So you should just catch SQLException.
The extra output you see is from within the HSQLDB driver class - if you look at the source for org.hsqldb.Database#reopen() you can see it calls logger.logSevereEvent("could not reopen database", e); and that's what responsible for the extra output you see. It tries to use a logging framework if one is available and defaults to the one in the JDK, which if not configured will print to System.err.

How to use a JavaDB database from a Java Servlet in Jetty?

I have created a Dynamic Web Page project in Eclipse and coded a Servlet. I have added derby.jar as a library. When I'm going to deploy the project, I export it as a .war-file and then start Jetty with java -jar start.jar. The servlet works fine without database code. But when I try to use the JavaDB database, the JDBC driver couldn't be found. How do I use a JavaDB database from my Servlet in Jetty?
I try to use an in-memory-database for testing, and my code looks like this:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sql = "DECLARE GLOBAL TEMPORARY TABLE SESSION.mytable "+
"(id int, message varchar(10)) NOT LOGGED";
String connURL = "jdbc:derby:memory:memdatabase;create=true";
try {
Connection conn = DriverManager.getConnection(connURL);
PreparedStatement ps = conn.prepareStatement(sql);
boolean success = ps.execute();
System.out.println("created: " + success);
} catch (SQLException e) {
e.printStackTrace();
}
}
Here is the Exception that is thrown:
java.sql.SQLException: No suitable driver found for jdbc:derby:memory:memdatabas
e;create=true
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.example.MyJavaServlet.doPost(MyJavaServlet.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:598
)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java
:486)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.j
ava:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.jav
a:499)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandl
er.java:233)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandl
er.java:1065)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:
413)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandle
r.java:192)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandle
r.java:999)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.j
ava:117)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(Cont
extHandlerCollection.java:250)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerColl
ection.java:149)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper
.java:111)
at org.eclipse.jetty.server.Server.handle(Server.java:350)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(Abstrac
tHttpConnection.java:454)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpC
onnection.java:900)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.conten
t(AbstractHttpConnection.java:954)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:851)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnecti
on.java:77)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEn
dPoint.java:606)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEnd
Point.java:46)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPoo
l.java:603)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool
.java:538)
at java.lang.Thread.run(Unknown Source)
You did not load the driver class before obtaining a connection:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = ...
Just as a side note, you might also want to refactor the process of loading the driver class and using connections to a single point in your application.

Categories

Resources