Skip to content

Commit a14fc8a

Browse files
committed
updated
1 parent 5b9dfba commit a14fc8a

13 files changed

+390
-0
lines changed
28.2 KB
Binary file not shown.

jdbc-tutorial/JDBCExample.class

1.33 KB
Binary file not shown.

jdbc-tutorial/JDBCExample.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// information to run this program
2+
// to compile the program
3+
// javac JDBCExample.java
4+
// first method
5+
// to run the program
6+
// java -cp "C:\mysql-connector-java-5.1.49\mysql-connector-java-5.1.49-bin.jar;" JDBCExample
7+
// or
8+
// second method
9+
// set path
10+
// set path=%path%;C:\mysql-connector-java-5.1.49;
11+
// java JDBCExample
12+
// either your can use hostname 127.0.0.1 or localhost
13+
14+
// default port number 3306
15+
16+
import java.sql.Connection; // to establish a connection
17+
import java.sql.DriverManager;
18+
import java.sql.SQLException;
19+
20+
public class JDBCExample {
21+
22+
public static void main(String[] args) {
23+
24+
// https://docs.oracle.com/javase/8/docs/api/java/sql/package-summary.html#package.description
25+
// auto java.sql.Driver discovery -- no longer need to load a java.sql.Driver class via Class.forName
26+
27+
// register JDBC driver, optional since java 1.6
28+
/*try {
29+
Class.forName("com.mysql.jdbc.Driver");
30+
} catch (ClassNotFoundException e) {
31+
e.printStackTrace();
32+
}*/
33+
34+
// auto close connection
35+
try (Connection conn = DriverManager.getConnection(
36+
"jdbc:mysql://127.0.0.1:3306/IUT_DB?autoReconnect=true&useSSL=false", "root", "1234")) {
37+
38+
if (conn != null) {
39+
System.out.println("Connected to the database!");
40+
} else {
41+
System.out.println("Failed to make connection!");
42+
}
43+
44+
} catch (SQLException e) {
45+
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
46+
} catch (Exception e) {
47+
e.printStackTrace();
48+
}
49+
50+
}
51+
}

