My method must trying to connect to some other DB server. When I run my application on Linux Server - there was all OK. When I run it on Windows OS - I have java.net.SocketTimeoutException: Read timed out
private boolean pingServer(String host) {
String username = "username";
String password = "password";
boolean successful;
AS400 as400 = new AS400(host, username, password);
SocketProperties socketProperties = as400.getSocketProperties();
socketProperties.setLoginTimeout(TIMEOUT_MILLISEC);
socketProperties.setSoTimeout(TIMEOUT_MILLISEC);
as400.setSocketProperties(socketProperties);
try{
successful = as400.validateSignon();
} catch (AS400SecurityException e) {
successful = true;
} catch (IOException e) {
successful = false;
} catch (TransactionException e) {
successful = false;
}
as400.disconnectAllServices();
as400 = null;
return successful;
}
Can anybody give me some advice?
Thanks.
AS/400 connections use a lot of network ports which must be available to you.
Try disabling all firewalls between you and the server and try again. This include the software firewall (may be part of the antivirus suite) on the Windows system
Related
I'm trying to connect into FTP server, the problem is that wehn I try to log into the server with specific user it returns false.
This is the link for the ftp website: https://url.publishedprices.co.il/
The user name is: DorAlon
The password should be empty
When I'm connecting through the website it's working, it works too when i'm connecting from FileZilla, but when I do so from Java it returns false for this user. There are several more users that logged in successfully to this server, and only this user doesn't work.
Here's my code:
String server = "url.retail.publishedprices.co.il";
int port = 21;
String pass = " ";
FtpClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
boolean a = ftpClient.login("xxx", "");
boolean b = ftpClient.login("yyy", "");
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
// Checking whether the client is connected - in that case we'll logout and disconnect from the server
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
In the end the boolean a holds false, and b holds true.
It's mean the connection with the server is okay and also the login, but for the specific user it doesn't work.
How could this be possible?
I would like for some help, Thanks.
Ok - so I have been working on this for some time (sometimes not working on it), and I am ALMOST there but not yet. I am trying to tunnel into a mysql db and was able to successfully connect ssh using Jsch. But when I try to connect to the database, it gives me an "Access denied for user 'usernamne'#'localhost' (using password: YES)
Here is the following code: (of course I masked out sensitive info)
public void connect(){
String user = "user";
String password = "password";
String host = "host.com";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = null;
Properties info = new Properties();
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
jsch.addIdentity("/Users/user/.ssh/id_rsa", password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
if(session.isConnected()){
session.disconnect();
}
session.connect();
conn = DriverManager.getConnection("jdbc:mysql://localhost:4417/dbname","dbuser", "dbpassword");
System.out.print("Connection successful!" + "\n\n");
System.out.print("Connection:" + conn + "\n\n");
Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery("SELECT * FROM TABLENAME limit 1");
System.out.print(resultSet);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (JSchException j){
j.printStackTrace();
}
}
I've read other posts on here but I have not found the exact solution to my problem. I am able to ssh in correct. And what's even more interesting is that if I do the same exact procedure command_line, it works fine. So I don't know why it wouldn't work here.
Try this. it might help you . Already tested in my setup. Enjoy. :)
create db user in remote mysql db.
CREATE USER 'myuser'#'clientip' IDENTIFIED BY 'pass';
CREATE USER 'myuser'#'%' IDENTIFIED BY 'pass';
grant ALL ON mydb.* to 'myuser'#'clientip';
grant ALL ON mydb.* to 'myuser'#'%';
FLUSH PRIVILEGES;
Code for ssh tunneling.
int assigned_port;
final int local_port=8080;
// Remote host and port
final int remote_port=3306;
final String remote_host="192.168.150.139";
try {
JSch jsch = new JSch();
// Create SSH session. Port 22 is your SSH port which
// is open in your firewall setup.
session = jsch.getSession("sshuser", remote_host, 22);
session.setPassword("sshpassword");
// Additional SSH options. See your ssh_config manual for
// more options. Set options according to your requirements.
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("Compression", "yes");
config.put("ConnectionAttempts","2");
session.setConfig(config);
// Connect
session.connect();
// Create the tunnel through port forwarding.
// This is basically instructing jsch session to send
// data received from local_port in the local machine to
// remote_port of the remote_host
// assigned_port is the port assigned by jsch for use,
// it may not always be the same as
// local_port.
assigned_port = session.setPortForwardingL(local_port,
remote_host, remote_port);
} catch (JSchException e) {
LOGGER.log(Level.SEVERE, e.getMessage()); return;
}
if (assigned_port == 0) {
LOGGER.log(Level.SEVERE, "Port forwarding failed !");
return;
}
// Database access credentials. Make sure this user has
// "connect" access to this database;
// these may be initialized somewhere else in your code.
final String database_user="myuser";
final String database_password="pass";
final String database = "mydb";
// Build the database connection URL.
StringBuilder url =
new StringBuilder("jdbc:mysql://localhost:");
// use assigned_port to establish database connection
url.append(assigned_port).append ("/").append(database).append ("?user=").
append(database_user).append ("&password=").
append (database_password);
try {
Class.forName(
"com.mysql.jdbc.Driver");
System.out.println(url.toString());
java.sql.Connection connection =
java.sql.DriverManager.getConnection(url.toString());
String q="select * from customer";
PreparedStatement ps=connection.prepareStatement(q);
ResultSet rs= ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1)+rs.getString(2));
}
} catch (ClassNotFoundException |
java.sql.SQLException e) {
LOGGER.log(Level.SEVERE, e.getMessage());
e.printStackTrace();
}
finally{
session.disconnect();
}
How can I connect with java that interacts with my database? I am using the XAMPP for mysql
And I am having a problem knowing what port I am using.. I just copied the port which other are using from the internet But I dont really know what is my port number for the database
Also how do I check for the port going to data base?? //localhost:3306
HERE is my code:
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.print("COnnection succesfull");
}
catch(Exception ex)
{
System.out.print("Unable to connect ");
}
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?user=root&password=";
Connection con = DriverManager.getConnection(url);
System.out.print("Connection Stablished");
}
catch(Exception ex)
{
System.out.print("Connect cannot stablished");
}
I think you don't need to specify port if you want to connect to XAMPP's MySQL. Try this, this works for me:
String dbn = "yourdatabasename";
String usr = "root";
String pwd = "";
String connectionURL = "jdbc:mysql://localhost/"+dbn;
try {
// Load the Driver class.
Class.forName("com.mysql.jdbc.Driver");
// If you are using any other database then load the right driver here.
con = (Connection) DriverManager.getConnection (connectionURL, usr, pwd);
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
To check which port MySQL is running on, you can check a file 'my.ini' located in
C:\Program Files (x86)\MySQL\MySQL Server 5.1 (Windows 8, might vary, but its the install directory).
The default is 3306 to my knowledge.
The code looks right, though I usually pass the username and password with
DriverManager.getConnection(url, username, password);
I'm new to servers and databases. I've been trying to test out a java application that connects, reads and modifies a Mysql database that is hosted on amazon ec2.
System.out.println("-------- MySQL JDBC Connection Testing ------------");
String DNS = "MYDNS";
String myDBname = "DBNAMe"
String MYSQLUSER = "USER";
String MYSQLPW = "PW";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://" +DNS+#"/"+myDBname, MYSQLUSER, MYSQLPASS)
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
currentDir = "broke";
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
The issue is that despite being able to connect using the credentials via Mysql workbench, adding 3306 to my security group, binding 0.0.0.0 to mysql config, I still can't get a connection going (after doing some searching of other similar problems on SO)
Anyone have any insight as to what could be wrong? I have tried many variations of the getConnection() arguments.
connection = DriverManager.getConnection("jdbc:mysql://username#ec2-host/myDBname, MYSQLUSER, MYSQLPASS)
Our network team only allow us to connect to our third party client thru proxy server.
Is there a way to add a proxy server to FTPS client of apache commons net?
If it is not possible, can you tell a way on how to do it.
By the way here's the code that is working outside of the company network
String server = "ftp.xxxx.com";
String username = "username";
String password = "password";
String remoteFile = "xmlSR.xml";
String localFile = "c:/downloadedfile.xml";
String protocol = "TLS"; // TLS / SSL
int timeoutInMillis = 3000;
boolean isImpicit = false;
FTPSClient client = new FTPSClient(protocol, isImpicit);
client.enterLocalActiveMode();
client.setRemoteVerificationEnabled(false);
client.setActivePortRange(50000, 50200);
client.setDataTimeout(timeoutInMillis);
client.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
try {
int reply;
client.connect(server);
client.login(username, password);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.execPBSZ(0);
client.execPROT("P");
System.out.println("Connected to " + server + ".");
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.listFiles();
boolean retrieved = client.retrieveFile(remoteFile,
new FileOutputStream(localFile));
} catch (Exception e) {
if (client.isConnected()) {
try {
client.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
return;
} finally {
System.out.println("# client disconnected");
client.disconnect();
}
even we tried to add some system property for proxyHost and proxyPort
System.setProperty("http.proxyPort", "80");
System.setProperty("http.proxyHost", "yyyy.com");
System.setProperty("ftp.proxyPort", "80");
System.setProperty("ftp.proxyHost", "yyyy.com");
System.setProperty("socksProxyPort", "80");
System.setProperty("socksProxyHost", "yyyy.com");
error message
Could not connect to server.
java.net.UnknownHostException: ftp.xxxx.com
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1202)
at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
at java.net.InetAddress.getAllByName(InetAddress.java:1083)
at java.net.InetAddress.getAllByName(InetAddress.java:1019)
at java.net.InetAddress.getByName(InetAddress.java:969)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)# client disconnected
at org.apache.commons.net.SocketClient.connect(SocketClient.java:285)
at com.ti.itg.peit.bom.TestingApache.main(TestingApache.java:44)
Thank you very much.
Gerald
Its possible..see the link below....
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/ProxySelector.java
Well using Apache's common lib to access FTP using proxy server, please set the RemoteVerificationEnabled to false after creation of FTPClient object.
eg:
FTPClient fc = new FTPClient();
fc.setRemoteVerificationEnabled(false);
You can use java.net.Proxy class which is for Java 1.5 and above, this
is used to set or unset the Proxy per connection basis
By using the java.net.ProxySelector, will determine a Proxy for each Connection.