我试图在Java应用程序中使用JDBC建立连接以访问我的在线数据库,以便插入和查询表。以下是我目前正在:(实际IP /用户/密码编辑,但他们是对的,因为我已经做了类似从PHP脚本)Java - 使用JDBC连接到在线MySQL数据库时遇到的问题
String url = "jdbc:mysql://984.168.199.70/my_db_name";
String user = "my_username";
String pass = "my_password";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
Connection conn = (Connection) DriverManager.getConnection(url, user, pass);
stmt = (Statement) conn.createStatement();
但是,这并不工作,我得到的错误:
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.
然后错误日志很长。
所以只需指出:在使用PHP脚本之前,我已连接到此服务器,并且我已使用JDBC连接到本地主机MySQL数据库并与其进行交互。
感谢您的任何帮助。
任何特殊的端口打开了MySQL的,或者它的默认? – sgibly
另外,看看http://stackoverflow.com/questions/2102912/cant-make-jdbc-connection-to-mysql-using-java-intellij-and-linux – sgibly
至于具体问题,请检查http:// stackoverflow.com/questions/2983248/jdbc-with-mysql/2985169#2985169至于你已发布的代码,那些不必要的转换让我认为你实际导入了MySQL的具体实现类“Connection”和“Statement”。虽然不一定是你的问题的原因,这是一个*不好的做法*。总是声明'java.sql。*'接口!如果您升级驱动程序或切换数据库,您的JDBC代码将以其他方式破坏并需要完整的重写/重建。 – BalusC