Unable to connect to database through Eclipse - oracle

I have been trying to run this code for hours, to get no success. I have Oracle 11g Enterprise Edition. Does anyone know the code to get port number from sql * plus cmd prompt?
The code doesn't run and I am not able to view the output over eclipse. Please guide me.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Testt {
public static void main(String[] args) throws Exception {
try{
//Driver registration
Class.forName("oracle.jdbc.driver.OracleDriver");
//Create connection
Connection c =DriverManager.getConnection("jdbc:oracle:thin:
#localhost:1521:EE","scott","tiger");
//Create statement
Statement stmt=c.createStatement();
//Execute query
ResultSet rs=stmt.executeQuery("select * from emp;");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
c.close();
}
catch(Exception e){e.printStackTrace();}
}
}
Output:
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

Related

phoenix jdbc doesn't work, no exceptions and stuck

I'm new to phoenix and hbase.hbase table and phoenix view works well and i can fetch data through phoenix. when I access jdbc to phoenix,it stucks.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Phoenix {
private static String driver = "org.apache.phoenix.jdbc.PhoenixDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Statement stmt = null;
ResultSet rs = null;
System.out.println("start...");
Connection con = DriverManager.getConnection("jdbc:phoenix:[my_cloud_server_ip]:2181");
System.out.println(con);
con.close();
}
}
(only one zookeeper server has public internet ip,so i write this ip there,does it matters?)
it prints "start..." and no response any more
BUT when when the url is "jdbc:phoenix:ip:2181" or "jdbc:phoenix:ip:2181/hbase"
i got no response
when i add some other words ,for example "jdbc:phoenix:ip:2181/balabala"
i got NULL POINTER EXCEPTION
why ?
I hope u can understand what i say:)
Reproducing your example, I am getting a java.net.SocketTimeoutException after 60 seconds, caused by java.net.UnknownHostException: unknown host: <hostname_of_my_zk_server>. But maybe that's what you called "no response" if you were in a hurry (or if you have a custom hbase-site.xml with a larger timeout client-side).
This error seems like the one explained in this article (section Zookeeper at the end) :
As in our case CDH was running in a test VM, we encountered this issue: http://stackoverflow.com/questions/18428722/hbase-java-client-unknown-host-localhost-localdomain
That was worked around by adding localhost.localdomain to the existing /etc/hosts entry for cluster1, which was already pointing to the right IP address.
This answer on SO summarizes the solution.
Basically, you need to add an entry to your /etc/hosts client-side :
<my_cloud_server_ip> <hostname_of_my_cloud_server_ip>
Moreover, you need to have an hbase-site.xml client-side (you can use the one on your server as a basis).

JDBC connection error

