Unable to connect to Oracle database using the JAR file - java

I'm using eclipse 2020 edition and I've added all libraries I need to connect to Oracle server like ojdbc7.jar and my code is like this:
public Connection SetDatabaseConnection() {
writeInLog("Connecting to IRB", 0);
if(openConnection()){
try {
productionPool.setDriverType("thin");
productionPool.setUser(username);
productionPool.setPassword(password);
productionPool.setPortNumber(Integer.parseInt(port));
productionPool.setServerName(IP);
productionPool.setServiceName(serviceName);
productionPool.setURL("jdbc:oracle:thin:#"+ _connStr.substring(_connStr.indexOf(":")+1));
productionPooledConnection = productionPool.getPooledConnection();
if (productionPooledConnection != null) {
//return true;
currentConnection = productionPooledConnection.getConnection();
logger.info("Connected to IRB server");
return currentConnection;
}
} catch (SQLException ex) {
logger.info("Unable to connect to IRB server, SQLException: "+ex.getMessage());
System.out.println(" (IRB-Exception) DB Exception: \n"+ ex);
}
}
}
my problem is: i can connect to the server while debugging or running the application in the eclipse but when I exported a JAR file the application stopped in this step.
in addition:
my code to open a connection:
private boolean openConnection(){
try {
productionPool = new OracleConnectionPoolDataSource();
productionPooledConnection = new OraclePooledConnection();
logger.info("openConnection(): Connected to IRB server \n");
return true;
} catch (SQLException e) {
e.printStackTrace();
logger.info("Unable to connect to IRB server , SQLException: "+e.getMessage());
}
logger.info("openConnection(): Unable to connect to IRB server \n");
return false;
}
The application never throws any excption it only write in the log file this statment: writeInLog("Connecting to IRB", 0);

I couldn't find the exact reason why this happened but I removed the JARs that cause the error FAT Jar Export: couldn't find the class-path for ...etc and import them again. It worked successfully.

Related

java.lang.ClassNotFoundException while executing DB connection statements

