Skip to content

Commit c69a68e

Browse files
Bob ITBob IT
Bob IT
authored and
Bob IT
committed
Java Database Connection
0 parents  commit c69a68e

15 files changed

+485
-0
lines changed

.classpath

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="lib" path="mysql-connector-java-8.0.18.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>JavaDatabaseConnection</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8

bin/MySQL_Queries/Jdbc_Delete.class

3.58 KB
Binary file not shown.

bin/MySQL_Queries/Jdbc_Insert.class

2.49 KB
Binary file not shown.

bin/MySQL_Queries/Jdbc_Submit.class

2.16 KB
Binary file not shown.

bin/MySQL_Queries/Jdbc_Test.class

2.15 KB
Binary file not shown.

bin/MySQL_Queries/Jdbc_Update.class

3.31 KB
Binary file not shown.

mysql-connector-java-8.0.18.jar

2.22 MB
Binary file not shown.

sql/table-setup.sql

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
create database if not exists demo;
2+
3+
use demo;
4+
5+
drop table if exists employees;
6+
7+
CREATE TABLE `employees` (
8+
`id` int(11) NOT NULL AUTO_INCREMENT,
9+
`last_name` varchar(64) DEFAULT NULL,
10+
`first_name` varchar(64) DEFAULT NULL,
11+
`email` varchar(64) DEFAULT NULL,
12+
`department` varchar(64) DEFAULT NULL,
13+
`salary` DECIMAL(10,2) DEFAULT NULL,
14+
PRIMARY KEY (`id`)
15+
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
16+
17+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (1,'Doe','John','[email protected]', 'HR', 55000.00);
18+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (2,'Public','Mary','[email protected]', 'Engineering', 75000.00);
19+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (3,'Queue','Susan','[email protected]', 'Legal', 130000.00);
20+
21+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (4,'Williams','David','[email protected]', 'HR', 120000.00);
22+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (5,'Johnson','Lisa','[email protected]', 'Engineering', 50000.00);
23+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (6,'Smith','Paul','[email protected]', 'Legal', 100000.00);
24+
25+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (7,'Adams','Carl','[email protected]', 'HR', 50000.00);
26+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (8,'Brown','Bill','[email protected]', 'Engineering', 50000.00);
27+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (9,'Thomas','Susan','[email protected]', 'Legal', 80000.00);
28+
29+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (10,'Davis','John','[email protected]', 'HR', 45000.00);
30+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (11,'Fowler','Mary','[email protected]', 'Engineering', 65000.00);
31+
INSERT INTO `employees` (`id`,`last_name`,`first_name`,`email`, `department`, `salary`) VALUES (12,'Waters','David','[email protected]', 'Legal', 90000.00);

src/MySQL_Queries/Jdbc_Delete.java

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package MySQL_Queries;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.PreparedStatement;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
10+
public class Jdbc_Delete {
11+
12+
public static void main(String[] args) throws SQLException {
13+
14+
Connection myConn = null;
15+
Statement myStmt = null;
16+
ResultSet myRs = null;
17+
18+
//1. Get connection to database
19+
20+
try{
21+
22+
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo"
23+
+ "?useUnicode=true"
24+
+ "&useJDBCCompliantTimezoneShift=true"
25+
+ "&useLegacyDatetimeCode=false"
26+
+ "&serverTimezone=UTC", "ilhan", "ilhan");
27+
28+
System.out.println("Database connection successful!\n");
29+
30+
//2. Create a statement
31+
32+
myStmt = myConn.createStatement();
33+
34+
//3. Before the delete to display the employee's info
35+
36+
System.out.println("BEFORE THE DELETE....");
37+
//helper method
38+
displayEmployee(myConn, "John", "Doe");
39+
40+
//DELETE THE Employee
41+
System.out.println("\nDELETING THE EMPLOYEE FOR: John Doe\n");
42+
43+
int rowsAffected = myStmt.executeUpdate(
44+
"delete from employees "+
45+
"where last_name='Doe' and first_name='John'"
46+
);
47+
48+
//Verify Updating
49+
System.out.println("AFTER THE DELETE...");
50+
//helper method
51+
displayEmployee(myConn, "John", "Doe");
52+
53+
}catch(Exception exc){
54+
exc.printStackTrace();
55+
}
56+
finally {
57+
close(myConn, myStmt, myRs);
58+
}
59+
60+
}
61+
private static void displayEmployee(Connection myConn, String firstName, String lastName) throws SQLException{
62+
PreparedStatement myStmt = null;
63+
ResultSet myRs = null;
64+
65+
try {
66+
//Prepare statement
67+
myStmt = myConn
68+
.prepareStatement("select last_name, first_name, email from employees where last_name=? and first_name=?");
69+
myStmt.setString(1, lastName);
70+
myStmt.setString(2, firstName);
71+
72+
//execute SQL query
73+
myRs = myStmt.executeQuery();
74+
75+
//Process result set
76+
boolean found = false;
77+
78+
while(myRs.next()){
79+
String theLastName = myRs.getString("last_name");
80+
String theFirstName = myRs.getString("first_name");
81+
String email = myRs.getString("email");
82+
83+
System.out.printf("%s, %s, %s\n", theFirstName, theLastName, email);
84+
found=true;
85+
}
86+
87+
if(!found){
88+
System.out.println("Employee NOT FOUND "+ firstName+ " "+ lastName);
89+
}
90+
91+
} catch (Exception exc) {
92+
exc.printStackTrace();
93+
}finally {
94+
close(myStmt, myRs);
95+
}
96+
97+
}
98+
private static void close(Connection myConn, Statement myStmt, ResultSet myRs) throws SQLException{
99+
100+
if(myRs !=null){
101+
myRs.close();
102+
}
103+
if(myStmt !=null){
104+
myStmt.close();
105+
}
106+
if(myConn !=null){
107+
myConn.close();
108+
}
109+
}
110+
111+
private static void close(Statement myStmt, ResultSet myRs) throws SQLException{
112+
113+
close(null, myStmt, myRs);
114+
115+
}
116+
117+
118+
}