I have created a JDBC Connection Pool in Glassfish and use it as a JDBC Resource "jdbc/__default". I can ping this connection pool successfully in the Administration Console interface.
Then, I am trying to run the following program:
package de.java2enterprise.onlineshop;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
#WebServlet("/test")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// #Resource(name="jdbc/__default")
// private DataSource ds;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter writer = response.getWriter();
response.setContentType("text/html;charset=UTF-8");
writer.println("<!DOCTYPE html>");
writer.println("<html><body>");
Connection con = null;
DataSource ds;
try {
ds = (DataSource) InitialContext.doLookup("jdbc/__default");
con = ds.getConnection();
if (con.isValid(10)) {
writer.println("<BR>Connected!");
}
con.close();
} catch (Exception ex) {
writer.println(ex.getMessage());
} finally {
if (con != null){
try{
con.close();
} catch (Exception ex){
}
}
}
writer.println("<BR>Test finished!</body></html>");
writer.close();
}
}
However, then I receive the following error in the web browser:
Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error: ORA-12518, TNS:listener could not hand off client connection
Test finished!
In the server log, I get the following error:
2015-04-08T08:15:20.540+0200|Information: Loading application [onlineshop#onlineshop-war.war] at [onlineshop-war]
2015-04-08T08:15:20.636+0200|Information: onlineshop was successfully deployed in 611 milliseconds.
2015-04-08T08:15:38.552+0200|Warnung: Context path from ServletContext: /onlineshop-war differs from path from bundle: onlineshop-war
2015-04-08T08:15:38.802+0200|Information: visiting unvisited references
2015-04-08T08:15:39.519+0200|Information: Successfully got INSTRUMENTATION: sun.instrument.InstrumentationImpl#2d618042
2015-04-08T08:15:41.471+0200|Warnung: RAR5038:Unexpected exception while creating resource for pool Onlineshop. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: Listener refused the connection with the following error:
ORA-12516, TNS:listener could not find available handler with matching protocol stack
2015-04-08T08:15:41.471+0200|Warnung: RAR5117 : Failed to obtain/create connection from connection pool [ Onlineshop ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: Listener refused the connection with the following error:
ORA-12516, TNS:listener could not find available handler with matching protocol stack
2015-04-08T08:15:41.486+0200|Warnung: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: Listener refused the connection with the following error:
ORA-12516, TNS:listener could not find available handler with matching protocol stack
]
After that, I can not ping the connection pool again and receive more or less the same error in the Administration Console.
I use the following configuration: JDK 1.8.0_25, GlassFish Server Open Source Edition 4.1 (build 13), Oracle Database XE 11.2.
In Glassfish administration console, I've created the following JDBC Resource: JNDI Name: jdbc/__default, Logical JNDI Name: java:comp/DefaultDataSource, Connection Pool: Onlineshop.
The JDBC Connection Pool Name is Onlineshop, Resource Type: javax.sql.DataSource, Classname: oracle.jdbc.pool.OracleDataSource.
I've a minimal pool size of 800 connections, maximum of 3200 connections. The other parameters I've kept as they were, I've only added user and password at JDBC Connection Pool Properties.
Anyone an idea? :-)
Close your connection in a finally block or in try-with-resources if you use jdk7+ as it should be done in Java. See if it helps

SQL exception while creating connection to SQL Server using jdbc

I am getting the following error while creating a connection to SQL Server using jdbc. THe error is :
java.sql.SQLException: No suitable driver found for jdbc:sqlserver//D-PC//SQLEXPRESS;integratedSecurity=true
Failed to make connection!
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement;
/** * * #author Jatin */ public class SQLConnect {
public Connection sqlcon(Connection conn){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
Statement stmt = null;
try{
conn = DriverManager.getConnection("jdbc:sqlserver//D-PC//SQLEXPRESS;integratedSecurity=true");
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Students");
System.out.println("Database Created");
}
catch(SQLException e){
e.printStackTrace();
}
if (conn != null) { System.out.println("You made it, take control your database now!"); } else { System.out.println("Failed to make connection!"); }
return(conn);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Connection con =null;
SQLConnect sqlcon = new SQLConnect();
con = sqlcon.sqlcon(con);
}
}
The connection to the host GUNA, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:237)
how to resolve this

Getting an error when try to connect to Oracle java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

i am trying connect to Oracle Database through following program. but throws an error
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver "
only SQL developer client is installed on my machine whereas Actual data base is store on Server. Please help on resolving the issue
package test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
public class DBConnection {
public static void main(String[] argv) {
System.out.println("-------- Oracle JDBC Connection Testing ------");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your Oracle JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
enter code here
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#hostname:5800:SID", "user","password");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
Add your Oracle JDBC Driver jar to the classpath
Can download the driver for your Oracle Database Version from here

JDBC-ODBC connectivity

import java.sql.*;
public class QBreaker
{
public static void main (String[] args)
{
Connection conn = null;
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String url="jdbc:odbc:huss";
try
{
Class.forName(driver);
conn=DriverManager.getConnection(url,"sa","admin123");
System.out.println("Connection is created");
Statement db_statement=conn.createStatement();
ResultSet rs=db_statement.executeQuery("select * from Details");
while(rs.next())
{
System.out.print("ID: "+rs.getInt("Id"));
System.out.print("\tBalance: "+rs.getString("Bal"));
}
conn.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
This is my code to connect to the database.
i have created the odbc driver n followed all the required steps in dat.
but still while running the i got this exception:
Connection is created
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'Details'.
Process completed.
There is no such table or view named Details exists. Please verify the database.
u create Data source ? , if u not created it can u go to control panel in windows then Administrative Tools then Data Sources (ODBC) .

Resources