Compare commits

...

6 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 867176ba94 Databases
- Practice the concurrency types, result set types and scroll types
- Added information on `Statement`'s `executeUpdate(String sql)` method's return value
2023-09-24 13:33:08 +02:00
Tristan B. Velloza Kildaire e5d2992023 Flow
- Added an example of the compile-time checked switch block expression type
2023-09-21 12:36:09 +02:00
Tristan B. Velloza Kildaire 9476bc4b09 Flow
- Added old-style switch expressions with multiple statements
2023-09-21 12:30:37 +02:00
Tristan B. Velloza Kildaire 81af1bc831 Flow
- Added an example of a switch expression
2023-09-21 12:24:54 +02:00
Tristan B. Velloza Kildaire 3209a6baa6 Flow
- Added basic switch statement turorial
- Added example of default case usage
2023-09-21 12:15:23 +02:00
Tristan B. Velloza Kildaire 691078f716 EnumTests
- Yes, you CAN override the `toString()` of an enum
2023-09-21 11:35:49 +02:00
6 changed files with 261 additions and 9 deletions

Binary file not shown.

View File

@ -14,6 +14,50 @@ import java.sql.Statement;
public class App
{
public static void main(String[] args) throws SQLException
{
/**
* Basics usage of JDBC
*/
basicsUsage();
/**
* More intricate usage
*/
intricateUsage();
}
/**
* More intricate usage
*/
private static void intricateUsage() throws SQLException
{
Connection dbConn = DriverManager.getConnection("jdbc:sqlite:mydb");
/**
* Note that <code>ResultSet.TYPE_SCROLL_SENSITIVE</code> means that
* when scrolling through the rows in the {@link ResultSet} that it
* will reflect the current data in the underlying database
* (if supported). Therefore you get the latest data AS changes are
* made by some other thread or process
*/
PreparedStatement stmt = dbConn.prepareStatement("select * from users", ResultSet.TYPE_SCROLL_SENSITIVE);
// stmt.set
ResultSet results = stmt.executeQuery();
while(results.next())
{
System.out.println("Result: "+results.getString("name"));
}
results.close();
stmt.close();
dbConn.close();
}
/**
* Basics usage of JDBC
*/
private static void basicsUsage() throws SQLException
{
/**
* First we must get a {@link Connection}
@ -23,6 +67,14 @@ public class App
*/
Connection dbConn = DriverManager.getConnection("jdbc:sqlite:mydb");
/**
* This sets whether all {@link ResultSet} objects
* should be closed when a commit() is called
* on the {@link Connection} object. Sometimes
* you may want to have them stay open.
*/
dbConn.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
/**
* If we wish to run any statements then we must
* first create a {@link Statement}, as shown below.
@ -67,8 +119,17 @@ public class App
/**
* We can read data back using a {@link ResultSet}
*
* We can set <code>ResultSet.CONCUR_UPDATEABLE</code>
* which allows you to update the rows as you process
* them through each ResultSet result.
*
* The first argument controls how results are fetched.
*
* We, however, haven't tried it here because it is
* unsupported by the SQLITE driver.
*/
Statement readStmt = dbConn.createStatement();
Statement readStmt = dbConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = readStmt.executeQuery("select * from users");
/**
@ -86,6 +147,11 @@ public class App
System.out.println("Age: "+rs.getInt("age"));
System.out.println("Name: "+rs.getString("name"));
/**
* NOTE: This is not supported in this JDBC driver!
*/
// rs.updateString("name", rs.getString("name")+"GOLEMN!");
// They can be index-based too but string `getX(...)`s
// ... are clearer
}
@ -108,12 +174,19 @@ public class App
* to.
*/
/**
* When doing <code>executeUpdate</code> on a <code>Statement</code>
* it returns the number of rows updated
*
* NOTE: This cannot be called on a prepared statement
*/
Statement stmt = dbConn.createStatement();
int updateCount = stmt.executeUpdate("UPDATE users SET name = 'Bruh'");
System.out.println("Update count: "+updateCount);
stmt.close();
// Close the connection to the database
dbConn.close();
}
record User(int age, String name)
{
}
}
}

View File

