Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

Cannot Connect Mysql Database In Httpservlet Context

Discussion in 'Xây dựng ứng dụng server-client desktop' started by Thanhpv, 3/12/19.

  1. Thanhpv

    Thanhpv New Member

    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.
     
  2. quydtkt

    quydtkt Administrator

  3. Joe

    Joe Thành viên VIP

    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.
     
    Thanhpv likes this.
  4. Thanhpv

    Thanhpv New Member

    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.
     
    Last edited: 4/12/19
  5. Thanhpv

    Thanhpv New Member

  6. quydtkt

    quydtkt Administrator

    You are pro =D>
     
    Joe likes this.
  7. Joe

    Joe Thành viên VIP

    44 yrs old, 26 yrs with IT, etc. all that makes me somehow "IT-Proof" :)
     
    Last edited: 4/12/19
  8. quydtkt

    quydtkt Administrator

    I wish I would have this in-depth knowledge
     

Chia sẻ trang này

Loading...