I have set of DB connection statements in my java file which takes the driver name, url, username and password from my property file and then try to make a connection. But I am keep getting error exactly in this try block . My code is
try {
Class.forName(properties.getProperty("DB2.database.driver"));
} catch (Exception e) {
System.err.println("Unable to locate database driver:" + e);
return null;
}
The error message is
Unable to locate database `driver:java.lang.ClassNotFoundException:
May i know why it is keep getting this error?

Can't receive mail from POP3 GMAIL by using Javamail, get SSLHandshakeException

I have JAVA application which work as service on Tomcat server. I need to develope service for receiving mail from GMAIL pop3 server. I use Javamail. I have written usual code, but when I deploy it on server I am getting
javax.mail.MessagingException: Connect failed;
nested exception is:
javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
As for development my Tomcat started on Localhost.
And this problemm looks like concerned with certificate.
My code is
public void downloadMail() {
try {
// connects to the message store and opens the inbox folder
Store store = session.getStore(protocol);
store.connect(username, password);
Folder folderInbox = store.getFolder(inbox);
folderInbox.open(Folder.READ_WRITE);
// fetches new messages from server
Message[] messages = folderInbox.getMessages();
for (int i = 0; i < messages.length; i++) {
Mail mail = extractMail(messages[i]);
messages[i].setFlag(Flags.Flag.DELETED, true);
}
// disconnect
folderInbox.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println( "No provider for pop3.");
ex.printStackTrace();
} catch (MessagingException ex) {
System.out.println( "Could not connect to the message store");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println( "Can not save file or open directory");
ex.printStackTrace();
}
}
I am getting exception on line store.connect(username, password);
I use properties:
mail.pop3.host=pop.gmail.com
mail.pop3.port=995
mail.pop3.ssl.enable=true
mail.pop3.protocol=pop3
mail.pop3.inbox=INBOX
Please help me to solve this problem. I never worked with Tomcat and certificates before!

FTPSClient.listFiles() not working for NonStop/Tandem System

I am writing a small FTPS client that will download Enscribe files from NonStop/Tandem and will be processes in Windows. I am using the Apache Commons Net API to achieve this.
I am able to download and upload files from and to NonStop/Tandem. But I am not able to list the files and the directories using the listFiles() and/or mlistDir() methods present under org.apache.commons.net.ftp.FTPClient class.
Below is my code to list the files present in the current working directory.
FTPSClient client = new FTPSClient(false);
try {
client.connect(serverAddress, serverPort);
if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
if (client.login(userName, passwd)) {
System.out.println(client.getReplyString());
// Set protection buffer size
client.execPBSZ(0);
// Set data channel protection to private
client.execPROT("P");
// Enter local passive mode
client.enterLocalPassiveMode();
// Get Current Working Directory
client.printWorkingDirectory();
System.out.println(client.getReplyString());
FTPFile[] files = client.listFiles();
// Logout
client.logout();
System.out.println(client.getReplyString());
} else {
System.out.println("Login failed...");
}
// Disconnect from Server
client.disconnect();
System.out.println("Disconnected from Host...");
} else {
System.out.println("Connection to Host failed...");
System.out.println("Error Code - " + reply);
}
} catch (Exception e) {
e.printStackTrace();
}
I get the following error while executing the code:
org.apache.commons.net.ftp.parser.ParserInitializationException: Unknown parser type: Nonstop J-series Server : J06.19.
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:169)
at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:94)
at org.apache.commons.net.ftp.FTPClient.__createParser(FTPClient.java:3377)
at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3334)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3012)
at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3065)
at com.connect.ssl.FTPSTest.main(FTPSTest.java:57)
I even tried to set the FTPClient configuration as UNIX as below, but it didn't helped.
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
Can anyone help me with this.

Java derby database server won't start anymore

I inherited some source code that uses a derby database but starting the server doesn't work anymore.
public void startDatabase(){
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
System.setProperty("derby.system.home", "D:\\runtime-my.product\\log");
NetworkServerControl nsc = new NetworkServerControl(InetAddress.getByName("localhost"), 1527)
nsc.start(null);
nsc.ping();
catch (Exception e){
e.printStackTrace();
}
}
When nsc.ping() is exectued, the following exception is thrown:
Exception: java.lang.Exception: DRDA_NoIO.S:Could not connect to Derby Network Server on host 127.0.0.1, port 1527: Connection refused: connect
Is there anything obvious missing or wrong with those lines of codes?
Check whether the server is started. You need to start the server explicitly. or via
setting the system property derby.drda.startNetworkServer=true.
It takes a while from the NetworkServerControl constructor returns, before the socket is ready for connections. On my machine about 50 ms. I changed your example like so:
NetworkServerControl nsc = new NetworkServerControl(InetAddress.getByName("localhost"), 1527);
nsc.start(null);
for (int i = 0; i < 10; ++i) {
try {
System.out.println("Attempting to ping...");
nsc.ping();
break;
} catch (Exception e) {
System.out.println(e.getMessage());
}
Thread.sleep(10);
}
It succeeded on the 5. attempt...

Simple way to keep files updated from remote server using java?

I was hoping to use Java to access a remote server, and access the files and keep my files updated, or update the files on the server.
Is there a simple way to access a remote server using the username#host and password, which would allow me to upload and download files?
Thanks
You can use JSch to access files via ssh remotely.
Use the proper tool for the job: rsync
If you like to connect to a machine while opening a ssh connection ,
to run OS command you can use trilead.
This is an example to a method that will open a connection.
public static Connection newConnectionNoPassword(String host, String username, File privateKey) {
Connection newConn = new Connection(host);
try {
newConn.connect(); // Ignoring ConnectionInfo returned value.
//If the authentication was successful the authenticated connection will be returend
if ( newConn.authenticateWithPublicKey(username, privateKey, null)){
return newConn;
}else{
newConn.close();
return null;
}
} catch (IOException ioe) {
newConn.close();
ioe.printStackTrace();
return null;
}
}
If you use maven you can get it by adding the following dependency to your pom.xml:
<dependency>
<groupId>com.trilead</groupId>
<artifactId>trilead-ssh2</artifactId>
<version>build213-svnkit-1.3-patch</version>
</dependency>
In order to upload\download files from\to a server you can use trilead SCPClient.
Here is an example of downloading files from a remote server to a local folder:
public void downloadFiles(String[] remoteFiles, String localDir) throws IllegalArgumentException, IOException {
checkNotEmpty(localDir);
checkNotEmpty(remoteFiles);
File dir = new File(localDir);
if (!dir.exists() || !dir.mkdirs()) {
throw new IOException("Failed to create local directory : " + localDir);
}
SCPClient scp = new SCPClient(this.conn);
try {
scp.get(remoteFiles, localDir);
} catch (IOException e) {
throw new IOException("Failed to copy remote files to local folder", e);
}
}
Hope it helps..

Categories