当我尝试连接到我的 MySQL 数据库时,我不断收到此错误:
exception
javax.servlet.ServletException: JDBC Problem:
root cause
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.
root cause
java.net.ConnectException: Connection timed out: connect
这是一些代码:
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://mysql11.000webhost.com/database_name","user_name","password");
stmt = con.createStatement();
rs = stmt.executeQuery("INSERT into emailadresses('email') values ('"+email+"')");
} catch (ClassNotFoundException ex) {
Logger.getLogger(EmailServlet.class.getName()).log(Level.SEVERE, null, ex);
}catch(SQLException e){
throw new ServletException("JDBC Problem: ", e);
}
编辑:我的库中有 mysql-connector-java jar 文件
编辑:发现问题 我在我托管数据库的网站上寻找答案并找到了这个:
JDBC/ODBC is not supported here
这太糟糕了...
最佳答案
“在尝试将套接字连接到远程地址和端口时发生错误的信号。通常,连接被远程拒绝(例如,没有进程正在监听远程地址/端口)”。 - From Oracle Docs
查看您的连接字符串,确保数据库、用户名和密码输入正确。
另一个原因可能是您的数据库服务器未启动。
关于java - 无法连接到servlet中的mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13362835/