Cannot Connect Mysql Database In Httpservlet Context

Thanhpv

New Member
30/3/18
28
6
3
Hi,
I know that I have not learnt enough for this task but this is a very simple case that fusses me up.

The whole thing is here. Someone please help to test with a mysql database.

Code:
import org.apache.commons.lang3.exception.ExceptionUtils;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
 
 
public class DBTest extends HttpServlet {
 
 
private Connection connect;
 
private static String DB_USER = "some";
private static String DB_PASSWORD = "some";
private static String DB_URL = "jdbc:mysql://localhost:3306/plantrek";
 
 
public DBTest() {
 
 
}
 
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response){
String message = "";
try {
 
new DBConnManager(DB_URL, DB_USER, DB_PASSWORD);
 
} catch (Exception e) {
message = message + ExceptionUtils.getStackTrace(e);
}
finally {
 
response.setHeader("message", message);
}
 
 
}
 
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) {
 
doGet(request, response);
}
 
 
public static void main(String[] args) throws Exception {
new DBConnManager(DB_URL, DB_USER, DB_PASSWORD);
//Connect perfectly
 
}
 
static class DBConnManager {
 
private Connection connection;
 
private DBConnManager(String dbURL, String user, String pwd) throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.cj.jdbc.Driver");
this.connection = DriverManager.getConnection(dbURL, user, pwd);
}
 
public Connection getConnection(){
return this.connection;
}
}
 
 
}
Connection established perfectly in the main function, but cannot in doPost() or doGet().

This is what it spitted out and I do not know why.
"message: com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) at java.sql.DriverManager.getConnec..."
 

Joe

Thành viên VIP
21/1/13
2,969
1,310
113
Normally I won't touch any issue that belongs to the 3rd party software or DB. Today I make an exception for you **== Using the term localhost as an IP is the worst choice with MySQL. For the main() it's static and it works so far because it won't act as a client, while your doGet() is a client (method of HttpServlet) and MySQL driver tries to connect to a local domain socket or so (usually on Linux System) and not to TCP socket to the server. Try to work with the default IP 127.0.0.1 (same for localhost). This coerces the driver to go with TCP socket.
 
  • Like
Reactions: Thanhpv

Thanhpv

New Member
30/3/18
28
6
3
For the main() it's static and it works so far because it won't act as a client
You are exact. The database server accepted local clients, but via TCP/IP the database server 'ignored'. Perhaps, security problems.
update:
Yes. I temporarily lowed down the database server security and it works.
 
Sửa lần cuối:

quydtkt

Administrator
1/11/19
389
38
28
27
Normally I won't touch any issue that belongs to the 3rd party software or DB. Today I make an exception for you **== Using the term localhost as an IP is the worst choice with MySQL. For the main() it's static and it works so far because it won't act as a client, while your doGet() is a client (method of HttpServlet) and MySQL driver tries to connect to a local domain socket or so (usually on Linux System) and not to TCP socket to the server. Try to work with the default IP 127.0.0.1 (same for localhost). This coerces the driver to go with TCP socket.
You are pro =D>
 
  • Like
Reactions: Joe