mirror of https://github.com/deavmi/JavaOCP
Compare commits
6 Commits
2749ae09de
...
867176ba94
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | 867176ba94 | |
Tristan B. Velloza Kildaire | e5d2992023 | |
Tristan B. Velloza Kildaire | 9476bc4b09 | |
Tristan B. Velloza Kildaire | 81af1bc831 | |
Tristan B. Velloza Kildaire | 3209a6baa6 | |
Tristan B. Velloza Kildaire | 691078f716 |
BIN
DB/Database/mydb
BIN
DB/Database/mydb
Binary file not shown.
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package ocp.db.Database;
|
||||
|
||||
public record User(int age, String name)
|
||||
{
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
/META-INF/
|
||||
/ocp/
|
|
@ -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");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue