How to connect wordpress database with JAVA ? (mysql) - java

I dont know much about the connection proccess i'm trying to connect wordpress database without creating new database with my remotedesktop IP in mysql. Normally i can connect localhost and i can connect to database that created with remote ip. In my server mydatabase host name/Ip adress is localhost, as i said i dont know the whole connection between url request,wp-config file and mysql. I think i'm sending request with url to wp-config and it is connecting to my localhost. Anyway i want to connect to my remotedesktop's localhost with JAVA but i couldn't. Let me explain with code . Hope i could explain myself.
Thanks in advance.
Here are the codes :
wp-config.php
define('DB_NAME', 'mydbname');
define('DB_USER', 'mydbuser');
define('DB_PASSWORD', 'mydbpass');
define('DB_HOST', '127.0.0.1');
My connection code :
public class Dt_page {
public static DataSource getDataSource(int server) {
String database_driver = null;
String database_adres = null;
String dbuser = null;
String dbpass = null;
ConnectionFactory connectionFactory;
switch (server) {
case 1://profile server
database_driver = "com.mysql.jdbc.Driver";
database_adres = "jdbc:mysql://MY_REMOTE_DESKTOP_IP:3306/?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true";
dbuser = "mydbuser";
dbpass = "mydbpass";
}
try {
Class.forName(database_driver);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Dt_page.class.getName()).log(Level.SEVERE, null, ex);
}
Properties props = new Properties();
props.put("user", dbuser);
props.put("password", dbpass);
props.put("autoReconnect", "true");
props.put("autoReconnectForPools", "true");
props.put("validationQuery", "SELECT 1 from dual;");
props.put("testWhileIdle","true");
connectionFactory = new DriverManagerConnectionFactory(database_adres, props);
GenericObjectPool connectionPool = new GenericObjectPool(null, 12, GenericObjectPool.WHEN_EXHAUSTED_GROW, 60*30, 12,true,true);
GenericKeyedObjectPoolFactory keyedObjectPoolFactory = new GenericKeyedObjectPoolFactory(null);
PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory,
connectionPool,
keyedObjectPoolFactory,
"select 1",
30,
null,
false,
true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
return dataSource;
And SQL
QueryRunner qr;
qr = new QueryRunner(Dt_page.getDataSource(1));
try {
List<Object[]> v = qr.query("Select meta_value from mydbname.wp_favorite", new ArrayListHandler());
System.out.println(v.size());
for (Object[] objects : v) {
for (Object object : objects) {
System.out.println(object);
}
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
Error Log:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2367)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2288)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:187)
at org.apache.commons.dbcp.DriverManagerConnectionFactory.createConnection(DriverManagerConnectionFactory.java:78)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1188)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
at org.apache.commons.dbutils.AbstractQueryRunner.prepareConnection(AbstractQueryRunner.java:175)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:304)
at tazedizi.TazeDizi.main(TazeDizi.java:29)
Caused by: 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2306)
... 19 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
... 21 more

Two things to check:
Are you sure that your mysql server is configurated to listend on MY_REMOTE_DESKTOP_IP ?
Are you sure that your firewall (iptable...) allows connexion on MY_REMOTE_DESKTOP_IP on tpc port 3306 ?

Related

Forwarding Java HTTP Requests through another server

I have two AWS Instance I1 and I2 having Ip addresses as ip1 and ip2 respectively.What I want to achieve is:
I want to hit a url from I1(using java httpclient) and want that request should go through I2 instance i.e to outside world it should appear that the request is coming from instance I2.
For Example :
I am executing the below-mentioned code from instance I1 :
class Test {
public static void main(String[] args) {
String url = "https://api.ipify.org?format=json",
proxy = "ip2";
Proxy p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, 80));
URL server = new URL(url);
Properties systemProperties = System.getProperties();
HttpURLConnection connection = (HttpURLConnection)server.openConnection(p);
connection.connect();
String theString = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8);
System.out.println(theString);
}
}
The Output should be public Ip of instance I2. But it is throwing the exception:
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient$1.run(HttpClient.java:515)
at sun.net.www.http.HttpClient$1.run(HttpClient.java:513)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.http.HttpClient.privilegedOpenServer(HttpClient.java:512)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:553)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1199)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1050)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:162)
at sun.net.www.protocol.https.HttpsURLConnectionImpl$connect.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at com.exp.Test.main(Test.java:24)
I have googled for the above mentioned use case but was not able to find anything. Any help is appriciated.
This issue is fixed.
Installed squid server on instance I2.