src/MySQL_Queries/Jdbc_Insert.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package MySQL_Queries;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
public class Jdbc_Insert {
10+
11+
public static void main(String[] args) throws SQLException {
12+
13+
14+
Connection myConn = null;
15+
Statement myStmt = null;
16+
ResultSet myRs = null;
17+
18+
//1. Get connection to database
19+
20+
try{
21+
22+
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo"
23+
+ "?useUnicode=true"
24+
+ "&useJDBCCompliantTimezoneShift=true"
25+
+ "&useLegacyDatetimeCode=false"
26+
+ "&serverTimezone=UTC", "ilhan", "ilhan");
27+
28+
System.out.println("Database connection successful!\n");
29+
30+
//2. Create a statement
31+
32+
myStmt = myConn.createStatement();
33+
34+
//3.Insert a new employee in the database
35+
36+
System.out.println("Inserting a new employee to database\n");
37+
38+
int rowsAffected = myStmt.executeUpdate(
39+
"insert into employees" +
40+
"(last_name, first_name, email, department, salary)"+
41+
"values "+
42+
"('Wright', 'Eric', '[email protected]', 'HR', 3300.00)"
43+
);
44+
45+
//4. Verify this by getting a list of employees
46+
myRs = myStmt.executeQuery("select * from employees order by last_name");
47+
48+
//5. Process the result set
49+
while(myRs.next()){
50+
System.out.println(myRs.getString("last_name")+ " "+ myRs.getString("first_name"));
51+
}
52+
53+
}catch(Exception exc){
54+
exc.printStackTrace();
55+
}
56+
finally {
57+
if(myRs !=null){
58+
myRs.close();
59+
}
60+
if(myStmt !=null){
61+
myStmt.close();
62+
}
63+
if(myConn !=null){
64+
myConn.close();
65+
}
66+
}
67+
68+
}
69+
70+
}

src/MySQL_Queries/Jdbc_Submit.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package MySQL_Queries;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
public class Jdbc_Submit {
10+
11+
12+
public static void main(String[] args) throws SQLException {
13+
14+
Connection myConn = null;
15+
Statement myStmt = null;
16+
ResultSet myRs = null;
17+
18+
19+
20+
try {
21+
// 1. Get a connection to database
22+
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo"
23+
+ "?useUnicode=true"
24+
+ "&useJDBCCompliantTimezoneShift=true"
25+
+ "&useLegacyDatetimeCode=false"
26+
+ "&serverTimezone=UTC", "ilhan", "ilhan");
27+
28+
System.out.println("Database connection successful!\n");
29+
30+
// 2. Create a statement
31+
myStmt = myConn.createStatement();
32+
33+
// 3. Execute SQL query
34+
myRs = myStmt.executeQuery("select * from employees");
35+
36+
// 4. Process the result set
37+
while (myRs.next()) {
38+
System.out.println(myRs.getString("last_name") + ", " + myRs.getString("first_name"));
39+
}
40+
}
41+
catch (Exception exc) {
42+
exc.printStackTrace();
43+
}
44+
finally {
45+
if (myRs != null) {
46+
myRs.close();
47+
}
48+
49+
if (myStmt != null) {
50+
myStmt.close();
51+
}
52+
53+
if (myConn != null) {
54+
myConn.close();
55+
}
56+
}
57+
}
58+
59+
}

src/MySQL_Queries/Jdbc_Test.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package MySQL_Queries;
2+
3+
import java.sql.*;
4+
5+
public class Jdbc_Test {
6+
7+
public static void main(String[] args) throws SQLException {
8+
Connection myConn = null;
9+
Statement myStmt = null;
10+
ResultSet myRs = null;
11+
12+
//1. Get connection to database
13+
14+
try{
15+
16+
myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo"
17+
+ "?useUnicode=true"
18+
+ "&useJDBCCompliantTimezoneShift=true"
19+
+ "&useLegacyDatetimeCode=false"
20+
+ "&serverTimezone=UTC", "ilhan", "ilhan");
21+
22+
System.out.println("Database connection successful!\n");
23+
24+
25+
//2. Create a statement
26+
27+
myStmt = myConn.createStatement();
28+
29+
//3. Execute SQL query
30+
31+
myRs = myStmt.executeQuery("select * from Employees");
32+
33+
//4.Process the result set
34+
35+
while(myRs.next()){
36+
System.out.println(myRs.getString("last_name")+ " "+ myRs.getString("first_name"));
37+
}
38+
39+
}
40+
catch (Exception exc) {
41+
exc.printStackTrace();
42+
}finally {
43+
if(myRs !=null){
44+
myRs.close();
45+
}
46+
if(myStmt !=null){
47+
myStmt.close();
48+
}
49+
if(myConn !=null){
50+
myConn.close();
51+
}
52+
}
53+
54+
}
55+
56+
}

0 commit comments

Comments
 (0)