jdbc-tutorial/JdbcSelectTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.sql.*; // Use 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
2+
3+
// JDK 1.7 and above
4+
5+
public class JdbcSelectTest { // Save as "JdbcSelectTest.java"
6+
public static void main(String[] args) {
7+
try (
8+
// Step 1: Allocate a database 'Connection' object
9+
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/IUT_DB?autoReconnect=true&useSSL=false", "root", "1234");
10+
// MySQL: "jdbc:mysql://hostname:port/databaseName", "username", "password"
11+
12+
// Step 2: Allocate a 'Statement' object in the Connection
13+
Statement stmt = conn.createStatement();) {
14+
// Step 3: Execute a SQL SELECT query, the query result
15+
// is returned in a 'ResultSet' object.
16+
String strSelect = "select Student_name, studentID from student";
17+
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
18+
System.out.println();
19+
20+
ResultSet rset = stmt.executeQuery(strSelect);
21+
22+
// Step 4: Process the ResultSet by scrolling the cursor forward via next().
23+
// For each row, retrieve the contents of the cells with getXxx(columnName).
24+
System.out.println("The records selected are:");
25+
int rowCount = 0;
26+
27+
while(rset.next()) { // Move the cursor to the next row, return false if no more row
28+
String sname = rset.getString("student_name");
29+
int sid= rset.getInt("studentID");
30+
31+
System.out.println(sid+ " " +sname );
32+
++rowCount;
33+
}
34+
System.out.println("Total number of records = " + rowCount);
35+
36+
} catch(SQLException ex) {
37+
ex.printStackTrace();
38+
}
39+
// Step 5: Close the resources - Done automatically by try-with-resources
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.sql.*; // Use 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
2+
3+
// JDK 1.7 and above
4+
public class JdbcSelectTest_insert { // Save as "JdbcSelectTest.java"
5+
public static void main(String[] args) {
6+
try (
7+
// Step 1: Allocate a database 'Connection' object
8+
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/IUT_DB?autoReconnect=true&useSSL=false", "root", "1234");
9+
// MySQL: "jdbc:mysql://hostname:port/databaseName", "username", "password"
10+
11+
// Step 2: Allocate a 'Statement' object in the Connection
12+
Statement stmt = conn.createStatement();) {
13+
// Step 3: Execute a SQL SELECT query, the query result
14+
// is returned in a 'ResultSet' object.
15+
int sid=999;
16+
String sname="Muhaamad";
17+
18+
String strSelect = "insert into student values (" +sid+","+"'" +sname+"')";
19+
20+
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
21+
System.out.println();
22+
23+
int count = stmt.executeUpdate(strSelect);
24+
25+
// Step 4: Process the ResultSet by scrolling the cursor forward via next().
26+
// For each row, retrieve the contents of the cells with getXxx(columnName).
27+
conn.close();
28+
} catch(SQLException ex) {
29+
ex.printStackTrace();
30+
}
31+
// Step 5: Close the resources - Done automatically by try-with-resources
32+
}
33+
}
34+
35+
// to delete record
36+
// String strSelect = "delete from student where studentid ="+ sid;

jdbc-tutorial/Readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# MySQL
2+
3+
- How to open MySQL command line: https://www.tutorialspoint.com/how-to-open-mysql-command-line-on-windows10
4+
5+
- Key points:
6+
- Connection between application and database
7+
- How to retrieve the data from database
8+
- How to insert data into database
9+
10+
- JBDC - Java Database Connectivity
11+
12+
- `set path=%path%;C:\mysql-connector-java-5.1.49;`
13+
14+
- `javac -cp "C:\mysql-connector-java-5.1.49\mysql-connector-java-5.1.49-bin.jar;" JDBCExample.java`
15+
16+
- `executeUpdate() `: This method is used for execution of DML statement(INSERT, UPDATE and DELETE) which is return int value, count of the affected rows.
17+
18+
- `executeQuery()` : This method is used to retrieve data from database using SELECT query.

lab-08-mysql/IUT_BMI.java

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import java.util.Scanner; // Import the Scanner class
2+
import java.sql.*; // Use 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
3+
4+
public class IUT_BMI {
5+
public static void main(String[] args) {
6+
7+
try (
8+
// Allocate a database 'Connection' object
9+
Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/IUT_BMI?autoReconnect=true&useSSL=false", "root", "1234");
10+
11+
// Allocate a 'Statement' object in the Connection
12+
Statement stmt = conn.createStatement();
13+
) {
14+
15+
Scanner input = new Scanner(System.in); // Create a Scanner object
16+
int option, isExit; // Initializing some variables
17+
18+
int employeeID, employeeHeight, employeeWeight;
19+
String employeeName;
20+
21+
do { // Loop for reusing the program
22+
option = showMenu(); // The option value
23+
24+
switch (option){
25+
case 1:
26+
System.out.println("***** DISPLAY RECORDS *****");
27+
// Execute a SQL SELECT query, the query result is returned in a 'ResultSet' object.
28+
String strSelect = "SELECT * FROM Employee_BMI; ";
29+
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
30+
System.out.println();
31+
32+
ResultSet rset = stmt.executeQuery(strSelect);
33+
34+
// Process the ResultSet by scrolling the cursor forward via next().
35+
// For each row, retrieve the contents of the cells with getXxx(columnName).
36+
System.out.println("The records selected are:");
37+
int rowCount = 0;
38+
39+
while(rset.next()) { // Move the cursor to the next row, return false if no more row
40+
employeeID = rset.getInt("employeeID");
41+
employeeHeight = rset.getInt("employeeHeight");
42+
employeeWeight = rset.getInt("employeeWeight");
43+
employeeName = rset.getString("employeeName");
44+
45+
System.out.println(employeeID + " " + employeeName + " " + employeeHeight + " " + employeeWeight );
46+
rowCount++;
47+
}
48+
System.out.println("Total number of records = " + rowCount);
49+
break;
50+
51+
case 2:
52+
System.out.println("***** INSERT RECORD *****");
53+
//Execute a SQL SELECT query, the query result is returned in a 'ResultSet' object.
54+
System.out.print("Employee ID: "); employeeID = (input.nextInt());
55+
System.out.print("Employee height: "); employeeHeight = (input.nextInt());
56+
System.out.print("Employee weight: "); employeeWeight = (input.nextInt());
57+
System.out.print("Employee name: "); employeeName = (input.next());
58+
59+
strSelect = "INSERT INTO Employee_BMI VALUES(" + employeeID + "," + "'" + employeeName + "'," + " " + employeeHeight+"," + " " + employeeWeight+")";
60+
61+
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
62+
63+
int count = stmt.executeUpdate(strSelect);
64+
65+
break;
66+
67+
case 3:
68+
System.out.println("***** UPDATE RECORD *****");
69+
// Execute a SQL SELECT query, the query result is returned in a 'ResultSet' object.
70+
System.out.print("Employee ID: "); employeeID = (input.nextInt());
71+
System.out.print("Employee height: "); employeeHeight = (input.nextInt());
72+
System.out.print("Employee weight: "); employeeWeight = (input.nextInt());
73+
74+
// SQL query to update record
75+
strSelect ="UPDATE Employee_BMI SET employeeWeight=" + employeeWeight + ", employeeHeight=" + employeeHeight + " WHERE employeeID="+ employeeID;
76+
77+
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
78+
79+
count= stmt.executeUpdate(strSelect);
80+
81+
break;
82+
83+
case 4:
84+
System.out.println("***** DELETE RECORD *****");
85+
// Execute a SQL SELECT query, the query result is returned in a 'ResultSet' object.
86+
System.out.print("Employee ID: "); employeeID = (input.nextInt());
87+
88+
// to delete record
89+
strSelect ="DELETE FROM Employee_BMI WHERE employeeID=" + employeeID;
90+
91+
System.out.println("The SQL query is: " + strSelect); // Echo For debugging
92+
System.out.println();
93+
94+
count = stmt.executeUpdate(strSelect);
95+
break;
96+
97+
case 0:
98+
System.out.println("Quitting Program...");
99+
System.exit(0); // program ends
100+
break;
101+
default:
102+
System.out.println("Please choose a valid option!");
103+
104+
} // End of switch
105+
106+
System.out.print("\nDo you want to continue? (0.No 1.Yes) - ");
107+
isExit = input.nextInt(); // validation checking
108+
109+
} while(isExit == 1);
110+
111+
112+
} catch(SQLException ex) {
113+
ex.printStackTrace();
114+
}
115+
116+
} // end of main()
117+
118+
public static int showMenu() {
119+
Scanner input = new Scanner(System.in); // Create a Scanner object
120+
121+
int option = 0;
122+
// Printing menu to screen
123+
System.out.println("\n***** MAIN MENU *****");
124+
System.out.println("1. Display records");
125+
System.out.println("2. Insert record");
126+
System.out.println("3. Update record");
127+
System.out.println("4. Delete record");
128+
System.out.println("0. Exit");
129+
130+
// Getting user option from above menu
131+
System.out.print("Your choice: ");
132+
option = input.nextInt();
133+
System.out.println();
134+
135+
return option;
136+
} // End of showMenu
137+
}

lab-08-mysql/TemplateMenu.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Scanner; // Import the Scanner class
2+
3+
public class TemplateMenu {
4+
public static void main(String[] args) {
5+
6+
Scanner input = new Scanner(System.in); // Create a Scanner object
7+
int option; // Initializing some variables
8+
9+
while(true) {
10+
System.out.println("\n***** MAIN MENU *****");
11+
System.out.println("1. TV");
12+
System.out.println("2. Mobile");
13+
System.out.println("0. Exit");
14+
System.out.print("Your choice: ");
15+
option = input.nextInt(); // get user option from above menu
16+
17+
switch (option) { // main menu
18+
case 1:
19+
System.out.println("11111...");
20+
break;
21+
22+
case 2:
23+
System.out.println("2222...");
24+
break;
25+
26+
case 0:
27+
System.out.println("Quitting Program...");
28+
System.exit(0); // program ends
29+
break;
30+
31+
default:
32+
System.out.println("Please enter a valid option!");
33+
} // end of switch
34+
35+
} // end of while
36+
37+
}
38+
39+
}

lab-08-mysql/TemplateMenu2.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.Scanner; // Import the Scanner class
2+
3+
public class TemplateMenu2 {
4+
public static void main(String[] args) {
5+
6+
Scanner input = new Scanner(System.in); // Create a Scanner object
7+
int option, isExit; // Initializing some variables
8+
9+
do { // Loop for reusing the program
10+
option = showMenu(); // The option value
11+
12+
switch (option){
13+
case 1:
14+
System.out.println("***** DISPLAY *****");
15+
16+
break;
17+
18+
case 2:
19+
System.out.println("***** INSERT RECORD *****");
20+
21+
break;
22+
23+
case 3:
24+
System.out.println("***** UPDATE RECORD *****");
25+
26+
break;
27+
28+
case 4:
29+
System.out.println("***** DELETE RECORD *****");
30+
31+
break;
32+
33+
case 0:
34+
System.out.println("Quitting Program...");
35+
System.exit(0); // program ends
36+
break;
37+
default:
38+
System.out.println("Please choose a valid option!");
39+
40+
} // End of switch
41+
42+
System.out.print("\nDo you want to continue? (0.No 1.Yes) - ");
43+
isExit = input.nextInt(); // validation checking
44+
45+
} while(isExit == 1);
46+
47+
} // end of main()
48+
49+
public static int showMenu() {
50+
Scanner input = new Scanner(System.in); // Create a Scanner object
51+
52+
int option = 0;
53+
// Printing menu to screen
54+
System.out.println("\n***** MAIN MENU *****");
55+
System.out.println("1. Display records");
56+
System.out.println("2. Insert record");
57+
System.out.println("3. Update record");
58+
System.out.println("4. Delete record");
59+
System.out.println("0. Exit");
60+
61+
// Getting user option from above menu
62+
System.out.print("Your choice: ");
63+
option = input.nextInt();
64+
System.out.println();
65+
66+
return option;
67+
} // End of showMenu
68+
}

lab-08-mysql/U1910049_Lab_8_JDBC.zip

276 KB
Binary file not shown.

lab-08-mysql/mysql-shell.png

81.7 KB
Loading

lab-08-mysql/screenshot-1.png

107 KB
Loading

lab-08-mysql/screenshot-2.png

105 KB
Loading

0 commit comments

Comments
 (0)