我一直在寻找SOF的答案,但无论我做什么都行不通。我见过类似于我的问题,所以请不要发表评论,说这已经得到了回答。正如我之前所说,我发现以前的答案对我不起作用。
基本上我只是搞乱SQL并决定制作一个简单的程序来保存我''商店'中的所有“客户”。这是Store.java(Main class)的代码
import java.util.UUID;
import com.galanjulio.store.customers.Customer;
import com.galanjulio.store.sql.SQL;
public class Store {
public static void main(String[] args){
System.out.println("============================");
SQL sql = new SQL();
sql.checkTable();
System.out.println("============================");
Customer c = new Customer();
c.setName("Tom");
c.setUniqueId(UUID.randomUUID());
c.setCredits(0);
sql.addToDatabase(c);
}
这是我的SQL.java类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.galanjulio.store.customers.Customer;
public class SQL {
private Connection con;
private Statement st = null;
private String connectionString =
"jdbc:mysql://167.114.67.166:3306/store?user=user&password=pass";
public void checkTable(){
String query = "CREATE TABLE IF NOT EXISTS `customers` (uuid VARCHAR(36), name VARCHAR(15), credits INT);";
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionString);
st = con.createStatement();
st.executeUpdate(query);
System.out.println("Database created successfully...");
}catch(SQLException se){
//Handle errors for JDBC
System.out.println(se.getMessage());
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(st != null)
st.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(con != null)
con.close();
}catch(SQLException se){
System.out.println(se.getMessage());
}//end finally try
}//end try
}
public void addToDatabase(Customer c){
String query = "INSERT INTO `customers` (uuid, name, credits) VALUES (" + c.getUniqueId() + ", " + c.getName() + ", " + c.getCredits() + ");";
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(connectionString);
st = con.createStatement();
st.executeUpdate(query);
System.out.println("Added " + c.getName() + " to the database");
}catch(SQLException se){
//Handle errors for JDBC
System.out.println(se.getMessage());
}catch(Exception e){
//Handle errors for Class.forName
System.out.println("Class.forName has thrown an exception!");
}finally{
//finally block used to close resources
try{
if(st!=null)
st.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(con != null)
con.close();
}catch(SQLException se){
System.out.println(se.getMessage());
}//end finally try
}//end try
}
这是我得到的错误
Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused: connect
STACKTRACE:
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 java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.galanjulio.store.sql.SQL.checkTable(SQL.java:26)
at com.galanjulio.store.Store.main(Store.java:15)
** END NESTED EXCEPTION **
Last packet sent to the server was 0 ms ago.
============================
Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused: connect
STACKTRACE:
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 java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.galanjulio.store.sql.SQL.addToDatabase(SQL.java:57)
at com.galanjulio.store.Store.main(Store.java:24)
** END NESTED EXCEPTION **
Last packet sent to the server was 0 ms ago.
这是我在SOF上提出的第一个问题,抱歉如果格式不正确。 感谢您的帮助!
答案 0 :(得分:1)
我的第一个猜测是&#34;防火墙&#34;。无论是在您身边,在您的服务器方面,还是两者兼而有之。
检查的一种方法是查看任何客户端是否可以从PC连接到删除服务器。例如,&#34; telnet SERVERNAME 3306&#34;。或者从PC上试用mySQL客户端。
我的第二个猜测是,一旦您完全验证了TCP / IP级连接,就是mySQL权限。
看这里:
http://www.thegeekstuff.com/2010/08/allow-mysql-client-connection/
http://www.rackspace.com/knowledge_center/article/mysql-connect-to-your-database-remotely
请注意&#34;拨款&#34;可以申请&#34; db资源&#34; +&#34; db user&#34; +&#34; db客户端主机&#34;。
还要注意&#34; mySQL权限&#34;通常导致不同的错误,而不是&#34;连接被拒绝&#34;。再次 - 检查您的主机名,检查您的防火墙,验证网络连接。
&#39;希望有所帮助!