我正在尝试通过我的通过eclipse在java上构建的应用程序将数据插入Google Cloud mysql数据库中。代码如下:
package com.example.dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class dao {
private static Connection con;
public static Connection getcon(){
try{
String instanceConnectionName = "edu-vitae1";
String databaseName = "eduvitae";
String username = "root";
String password = "edu";
String jdbcUrl = String.format(
"jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root&useSSL=false",
databaseName,
instanceConnectionName);
Connection con = DriverManager.getConnection(jdbcUrl, username, password);
Class.forName("com.mysql.jdbc.GoogleDriver");
//con=DriverManager.getConnection("jdbc:google:mysql://edu-vitae1>/edu-vitae?user=root&password=root");
}
catch (Exception e) {
System.out.println(e);
}
return con;
}
}
部署项目后,出现此错误:
java.sql.SQLException:未找到适合jdbc的驱动程序:mysql:// google / eduvitae?cloudSqlInstance = edu-vitae1&socketFactory = com.google.cloud.sql.mysql.SocketFactory&user = root&password = root&useSSL = false
任何人都可以帮助我解决此问题。 预先感谢您
答案 0 :(得分:1)
3天后,我在代码中发现一个愚蠢的错误。注意,在我的代码中,我首先建立了连接,然后使用Class.forName(“ com.mysql.jdbc.GoogleDriver”)加载了驱动程序;这是不正确的。您应该先使用Class.forName加载驱动程序,然后再使用DriverManager.getConnection建立连接。请不要犯这种愚蠢的错误,就像我在这种愚蠢的错误上浪费了三天一样。以下代码供您参考:
package com.example.dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class dao {
private static Connection con;
public static Connection getcon(){
try{
String instanceConnectionName = "edu-vitae1";
String databaseName = "eduvitae";
String username = "root";
String password = "root";
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = String.format(
"jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=root&useSSL=false",
databaseName,
instanceConnectionName);
Connection con = DriverManager.getConnection(jdbcUrl, username, password);
//con=DriverManager.getConnection("jdbc:mysql://35.200.134.221/edu","root","root");
}
catch (Exception e) {
System.out.println(e);
}
return con;
}
}