Can not connect to MySQL server - Java [duplicate]

This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 5 years ago.
I have problem with connecting to MySQL Server installed on Ubuntu 16.04 server but I got these Errors:
Exception in thread "main" 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at schoolcontroller.tools.DataBase.mysql.DataAccess.connect(DataAccess.java:45)
at schoolcontroller.tools.DataBase.mysql.DataAccess.main(DataAccess.java:40)
I can connect to the server using MySQL Client applications like Navicate and MySQL workbench with no error.
Here is my code
public DataAccess() throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
System.out.println("Connecting");
new DataAccess().connect();
}
public void connect() throws SQLException {
String connection = "jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=UTF-8";
conn = DriverManager.getConnection(String.format(connection, "IPAddress", "3306", "DBname"), "user", "pass");
System.out.println("Connected");
st = conn.createStatement();
}
The port and the username the database name and and the IP is correct.
You could try to register the driver class to the DriverManager
String driver = "com.mysql.jdbc.Driver"
Class driverClass = Class.forName(driver);
Driver driverObj = (Driver)driverClass.newInstance();
DriverManager.registerDriver(driverObj);
If you still have problems, then you could try to deregister all drivers in first place and then add the driver again like this:
String driver = "com.mysql.jdbc.Driver"
List<Driver> allDriver = new ArrayList<Driver>();
Enumeration drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements())
allDriver.add((Driver)drivers.nextElement());
for (Driver d : allDriver)
DriverManager.deregisterDriver(d);
Class driverClass = Class.forName(driver);
Driver driverObj = (Driver)driverClass.newInstance();
DriverManager.registerDriver(driverObj);

Error when sending email in Java

I have a problem with sending emails in Java. I am using the JavaMail api to send emails. I have a program which downloads an email attachment using pop3, reads the content, does some manipulation and then sends the result in an email using smtp.
The program works fine until the last step where it sends the email and I get the following exception.
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.adidas.monitoring.SendMail.sendMail(SendMail.java:46)
at com.adidas.monitoring.MainClass.main(MainClass.java:115)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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 com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
I am using the following code to send the email
public void sendMail(String strMailContent)
{
MimeMessage emailMessage;
Properties emailProperties = new Properties();
try {
emailProperties.load(new FileInputStream("Email.ini"));
} catch (IOException e){
DataLogger.logger.error(e.getMessage());
}
/*Get the properties from the email configuration file*/
String From = emailProperties.getProperty("Email.From");
String ToAdd = emailProperties.getProperty("Email.To");
String CC = emailProperties.getProperty("Email.CC");
String Subject = emailProperties.getProperty("Email.Subject");
String Host = emailProperties.getProperty("Email.Gateway");
emailProperties.setProperty("mail.smtp.host", Host);
Session emailSendSession = Session.getDefaultInstance(emailProperties);
try{
emailMessage = new MimeMessage(emailSendSession);
emailMessage.setFrom(new InternetAddress(From));
emailMessage.addRecipients(javax.mail.Message.RecipientType.TO,ToAdd);
emailMessage.addRecipients(javax.mail.Message.RecipientType.CC,CC);
emailMessage.setSubject(Subject);
emailMessage.setText(strMailContent);
Transport.send(emailMessage);
}
catch (Exception mailEx)
{
mailEx.printStackTrace();
}
}
The above code works fine when I use it with other programs. But when I am using it along with the pop3 code in this case, I have this error. Strange thing is the host is shown as localhost even though I set the property "mail.smtp.host".
Any help on this topic is really appreciated.

Connect to mySQL using eclipse

