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:
Useful links: