Connecting Java App to my online database JDBC MySql - java

I am trying to fetch data from my Online database, but following error is coming. However it is working fine for my local database.
Java Code
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.sql.*;
import javax.swing.JOptionPane;
public class BankingSystem extends JFrame {
public static void main(String[] args) throws Exception
{
int ur=0;
int PIN;
String ID;
int userSelection=0;
Boolean pinCorrect = false;
int amountDeposite;
int amountWithdraw;
int updateResult;
Boolean isExit=false;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://mysql11.000webhost.com/a46854323_javaapp";
String user = "a46854323_javaapp";
String pass="password";
Connection con = DriverManager.getConnection(url,user,pass);
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
}
and I get the following error:
`
Exception in thread "main" java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection timed out: connect
java.net.SocketException
MESSAGE: java.net.ConnectException: Connection timed out: connect
STACKTRACE:
java.net.SocketException: java.net.ConnectException: Connection timed out: connect at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at BankingSystem.main(BankingSystem.java:22)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at BankingSystem.main(BankingSystem.java:22)
What can be the solution ?

I have gone through all the comments above and found that 000host.com doesn't provide the facility that its database can get accessed by outside its own space. So with little googling i found http://www.db4free.net that provide free mySql database for testing purpose. And in this way my problem get solved. Thanks to all comentators.

Related

Getting SSL error while trying to connect to SQL-Server 2014

Getting error while trying to connect to SQL Server 2014.
I'm using JRE7 and sqljdbc4-4.0.jar
here is my java code:
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Testing {
public static void main(String[] args) {
String url = "jdbc:sqlserver://localhost:1433;databaseName=test;encrypt=false";
String user = "sa";
String pass = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException: " + e);
}
try (Connection con=DriverManager.getConnection(url,user, pass);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM exts WHERE ext = 1001");){
if(rs.next()) {
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
} else {
System.out.println("NOT_FOUND");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Here is the full stack trace:
java.ext.dirs: C:\Cisco\CallStudio\eclipse\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:dd9cabc2-3683-4a14-857c-eeefd2751853".
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1668)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1323)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at com.test.Testing.main(Testing.java:32)
Caused by: java.io.IOException: SQL Server did not return a response. The connection has been closed. ClientConnectionId:dd9cabc2-3683-4a14-857c-eeefd2751853
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.ensureSSLPayload(IOBuffer.java:651)
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.readInternal(IOBuffer.java:708)
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.read(IOBuffer.java:700)
at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.readInternal(IOBuffer.java:895)
at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.read(IOBuffer.java:883)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
at sun.security.ssl.InputRecord.read(InputRecord.java:480)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1618)
... 7 more
SSL exceptions occur when TLS version mismatch. As you are using Java 7, Try to add sslProtocol=TLSv1.2 to your connection string.

Java RMI Method Call Connection Refused with OpenVPN

Grrr, I feel that this should be so obvious yet I can't find the solution.
For testing purposes I have created a simple Client and Server for an RMI application. I can successfully run the client and server over a local network. I can also successfully run the application when connected through a Windows 10 Built-In VPN connection. However, when connected via an OpenVPN connection I receive a Connection refused to host exception when calling a method (String response = lookup.helloTo(txt); in the Client code) on the remote object returned from the successful call to lookup = (RMIInterface)Naming.lookup("//192.168.10.14/MyServer");.
All my Google searches state that the java.rmi.server.hostname property must be set which I have done in this case. I am thinking that the issue may be between the VPN client IP address (10.8.0.14) and the server IP address (192.168.10.14). I can ping the server IP address and telnet to port 1099 on the server successfully.
Here is my code. I was hoping if someone could see something that I am missing or doing wrong or if I am on the right track regarding the VPN Client IP <=> Server IP.
Remote Interface:
package devel;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RMIInterface extends Remote {
public String helloTo(String name) throws RemoteException;
}
Server:
package devel;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ServerOperation extends UnicastRemoteObject implements RMIInterface {
private static final long serialVersionUID = 1L;
protected ServerOperation() throws RemoteException {
super();
}
#Override
public String helloTo(String name) throws RemoteException {
System.err.println(name + " is trying to contact!");
return "Server says hello to " + name;
}
public static void main(String[] args) {
try {
java.lang.System.setProperty("java.rmi.server.hostname", "192.168.10.14");
java.rmi.registry.LocateRegistry.createRegistry(1099);
Naming.rebind("//192.168.10.14/MyServer", new ServerOperation());
System.err.println("Server is ready");
} catch(Exception exc) {
System.err.println("Server exception: " + exc.toString());
exc.printStackTrace();
}
}
}
Client:
package devel;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.swing.JOptionPane;
public class ClientOperation {
private static RMIInterface lookup;
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
lookup = (RMIInterface)Naming.lookup("//192.168.10.14/MyServer");
String txt = JOptionPane.showInputDialog("What is your name?");
String response = lookup.helloTo(txt);
JOptionPane.showMessageDialog(null, response);
}
}
Exception received:
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 192.168.10.14; nested exception is:
java.net.ConnectException: Connection timed out: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy0.helloTo(Unknown Source)
at devel.ClientOperation.main(ClientOperation.java:17)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
... 8 more

Failing to connect client server to local network

I was learning socket programming in Java and had written a simple program for connecting a client server to a socket server on a local host. But every time when I run the ClientServer program it gives an error stating connection :refused.
I am enclosing both the client code and server code that i wrote and also enclosing the console output.
package Classes;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientSer {
/**
* #param args
* #throws IOException
* #throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
Socket s = new Socket("localhost",1029);
OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
String str = "tEST mESSAGE";
os.write(str);
os.flush();
}
}
This is the server code:
package Classes;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketSer {
public static void main(String args[]) throws Exception{
ServerSocket ss = new ServerSocket(1029);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String str = br.readLine();
System.out.print(str);
}
}
Console output:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Classes.ClientSer.main(ClientSer.java:18)
In the server, do you know what interface it's listening on?
I would start the server and then use telnet to try to connect rather than your client. You might find the server is NOT listening on localhost. Or maybe it's listening on every interface (127.0.0.1 plus your local network). I just don't know if I trust SocketServer(portNumber).
I would consider creating an unbound SocketServer and then use socket.bind(), feeding it an InetSocketAddress object constructed with both host and port.

getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in java [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 6 years ago.
I am trying to connect to data base.Earlier it was working fine. but now some jars has got deleted or something I don't know (red cross were coming on them). But I downloaded all jars again (mysql-connector,commons-io-2.4)still I am getting classnotfound exception at Class.forName("com.mysql.jdbc.Driver");
Here is my code
package abc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC_oracle {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//step1 load the driver class
Class.forName("com.mysql.jdbc.Driver");
//step2 create the connection object
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd");
//step3 create the statement object
Statement statement = connection.createStatement();
statement.executeUpdate("sql_query");
ResultSet rs=statement.executeQuery("sql_query");
while(rs.next())
System.out.println("I am triyng to print data");
//step5 close the connection object
connection.close();
}
}
This is error
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at deiniteguide.JDBC_example.main(JDBC_example.java:24)
I have tried using try catch also it is not working for me.
If you are using netbeans, you can go to your library folder in your project. Right click and add library and select mysql driver. Sorry couldn't comment due to less reputation and it would be easier if you can post with what error message you get. Also use try catch in your program so that it will be easier to locate the error. Your code can be like this:
public class JDBC_oracle {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//step1 load the driver class
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
System.out.println("Class not found" +e);
//step2 create the connection object
try{
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","user_name", "pswrd");
}catch(Exception es){
System.out.println("connection couldn't be made "+es);
}
//step3 create the statement object
try{
Statement statement = connection.createStatement();
statement.executeUpdate("sql_query");
ResultSet rs=statement.executeQuery("sql_query"); }
catch(Exception er){
System.out.println("Query wasnot executed "+er);
}
while(rs.next())
System.out.println("I am triyng to print data");
//step5 close the connection object
connection.close();
}
}

Windows authentication failing when connecting to sql server using JDBC

I have SQL server on a machine under the domain DOM005 and I am trying to connect to it using JDBC driver from my local machine that is in another domain. I am using windows authentication for the connection through domain administrator account.
I cannot use "integratedSecurity=true" in my connection URL since both machines are in different domain. Hence I use "authentication=ActiveDirectoryIntegrated" but I am getting the following error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'DOM005\administrator'. ClientConnectionId:84224bcd-d2f1-4386-aadf-397c1ef3eadf
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:251)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:81)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:3077)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2360)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2346)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1793)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1404)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1068)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:904)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:451)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1014)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at TestJDBCsqlServer.main(TestJDBCsqlServer.java:23)
Here is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestJDBCsqlServer {
private static final String SQLSERVER_URL_NTLM = "jdbc:sqlserver://10.20.13.4:1433;databaseName=DB1;authentication=ActiveDirectoryIntegrated";
public static void main(String[] args) throws Exception {
boolean isNTLM = true;
Connection conn = null;
Statement stmt = null;
if(isNTLM){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(SQLSERVER_URL_NTLM, "DOM005\\administrator", "abc");
stmt = conn.createStatement();
ResultSet set = stmt.executeQuery("select 1");
System.out.println("NTLM connection is successful");
set.next();
System.out.println(set.getString(1));
}finally{
if(stmt != null)
stmt.close();
if(conn!=null)
conn.close();
}
}
}
}
Can anyone help me resolve the issue?

Categories