I am trying to run a JAVA program which inserts values into mySQL database. When I run the program, it says Connection refused: connect I have included mySQL jars also. What is the problem? Can anyone help me out
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DataLogs {
public static void main(String[] args) throws SQLException,ClassNotFoundException {
Connection connection = null;
try {
//int i=0;
String strDate="", strTime="";
// Register MySQL JDBC driver to be known by
// the DriverManager object.
Class.forName("com.mysql.jdbc.Driver");
// Get a connection to database. We prepare the
// connection information here such as database
// url, user and password.
String url = "jdbc:mysql://localhost:8080/sampledatabase";
String user = "root";
String password = "root";
connection = DriverManager.getConnection(url, user, password);
// Create a statement object instance from the
// connection
Statement stmt = connection.createStatement();
// We are going to execute an insert statement.
// First you have to create a table that has an
// ID, NAME and ADDRESS field. For ID you can use
// an auto number, while NAME and ADDRESS are
// VARCHAR fields.
for(int i=1;i<=100;i++){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String formattedDate = sdf.format(date);
strDate=getFormatedDate();
strTime=getTime();
String mm=strDate.substring(3, 5);
String yy=strDate.substring(8, 10);
String hh=strTime.substring(0, 2);
String mi=strTime.substring(3, 5);
String ss=strTime.substring(6, 8);
String dd=strDate.substring(0, 2);
String date1 = ""+yy+"-"+mm+"-"+dd+" "+hh+":"+mi+":"+ss;
String sql= "INSERT INTO `site_values_"+i+"` VALUES("+i+",5316,0,0,130,89,904,171,1006,96,4000,"+ss+","+mi+","+hh+","+dd+","+mm+","+yy+",84753,0,0,0,0,0,0,0,0,0,'Short Circuit Shutdown','Shutdown',1,'"+date1+"')";
// Call an execute method in the statement object
// and passed the sql or query string to it.
stmt.execute(sql);
}
// After this statement is executed you'll have a
// record in your users table.
} catch (ClassNotFoundException e) {
System.err.println("Could not load database driver!"+e);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
public static String getFormatedDate() {
String strDate="";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdfdate = new SimpleDateFormat("dd/MM/yyyy");
strDate= sdfdate.format(cal.getTime());
return strDate;
}//getFormatedDate
public static String getTime() {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
java.util.Date date = new java.util.Date();
String datetime = dateFormat.format(date);
return datetime;
} //getTime
}
This is what I see in the console
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at solarnmsstandard.DataLogs.main(DataLogs.java:27)
Caused by: 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 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
... 15 more
Check if your connection parameters are correct (username, password) and especially the port where the MySql server is running.
check whether your mysql server is running or not, if it is not running then you will get this exception
java.net.ConnectException: Connection refused: connect
to run server
go to <mysql installation path>/bin/ and run mysqld.. to start the mysql server.
even after if you get the same error, provide the full stack trace.
check your port 8080 is free or not
if you are use linux run the follwing command and restart your server.
sudo kill sudo lsof -t -i:8080
sudo kill sudo lsof -t -i:8005
sudo kill sudo lsof -t -i:8009
I would be very suspicious of port 8080 being used as the port for MySQL; it really should be port 3306. So that would have to be checked properly as 8080 is usually the port number for an Application Server instance (such as GlassFish or Tomcat).

GWT Problem with MySQL Connection

I am working on a GWT project in which I need to make some MySQL queries. I have handled RPC properly and in the server-side I am trying to make a mysql connection but am running into an exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1013)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2412)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
.
.
.
more
.
.
.
I created a very simple java class to test out my code to confirm that my connection syntax and use is proper, and have confirmed that syntax is not a problem because with the very same code in a simple only main method java app I can create a connection and query the database properly. I have made sure to have mysql-connector-java-5.1.16-bin.jar in the classpath as well as the /lib folder in the WEB-INF folder.
Here is the class that I am using in order to create a connection:
public class DB_Connection {
protected Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql:///",
"",
"");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return conn;
}
}
public class Login extends DB_Connection {
public User getUser(String email) {
User user = new User();
user.setUserEmail(email);
String query = "";
try {
Connection conn = getConnection();
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(query);
while(result.next()) {
user.setUserId(result.getInt("rvuser_id"));
System.out.println(user.getUserId());
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return user;
}
It seems my problem happens all the way at the beginning in DB_Connection. Does anyone know what is making this happen? It is strange to me, because as a standalone the code works fine.
Another thing I just realized that I am at home now and don't even have access to that database server as it is on a local network and I am not connected through VPN. So it must be failing somehow before it even attempts to make the connection.
Thanks!
com.google.appengine ..
Does it means you are deploying on Google App Engine?
If so, MySQL,JDBC and many other things are restricted.

Categories

Resources