I'm doing a Proof-of-Concept (PoC) Java reporting project in which I'm connecting to a SSL-enabled Oracle database from my workstation, using JDBC Thin driver. As the database is SSL-enabled, I added all the required certificates into a Oracle Wallet and provided its location in the Java code. The certs were also added cacert of the JRE. Java code excerpt -
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("ERROR: Oracle JDBC Driver not found");
e.printStackTrace();
return;
}
System.out.println("Oracle JDBC Driver Registered!");
Connection connection = null;
String oracleURL = "jdbc:oracle:thin:#(DESCRIPTION(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCPS)(HOST=<hostname>)(PORT=2484)))(CONNECT_DATA=(SERVICE_NAME=<service>)))";
// Provide user ID, password for schema
Properties props = new Properties();
props.setProperty("user", "<user id>");
props.setProperty("password", "<password>");
// Setting properties for SSL
props.setProperty("oracle.net.ssl_cipher_suites", "(ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha, SSL_DH_anon_WITH_3DES_EDE_CBC_SHA, SSL_DH_anon_WITH_RC4_128_MD5,SSL_DH_anon_WITH_DES_CBC_SHA)");
props.setProperty("oracle.net.ssl_client_authentication", "false");
props.setProperty("oracle.net.ssl_version", "3.0");
props.setProperty("oracle.net.encryption_client", "REJECTED");
props.setProperty("oracle.net.crypto_checksum_client", "REJECTED");
props.setProperty("javax.net.ssl.keyStore", "C:\\APP\\ORACLE\\product\\11.2.0\\client_1\\ewallet.p12");
props.setProperty("javax.net.ssl.keyStoreType","PKCS12");
props.setProperty("javax.net.ssl.keyStorePassword","Password1");
try {
connection = DriverManager.getConnection(oracleURL, props);
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
System.out.println("Error code: " + e.getErrorCode());
System.out.println("SQL State: " + e.getSQLState());
e.printStackTrace();
return;
}
I'm able to compile the program and also run it using the following -
java -cp z:\jdk1.7.0_13\bin\ojdbc14.jar;z:\jdk1.7.0_13\bin OracleConnCheck
where:
z:\jdk1.7.0_13\bin\ojdbc14.jar - location of ojdbc14.jar
z:\jdk1.7.0_13\bin - Java classpath
OracleConnCheck - Java class
But I always encounter IO error accompanied with NL exception or SO exception. I checked out the Oracle and few articles related to the same exception in here but none addressed my exact problem. Could someone help? Thanks!
Edit: Adding the stacktrace -
Oracle JDBC Driver Registered!
Connection Failed! Check output console
Error code: 17002
SQL State: null
java.sql.SQLException: Io exception: NL Exception was generated
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:257)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:389)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:190)
at OracleConnCheck.establishConnection(OracleConnCheck.java:51)
at OracleConnCheck.main(OracleConnCheck.java:72)
------- The End -------
Make sure you use the latest 12.1.0.2 JDBC drivers. You can download from OTN. Also, you need to include osdt_core.jar and osdt_cert.jar.
Refer to SSL with Oracle JDBC whitepaper for more details.
"NL Exception was generated" indicates that there is a format error in the connection string. In your case you are missing = after DESCRIPTION.
Related
I'm trying to establish an SSL connection to a Sybase ASE 16.0 using JDBC driver. On the server, through interactive SQL, I am able to access the ASE server with SSL. I am using Jconnect 4 (jconn4.jar) and JDK 8 with the following connection string: jdbc:sybase:Tds:host:port/dbname?ENABLE_SSL=true. Server certificate imported to the java application trust store using keytool. I have gone through the other question already asked here but it doesn't help me to rectify this problem. Test code is below.
try {
Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
Connection con = DriverManager.getConnection(
"jdbc:sybase:Tds:host:5000/master?ENABLE_SSL=TRUE", "sa",
"password");
System.out.println("*********Connected**********");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select ##ssl_ciphersuite");
int i = 1;
while (rs.next()) {
System.out.println("Row: " + i);
System.out.println("ID: " + rs.getString(1).trim());
System.out.println(" ");
i = i + 1;
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
My JDBC code throwing below exception.
The connection should be established over the secure network but facing below error.
java.sql.SQLException: JZ00L: Login failed. Examine the SQLWarnings chained to this exception for the reason(s).
at com.sybase.jdbc4.jdbc.SybConnection.getAllExceptions(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.handleSQLE(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.a(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.handleHAFailover(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc4.jdbc.SybDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.testing.Test.main(Test.java:41)
I checked the Sybase log see the following error:
kernel SSL or Crypto Error Message: 'The SSL handshake failed. Root error: error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol'.
I went through the multiple Sybase documentation but no luck. Any help will be much appreciated. Thanks in advance!!
I'm trying to connect to mongodb server which is running on remote Linux machine using java code,
public static void main(String[] args) {
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("src/main/resources/mongodb.properties"));
String server = prop.getProperty("server");
String port = prop.getProperty("port");
int portnum = Integer.parseInt(port);
/**** Connect to MongoDB ****/
MongoClient mongo = new MongoClient(server,portnum);
System.out.println("Connected to database");
/**** Get database ****/
// if database doesn't exists, MongoDB will create it for you
DB db = mongo.getDB("test");
/**** Get collection / table from 'details' ****/
// if collection doesn't exists, MongoDB will create it for you
DBCollection table = db.getCollection("details");
/**** Insert ****/
// create a document to store key and value
BasicDBObject document = new BasicDBObject();
document.put("name", "test");
document.put("age", 24);
table.insert(document);
/**** Find and display ****/
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "test");
DBCursor cursor = table.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
It is working fine with my local mongo. But facing an issue while trying to connect with linux server.
Here is the error message,
Jan 18, 2016 12:23:02 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on server:port
java.io.IOException: couldn't connect to [server port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:88)
at com.mongodb.DBPort.findOne(DBPort.java:143)
at com.mongodb.DBPort.runCommand(DBPort.java:148)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
at com.mongodb.Mongo.getMaxBsonObjectSize(Mongo.java:620)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:254)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)
Jan 18, 2016 12:23:03 AM com.mongodb.DBTCPConnector initDirectConnection
WARNING: Exception executing isMaster command on sever port
java.io.IOException: couldn't connect to [server port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:88)
at com.mongodb.DBPort.findOne(DBPort.java:143)
at com.mongodb.DBPort.runCommand(DBPort.java:148)
at com.mongodb.DBTCPConnector.initDirectConnection(DBTCPConnector.java:548)
at com.mongodb.DBTCPConnector.checkMaster(DBTCPConnector.java:527)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:174)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)
Jan 18, 2016 12:23:04 AM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to server:port b/c of error
java.io.IOException: couldn't connect to [server:port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:84)
at com.mongodb.DBPort.say(DBPort.java:79)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:181)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)
Exception in thread "main" com.mongodb.MongoException$Network: can't say something
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:194)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:155)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:270)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:226)
at com.mongodb.DBCollection.insert(DBCollection.java:75)
at com.mongodb.DBCollection.insert(DBCollection.java:59)
at com.mongodb.DBCollection.insert(DBCollection.java:104)
at com.filecompare.util.MongoDBConnection.main(MongoDBConnection.java:42)
Caused by: java.io.IOException: couldn't connect to [server port] bc:java.net.ConnectException: Connection refused: connect
at com.mongodb.DBPort._open(DBPort.java:214)
at com.mongodb.DBPort.go(DBPort.java:107)
at com.mongodb.DBPort.go(DBPort.java:84)
at com.mongodb.DBPort.say(DBPort.java:79)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:181)
... 7 more
Steps that I generally follow to connect with mongo on linux are,
Login to server using Putty(windows) or ssh command(Mac).
type in mongo.
Connected to mongo, run mongo commands to view/ insert data.
But, in above java code I didn't include any login credentials. Do I have to do that. If so, could someone please guide me.
Are you sure you're providing the right values for your server and port property variables in mongodb.properties?
Default port for MongoDB is 27017.
Also, check your mongod.conf on the MongoDB server. This file is found, by default at /etc/mongod.conf and look for the following attribute:
bind_ip = 127.0.0.1
If this is set to 127.0.0.1 or localhost, then you won't be able to access your MongoDB server publicly from outside.
Now to fix this, change value of bind_ip to include your public ip address, so if your public address is 46.12.212.34, bind_ip will look like:
bind_ip = 127.0.0.1, 46.12.212.34
Save and exit the file and restart your MongoDB database. Your Java application should now work.
I have created a java applet that I need to connect to an oracle database. My problem for security reasons the Server from which the from which the oracle database is created is protected with firewall to prevent direct connection the database. I have the database ip address and the port from which the database is running and the Service name but I don't know how to connect to that server so that I can access the database. This is my sample source code that am trying to connect to the database.
String dbip="xx.xx.xx.xx";
String dbport="1523";
String serviceName="servicenamexample";
try {
url="jdbc:oracle:thin:#(description=(address_list="+
"(address=(protocol=tcp)(host="+dbip+")(port="+dbport+")))"+
"(source_route=yes)"+
"(connect_data=(INSTANCE_NAME="+serviceName+")))";
password="mypass";
username="myusername";
OracleDataSource odbc=new OracleDataSource();
odbc.setURL(url);
odbc.setPassword(password);
odbc.setUser(username);
conn=odbc.getConnection();
System.out.println("connection established !");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and this is the error that am getting.
java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at oracle.jdbc.pool.OracleDataSource.getPhysicalConnection(OracleDataSource.java:297)
at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:221)
im trying to create a connection to the MSSQL08 database on my computer :
this is my simple piece of code :
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws ClassNotFoundException {
String userName ="username";
String password ="password";
String url ="jdbc:microsoft:sqlserver://localhost; databaseName=Lando";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
try {
Connection conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException ex) {
Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
i get this Error :
SEVERE: null java.sql.SQLException: No suitable driver found for
jdbc:microsoft:sqlserver://localhost; databaseName=Lando at
java.sql.DriverManager.getConnection(DriverManager.java:596) at
java.sql.DriverManager.getConnection(DriverManager.java:215) at
javaapplication2.JavaApplication2.main(JavaApplication2.java:34)
when i change the URL to this : "jdbc:sqlserver://localhost; databaseName=Lando"
then i get this Error :
SEVERE: Java Runtime Environment (JRE) version 1.7 is not supported by
this driver. Use the sqljdbc4.jar class library, which provides
support for JDBC 4.0. Exception in thread "main"
java.lang.UnsupportedOperationException: Java Runtime Environment
(JRE) version 1.7 is not supported by this driver. Use the
sqljdbc4.jar class library, which provides support for JDBC 4.0. at
com.microsoft.sqlserver.jdbc.SQLServerConnection.(SQLServerConnection.java:304)
at
com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1011)
at java.sql.DriverManager.getConnection(DriverManager.java:571) at
java.sql.DriverManager.getConnection(DriverManager.java:215) at
javaapplication2.JavaApplication2.main(JavaApplication2.java:34)
I added the sqldjbc4.jar file to the project libraries in neatbeans, i also set the CLASSPATH in enviroment variables to the directory of the Microsoft Driver.
what could be the problem ?
I use Ubuntu & Net-beans. I make a java desktop database application from Netbeans wizard. I use MySQL database which I can open & run query from its Services → Databases. When I run it I got the Exception below. When I clean & Build the Project run jar file, same Exception. I have installed Windows in the same machine dual boot way, and also have the same MySQL database & Java. When I run the jar file I make using Ubuntu Netbeans, it perfectly run in Windows and show the database data. I did the other way also, I make the same java application in Windows Netbeans using the same database and it run perfectly both withing the Netbeans and jar file. But when I try to run it in Ubuntu, both in Netbeans & jar file show the below Exception.
[TopLink Info]: 2012.01.29 11:16:58.898--ServerSession(285416048)--TopLink, version: Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))
Jan 29, 2012 11:16:59 AM org.jdesktop.application.Application$1 run
SEVERE: Application class customerrecordsu.CustomerRecordsUApp failed to launch
Local Exception Stack:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Error Code: 0
at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:305)
... lot of more
Caused by: java.net.SocketException: Can't connect to SOCKS proxy:Connection refused
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:427)
... lot of more
Below is part of 'my.cnf' file removing comments.
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
user = mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
skip-external-locking
bind-address = 127.0.0.1
below is part of persistance.xml file
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/MyBusinessRecords"/>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value="password"/>
Netbeans wizard use JPA, & Top Link. I make a program without wizard by pure Java, it worked perfectly in Ubuntu as expected and show the database data. That program is below. Both above and below use same Java MySQL connector.jar.
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
int id = 0;
String name = null;
try {
String url = "jdbc:mysql://localhost:3306/MyBusinessRecords";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, "root", "5843");
if (con != null) {
System.out.println("A database connection has been establised!");
st = con.createStatement();
rs = st.executeQuery("select * from COUNTRIES");
while(rs.next()){
id = rs.getInt(1);
name = rs.getString(2);
System.out.println("id = " + id + " Name = " + name);
}
}
} catch (Exception e) {
System.out.println("Problem" + e.toString());
} finally {
if (con != null) {
try {
rs.close();
st.close();
con.close();
} catch (Exception e) {
System.out.println(e.toString());
}
con = null;
}
}
}
This is my system
java version "1.7.0_01"
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)
Ubuntu 11.10. OS type 64 bit
MySQL Version- MySQL 5.1.58-1ubuntu1
MySQL Client Version 5.1.58
Socket: /var/run/mysqld/mysqld.sock
I read the Q & A more then 15 in this site with same question, but that now one helped me. Please help me.
String url = "jdbc:mysql://localhost:3306/MyBusinessRecords";
Caused by: java.net.SocketException: Can't connect to SOCKS proxy:
a non-existant SOCKS proxy
bind-address = 127.0.0.1
You can try bind-address = 0.0.0.0 or jdbc:mysql://127.0.0.1:3306/MyBusinessRecords"
Try Java 1.6. I had a similar issue and changed back to Java 1.6 (I had upgraded to 1_7) and the issue went away. Probably need a different toplink or OracleDriver release for 1_7.