@ -0,0 +1,6 @@
package ocp.db.Database;
public record User(int age, String name)
{
}

View File

@ -10,10 +10,169 @@ public class App
{
public static void main( String[] args )
{
/**
* Practicing the standard traditional
* switch statements
*/
// basicSwitchStatements();
/**
* Usage of the default case
*/
// defaultCaseSwitchStatement();
/**
* Switch case expressions with
* a singular expression
*/
// switchCaseExpressionsSingular();
/**
* Switch case expressions with
* multiple statements and an
* expression
*/
// switchCaseExpressionMultiple1();
/**
* Switch case expressions with
* multiple statements and an
* expression but with COMPILE-TIME
* checks that a yield is present
* PER EACH CASE!
*/
switchCaseExpressionMultiple2();
/**
* Switch statements
*/
switchTests();
// switchTests();
}
/**
* Practicing your BASIC switch statements
* that we are all used to
*/
private static void basicSwitchStatements()
{
int number = (int)(Math.random()*2);
/**
* Note that the first match is jumped to,
* then it runs all the code below it,
* THEREFORE place a BREAK after the match
* to avoid running the cases BELOW
* the matched one
*/
switch(number)
{
case 0:
System.out.println("We got a zero!");
break; // VERY important!
case 1:
System.out.println("We got a 1!");
}
}
/**
* Practicing the use of <code>default</code>
* in a switch statement
*
* This let's you have a case which is matched
* if none of the other cases matched.
*/
private static void defaultCaseSwitchStatement()
{
int number = (int)(Math.random()*4);
switch(number)
{
case 0:
System.out.println("We got ZERO!");
break;
case 1:
System.out.println("We got 1!");
break;
default:
System.out.println("We got SOMETHING ELSE '"+number+"'");
}
}
/**
* Switch case expressions (singular/implicit-yield)
*/
private static void switchCaseExpressionsSingular()
{
int number = (int)(Math.random()*4);
/**
* A singular expression has
* an implicit `yield` added
* so NO fallthrough can occur
*/
String result = switch(number)
{
case 0 -> "ZERO";
case 1 -> "ONE";
default -> "SOMETHING ELSE '"+number+"'";
};
System.out.println(result);
}
/**
* This shows the old-style switch expression
*
* NOTE! Because these have multiple statements
* that a yield is now required, and just like
* a break, if missing then fallthrough can occur
*/
private static void switchCaseExpressionMultiple1()
{
int number = (int)(Math.random()*4);
String result = switch(number)
{
case 0:
System.out.println("ZERO");
yield "ZERO"; // If not present you fall through till hitting a yield
case 1:
System.out.println("ONE");
yield "ONE";
default:
System.out.println("UNKNOWN");
yield "Unknown '"+number+"'";
};
System.out.println("Result: "+result);
}
/**
* New-style switch expressions for multiple
* statements with a compile-time check thaty
* each case contains a yield expression
* to prevent accidental fallthrough
*/
private static void switchCaseExpressionMultiple2()
{
int number = (int)(Math.random()*4);
String result = switch(number)
{
case 0 -> {
System.out.println("ZERO");
yield "ZERO";
}
case 1 -> {
System.out.println("ONE");
yield "ONE"; // Remove it and see the compiler complain
}
default -> {
System.out.println("SOMETHING ELSE '"+number+"'");
yield "ELSE '"+number+"'";
}
};
System.out.println("Result: "+result);
}
/**

2
Flow/target/classes/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/META-INF/
/ocp/

View File

@ -47,6 +47,18 @@ public class EnumTests
{
return myHeight;
}
public String toString()
{
if(this == SLIM)
{
return "Slimmy boy";
}
else
{
return super.toString();
}
}
}
/**
@ -81,7 +93,7 @@ public class EnumTests
BodyType[] values = BodyType.values();
System.out.println("BodyType values: "+Arrays.toString(values));
System.out.println("Value of: "+BodyType.valueOf("SLIM") +(BodyType.valueOf("SLIM") == BodyType.SLIM));
System.out.println("Value of: "+BodyType.valueOf("SLIM") +":"+ (BodyType.valueOf("SLIM") == BodyType.SLIM));
System.out.println("\n\n<<< end enum tests >>>\n\n");
}