how to connect java with wamp mysql server - java

I am new java programmer, I try to setup jdbc connnectivity with wamp mysql server.
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection( "jdbc:mysql://182.73.151.36:3306/sbtsystem?user=sbtuser&password=Pm#dmin&useUnicode=true&characterEncoding=UTF-8");
182.73.151.36 this is server IP address. I can't understand how to connect java with wamp mysql server

Try This:
First run these commands
ping 182.73.151.36
telnet 182.73.151.36 3306
And make 182.73.151.36 mysql eligible for remote connection
After that change your connection line and run this program
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://182.73.151.36:3306/sbtsystem","sbtuser","Pm#dmin");

You need to first download the jdbc driver files for mysql
(download from here)

The code looks fine. You need to check the network configurations. Try the following commands to see if the server is accessible.
ping 182.73.151.36
telnet 182.73.151.36 3306

supposing your network configuration is correct,
I guess your mysql server on 182.73.151.36 is not configured for remote connection,

Related

JT400 - JDBC connection refused from IBMi machine but working from Windows machine

JT400 - The JDBC connection is working fine when connecting to a IBMi machine from windows machine.
DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
String sysname = "xxx.xxx.xx.xxx";
String xref = "IBMISQLLIB";
String url = "jdbc:as400://" + sysname + ";translate binary=true;prompt=false;naming=sql;libraries=" + xref;
Connection connection = DriverManager.getConnection(url, "USERNAME", "PASSWORD");
print("Connection created successfully");
But fails to create the connection by using the same code - when running the code on the same IBMi machine (by a runnable jar on 'QSH Command Entry'), ERROR:
java.sql.SQLException: The application requester cannot establish the
connection. (A remote host refused an attempted connect operation.
(Connection refused)) at
com.ibm.as400.access.JDError.createSQLExceptionSubClass(JDError.java:887)
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:610)
at com.ibm.as400.access.JDError.throwSQLException(JDError.java:568)
at
com.ibm.as400.access.AS400JDBCConnectionImpl.setProperties(AS400JDBCConnectionImpl.java:3522)
at
com.ibm.as400.access.AS400JDBCDriver.prepareConnection(AS400JDBCDriver.java:1430) at
com.ibm.as400.access.AS400JDBCDriver.initializeConnection(AS400JDBCDriver.java:1280)
at
com.ibm.as400.access.AS400JDBCDriver.connect(AS400JDBCDriver.java:403)
at java.sql.DriverManager.getConnection(DriverManager.java:675) at
java.sql.DriverManager.getConnection(DriverManager.java:258)
Any idea what is going wrong now ??
because this was working some days back
Same code is working on other IBMi servers
Some more details about the IBMi machine are:
No access right issue for the username used in connection profile
No firewall is setup on IBMi machine
TCP/IP configuration is verified as explained - Configuring TCP/IP on IBM i
Is the database host server active? Use WRKACTJOB to see if jobs name QZDASOINIT are running.
If not, try starting the host server with the command STRHOSTSVR *DATABASE.
If you're running your jar file from QSH, you need to make sure that multi-threading is allowed. That could possibly gum things up.
The same code should work just fine if it works on the PC unless you are using a different version of the jt400.jar file possibly as well.
Try using localhost also instead of a system name.
Or even writing a small Java class to open and close a connection.
The problem was related to default TCP/IP port, which was not listening the JDBC connection request.
And finally the problem got resolved by restarting the IBMi machine.
:)

connect amazon mysql with JDBC through remote server

I had a problem here, is it possible that JDBC connecting to the amazon MySQL server remotely? I searching on the Internet for solution but ended up receiving the error
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I am using jsch.jar to connect the remote server, then call up JDBC connector trying to connect MySQL DB, I had successfully connected to the remote server yet I cannot connect MySQL DB on amazon, here is my code
testPutty t = new testPutty();
t.connect("admin", "", "xx:xx:xx:xx", "openSSHPrivateKey", 22);
t.connect();
System.out.println("connected");
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://staging.ppyhcsxnlkji.ap-southwest-1.rds.amazonaws.com:3306/staging","superuser","password");
System.out.println("hello world");
t.close();
First set up the tunnel with SSH. You haven't shown what testPutty is so I'll assume it's a thin Java wrapper around a command line invocation of Putty. The command line option for setting up a tunnel is
-L[localport]:[host]:[remoteport]
For your example this would be
-L3306:staging.ppyhcsxnlkji.ap-southwest-1.rds.amazonaws.com:3306
This sets up a tunnel that forwards connections to local port 3306 to port 3306 on the given host (staging...amazonaws.com) on port 3306, routed through the host that is the target of the SSH command (in your sample xx:xx:xx:xx).
Then, your connection string needs to use localhost instead of the actual remote:
jdbc:mysql://localhost:3306/staging
If 3306 on your local host is not available because you are also running a local copy of MySQL, just choose a different unused port.

