Creation of a MySQL DB (in cloud) using a Google Cloud SQL instance (+ Java code)

In this weeks I had to create a DB, accessible from anywhere, to be integrated in an application and web app for a work project.

Here a breif description on my Notion, hope you like it 😁

Useful Java Code for MySQL db in Google Cloud SQL instance interaction:

// Template program for MySQL DB access and usage w. Java
public static void main(String args[]) throws IOException {
        System.out.println("test");
            try {
								// to import the JDBC driver
								// tou also need to add them to the project libraries (different for each IDE)
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection con = DriverManager.getConnection(
                        // DB_IP = SQL instance IP
												// DB_PORT = instance port (default should be 3306)
												// DB_NAME = database name
												// USERNAME
												// PSSW = password of the username choosen 
                        "jdbc:mysql://DB_IP:DB_PORT/DB_NAME", "USERNAME", "PSSW ");
                Statement stmt = con.createStatement();

								// example of INSERT command / ADD / REMOVE / ECC...
								String query = "INSERT INTO TableName(att1, att2, att3) VALUES" +
									"('" + att1_value+"', '"+att2_value+"', '"+att3_value+ "')" ;
								// use the syntax " 'varchar_value'" for text, with the single superscript
								// else just the variable for numeric values
                stmt.executeUpdate(query);

                
                //executeQuery to select from tables
                ResultSet rs = stmt.executeQuery("select * from TableName");
								// can add whatever you need to the above query of course

								// to get the attribute for each tuple returned
                while (rs.next())
                    System.out.println(rs.getInt(1) + " \t " + rs.getString(2) + " \t\t " + rs.getString(3) + " \t\t " + rs.getString(4));
                con.close();
            } catch (Exception e) {
                System.out.println(e);
            }//Try catch
   }//Main

Some of my posts:

Bitcoin, Gold, and Poop: How AI Got Symbolism Too Right

S&P 500 Sees Best Day Amid Trade War Tensions: A Paradox in the Markets

The Childish Game of Tariffs: US vs China

The Rise of AI-Generated Ghibli-Style Images and Their Impact

The Crispy Journey of French Fries: From Origins to World’s Best

The Juicy History of the Hamburger: How It Became a Global Icon

Why Going Out with Friends Matters & the Power of a Stress-Relief Dance

Why You Should Eat Pizza at Least a Few Times a Week

Ashton Hall’s Viral Morning Routine: Is It Discipline or Just Too Much?

ā€œI Need to Do Moreā€ – How to Face It and Accept Yourself

New Discoveries and Theories About the Pyramids of Egypt

Busy, Tired, but Happy: The Joy of a Productive Life

How AI is Simplifying Life: Everyday Benefits of Artificial Intelligence

Embracing Change and Self-Care: A New Chapter for This Blog

Kingdom of the Planet of the Apes – Trailer

John Wick 5 – Trailer

Quote #1

Pyhton – Loop Videos & Add Audio track

Number and Stats about the Universe

The Solar System

Useful links:

Advertisement