用httpserver写的一个java程序,在本地测试的时候连接一切正常,在远程服务器上却连接出错。
.我是在本地导出一个可执行的.jar文件上传到服务器上,然后用命令行java -jar执行的。
.服务器和本地的数据库是用同一段SQL脚本创建的....
.帐号密码我在ssh上用mysql -u root -p是可以正常登录的。
.在ssh上使用mysql -variable查看默认端口还是3306
.本地使用java -jar运行正常
.connector的jar包已经包括进去了
额,有哪些可能的原因呢。。。求答疑。如下
public Connection getConnection(){
String url = "jdbc:mysql://localhost:3306/bus";
try {
Class.forName("com.mysql.jdbc.Driver");
String user = "root";
String passWord = "****";
conn = DriverManager.getConnection(url,user,passWord);
if(conn!=null){
System.out.println("连接数据库成功");
}else{
System.out.println("连接数据库失败");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
异常如下:
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.GeneratedConstructorAccessor5.newInstance(Unknown Source)
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:1121)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2482)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)
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:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at Helper.MySql.getConnection(MySql.java:29)
at Helper.MySql.<init>(MySql.java:17)
at entrance.WHUTHandler.<init>(WHUTHandler.java:35)
at jwc.JwcScoreHandler.<init>(JwcScoreHandler.java:34)
at entrance.WHUT.main(WHUT.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
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.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:307)
... 23 more
MySql java
------解决方案--------------------
jdbc.url=jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
------解决方案--------------------
或者 在mysql的安装目录下找到my.ini
添加
wait_timeout=31536000
interactive_timeout=31536000
------解决方案--------------------
链接地址是本地的,你在远端的服务器上链接的mySQL在哪里,用IP链接。
jdbc:mysql://localhost:3306/bus
------解决方案--------------------
java.net.ConnectException: Connection refused
你MYSQL远程没开服务