简体   繁体   中英

How to insert form data into GoogleCloud Mysql database using java

I am trying to upload form data into GoogleCloud MySQL database using the commonly known MySQL commands as given below:

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){

    }

But,the data is not being inserted into the database.The connection on the other hand is being established easily as you can see below:

    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].

The code I used to establish connection with GoogleCloud Database is given below:`

 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;
    }

}

Please tell me how to insert data of a form into Google Cloud Database. ` Thank You in advance.

After carefully inspecting my code,I identified my problem.Notice in the the code,at the top I have created a global variable "private static Connection con;" but inside the try block I have created another local variable using Connection con = DriverManager.getConnection(jdbcUrl, username, password);.So what was happening was that the local variable was being used to establish the connection.So the connection con variable contained null connection.I am posting the solution below for reference:

   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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM