我正在尝试使用以下常见的MySQL命令将表单数据上传到GoogleCloud MySQL数据库:
try {java.sql.Connection conn=dao.getcon();
PreparedStatement ps=conn.prepareStatement("insert into register values(?,?,?,?,?,?,?,?)");
ps.setString(1,"Subhanshu");
ps.setString(2,"bigu");
ps.setLong(3,54566522);
ps.setLong(4,5456662);
ps.setString(5,"subhanshu");
ps.setString(6,"hello");
ps.setString(7,"hello");
ps.setString(8,"online");
ps.execute();
conn.commit();
flag=1;
System.out.print("Success");
}
catch(Exception e){
}
但是,数据没有插入数据库中。另一方面,可以很容易地建立连接,如下所示:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Aug 02, 2018 7:08:00 PM com.google.cloud.sql.mysql.SocketFactory connect
INFO: Connecting to Cloud SQL instance [edu-vitae-211606:asia-south1:edu-vitae1].
Aug 02, 2018 7:08:00 PM com.google.cloud.sql.mysql.SslSocketFactory getInstance
INFO: First Cloud SQL connection, generating RSA key pair.
Aug 02, 2018 7:08:01 PM com.google.cloud.sql.mysql.SslSocketFactory fetchInstanceSslInfo
INFO: Obtaining ephemeral certificate for Cloud SQL instance [edu-vitae-211606:asia-south1:edu-vitae1].
Aug 02, 2018 7:08:03 PM com.google.cloud.sql.mysql.SslSocketFactory createAndConfigureSocket
INFO: Connecting to Cloud SQL instance [edu-vitae-211606:asia-south1:edu-vitae1] on IP [35.200.134.221].
我用来与GoogleCloud数据库建立连接的代码如下:`
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 = <USER>;
String password = <PASSWORD>;
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = String.format(
"jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<USER>&password=<PASSWORD>&useSSL=false",
databaseName,
instanceConnectionName);
//Connection con=DriverManager.getConnection("jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<USER>&password=<PASSWORD>");
Connection con = DriverManager.getConnection(jdbcUrl, username, password);
//con=DriverManager.getConnection("jdbc:mysql://localhost:3306/edu",<USER>,<PASSWORD>);
//String url= String.format("jdbc:mysql://35.200.134.221:3306/edu-vitae-211606:asia-south1:edu-vitae1");
//Connection con= DriverManager.getConnection(url, <USER>,<PASSWORD>);
}
catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
请告诉我如何将表单数据插入Google Cloud Database。 ` 预先谢谢你。
答案 0 :(得分:1)
仔细检查我的代码后,我确定了我的问题。在代码中,我在顶部创建了一个全局变量“ private static Connection con;”。但是在try块中,我使用Connection con = DriverManager.getConnection(jdbcUrl,username,password);创建了另一个局部变量。所以发生的事情是使用了局部变量来建立连接。所以包含了con con变量空连接。我在下面发布解决方案供参考:
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 = <USER>;
String password = <PASSWORD>;
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = String.format(
"jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<USER>&password=<PASSWORD>;&useSSL=false",
databaseName,
instanceConnectionName);
con = DriverManager.getConnection(jdbcUrl, username, password);
//Connection con=DriverManager.getConnection("jdbc:mysql://google/eduvitae?cloudSqlInstance=edu-vitae-211606:asia-south1:edu-vitae1&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<USER>&password=<PASSWORD>;");
//con=DriverManager.getConnection("jdbc:mysql://localhost:3306/edu",<USER>,<PASSWORD>;);
//String url= String.format("jdbc:mysql://35.200.134.221:3306/edu-vitae-211606:asia-south1:edu-vitae1");
//Connection con= DriverManager.getConnection(url, <USER>,<PASSWORD>;);
}
catch (Exception e) {
e.printStackTrace();
}
return con;
}
}