作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用众所周知的 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 数据库。 `预先感谢您。
最佳答案
仔细检查我的代码后,我发现了我的问题。请注意,在代码中,在顶部我创建了一个全局变量“private static Connection con;”但在 try block 内,我使用 Connection con = DriverManager.getConnection(jdbcUrl, username, password); 创建了另一个局部变量。所以发生的情况是局部变量被用来建立连接。所以连接 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;
}
}
关于java - 如何使用java将表单数据插入GoogleCloud Mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643391/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 3年前关闭。 Improve t
作为在 Google Cloud Platform 下使用 Nodejs 8 执行的 Cloud Function 实现的一部分,我必须将信息存储在 GCP Datastore 中。 我使用用 Jav
我是一名优秀的程序员,十分优秀!