我肯定做错了什么或者我错过了一些东西,但是无法弄清楚那是什么。我安装了XAMPP Apache / MYSQL。我在本地连接,一切正常。我尝试更进一步,远程访问数据库。
问题
当我在浏览器中输入myip / xampp时,我只看到" 连接已重置"但是对于其他人都可以吗? (Xampp欢迎页面)
在以前工作时无法通过JDBC error com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
进行连接,只更改主机名。
这是代码。
public DBConnect(String whichDataBase){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+whichDataBase+"?characterEncoding=UTF-8","monty","some_pass");
st = con.createStatement();
} catch (Exception e) {
System.out.println("error "+e);}
}
在数据库状态选项卡上,它看起来像" connect?" (请参阅host但command = sleep
并在java中获取错误,如上所述)。但是,当我使用VPN连接和工作正常? O_O
我做过的事情
路由器端口转发80和3306(在http://www.yougetsignal.com/tools/open-ports/上测试)
更改了socket = "TCP/IP"
my.ini
评论#enable-named-pipe
和#skip-networking
bind-address="0.0.0.0"
(另请查看127.0.0.1
)
数据库用户在localhost
和%
我是初学者,所以感谢您的帮助!
编辑:
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1036)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2232)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBConnect.<init>(DBConnect.java:17)
at Poro.actionPerformed(Poro.java:297)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.DefaultComboBoxModel.addElement(Unknown Source)
at javax.swing.JComboBox.addItem(Unknown Source)
at Poro.<init>(Poro.java:231)
at Poro.main(Poro.java:287)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 24 more
答案 0 :(得分:0)
尝试以这种方式:
public class MysqlConnect {
static String template = "jdbc:mysql://localhost/%s?useEncoding=true&characterEncoding=UTF-8&user=%s&password=%s";
public void connect(String database, String user, String password) {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(String.format(template, database, user, password));
//...
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
请致电Class.forName("com.mysql.jdbc.Driver").newInstance()
并将username
和password
添加到您的连接字符串中。
答案 1 :(得分:0)
我有同样的错误。我尝试过这种方式(修改 dit 的答案代码)并且它对我有用:
public class SqlConnection {
static String template = "jdbc:mysql://localhost/%s?useEncoding=true&characterEncoding=UTF-8&user=%s&password=%s";
public static Connection dbConnector() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(String.format(template, <database-name>, <username>, "password"));
System.out.println("Connected succesfully");
return connection;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
注意:您必须在 DriverManager中修改数据库名称,用户名和密码。 getConnection 方法。 最后,您可以致电:
SqlConnection.dbConnector();
现在,我可以看到&#34;已成功连接&#34;。