Connect to sql server from java with jdbc(windows authentication mode)

I need to connect to Sql Server 2008 from java using jdbc 4.0.
I have a very simple code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;" +
"integratedSecurity=true;";
Connection con = DriverManager.getConnection(connectionUrl);
But i have this error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
...
I follow this answer:https://stackoverflow.com/a/12524566/1554397
I added jdbc4.jar in Libraries/Compile
SQL Server Browser windows service is running.
In SQL Server Network Configuration i selected Enebled on TCP/IP properties.
I Set TCP Address to 1433.
On Run,VM Options i put -Djava.library.path=my path to sqljdbc_auth.dll
And copy in JDk ,in bin sqljdbc_auth.dll.
What should I do?
EDIT:
When write in cmd telnet localhost 1433 i get 'Could not open connection to the host,on port 1433'
If using Window authentication you can do something like:
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);
and then add the path to sqljdbc_auth.dll as a VM argument (you need sqljdbc4.jar in the build path).
Take a look here for a short step-by-step guide showing how to connect to SQL Server from Java should you need more details. Hope it helps!

Database connection string for a remote database server named 'localhost'

I have a remote mysql database server setup on a machine myuniversity.edu and server is named 'localhost'. On it I have the database named 'MyDatabase'.
I want to connect it through Java.
The connection urls that I have tried are:
jdbc:mysql://myuniversity.edu/localhost
jdbc:mysql://myuniversity.edu/localhost/MyDatabase
jdbc:mysql://myuniversity.edu:3306/MyDatabase
but I get the `Connection refused: connect` exception.
Could someone please tell what the connection url should be in this case?
Not really sure if your machine name is myuniversity.edu, you can instead try the IP Address with the connection string, Localhost is the name for loopback network interface and accessible on that machine only. Also make sure if your default port for mysql (may be 3306) is open. With IP address your connection string would look like:
jdbc:mysql://192.168.0.123/MyDatabase
With IP and port it would be:
jdbc:mysql://192.168.0.123:3306/MyDatabase
(You need to replace your IP in the above string)
I'ts impossible to connect remotely without (IP) address
try this approach
if you want to connect it via internet :
OPEN CMD on your computer
in CMD write ping myuniversity.edu (for example ping google.com)
then you will get an ip address of the website and you can copy the ip
then try this approach :
Connection con;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://THE IP ADDRESS :3306/DatabaseName");
System.out.println("CONNECTED");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
Ok so here's what I did to fix the issue:
In my.cnf file, I changed the bind-address from '127.0.0.1' to the
'host ipaddress'. This allows connecting to the remote mysql server
but would not allow access for any remote host trying to connect to
it.
To fix that, I added an entry in user table with host '%'. This allows remote hosts to connect to the database.
Now I can connect to the database with jdbc:mysql://serverIpAddress:3306/MyDatabase

Trying to connect to mysql database with jdbc

I have ubuntu server running on virtual machine locally and I need to connect to mysql database there.
Database is in place and jdbc driver too. The only problem is that only way at the moment for my connection can be something like http://local/phpmyadmin/index.php?db=sandbox and that can't be used in jdbc connection string.
Hope, that somebody can advise a solution.
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url,"username", "pwd");
Replace localhost with the IP of your VM. You have to use something other than NAT for networking in your VM (like Host-only, internal or bridged assuming VirtualBox).
You probably have to configure your Ubuntu firewall to let the connection through.
Also, you have to set mysql up to accept connections from the outside.
In the file /etc/mysql/my.cnf edit bind-address to your ip:
bind-address = your-vms-ip

Categories