[英]Error occur while connecting to database mysql workbench:java.sql.SQLNonTransientConnectionException: Could not create connection to database server
[英]“Could not create connection to database server” netbeans google cloud sql error
我正在尝试使用java httpServlet和netbeans创建我的第一个API,这些API将根据其示例和文档连接到Google Cloud上的数据库。 我尚未创建数据库,但已获得创建连接所需的凭据和变量。 因此,我从github下载了该项目,当我尝试从netbeans打开它时,出现了一些与依赖项有关的问题。因此,我解决了这些问题,将凭据替换为其值并运行该项目。 不幸的是,抛出了一个错误: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
我进行了一些搜索,但未得到任何结果...此代码可能是错误的? 如果数据库安全性在云中不可见,则会出现错误? 任何帮助都超过了赞赏。
public class ListTables {
public static void main(String[] args) throws IOException, SQLException {
String instanceConnectionName = "<foo:foo:bar>";
String databaseName = "myDatabaseName ";
String username = "myUsername ";
String password = "<myPass>";
if (instanceConnectionName.equals("<insert_connection_name>")) {
System.err.println("Please update the sample to specify the instance connection name.");
System.exit(1);
}
if (password.equals("<insert_password>")) {
System.err.println("Please update the sample to specify the mysql password.");
System.exit(1);
}
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
databaseName,
instanceConnectionName);
try{
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
/* try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("select * from wf_accounts;");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}*/
}catch(Exception ex){
System.out.println("Error: " + ex.toString());
}
更新
当我这样做时,会引发相同的错误:
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
"",
"");
有什么提示吗?
首先,您必须检查mysql服务器是否正在运行。如果您是Windows用户,则只需
Go to Task Manager
Go to Services
then search for your Mysql server(eg:for my case its MYSQL56)
then you will see under the status column it says its not running
by right clicking and select start
如果它已经在运行,则必须按照mysql文档中的步骤进行操作,
您应该考虑在应用程序中使用连接之前使连接有效性到期和/或对其进行测试,或者增加服务器为客户端超时配置的值,或者使用Connector / J连接属性
autoReconnect的=真
避免这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.