Skip to content

Commit ac6ffb7

Browse files
committed
Initial commit
0 parents  commit ac6ffb7

File tree

10 files changed

+359
-0
lines changed

10 files changed

+359
-0
lines changed

.gitignore

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# NetBeans specific #
2+
nbproject/private/
3+
build/
4+
nbbuild/
5+
dist/
6+
nbdist/
7+
.nb-gradle/
8+
nbactions.xml
9+
nb-configuration.xml
10+
11+
## Gradle specific
12+
.gradle
13+
gradle
14+
gradle/wrapper
15+
16+
# Class Files #
17+
*.class
18+
19+
# Package Files #
20+
*.jar
21+
*.war
22+
*.ear
23+
24+
25+
#windows Junk
26+
*.ini
27+
[Tt]humbs.db
28+
*.DS_Store

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# An MVC-Based SQLite Client
2+
This is a simple MVC-based SQLite client written in Java. It uses the [Chinook](https://www.sqlitetutorial.net/sqlite-sample-database/) database and the [SQLite JDBC driver](https://github.com/xerial/sqlite-jdbc) that was designed and implmented by xerial (Taro L. Saito).
3+
4+
5+
6+

build.gradle

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: 'java'
2+
apply plugin: 'jacoco'
3+
apply plugin: 'application'
4+
5+
6+
description = 'Working with JDBC and SQLite DB'
7+
group = 'org.bee'
8+
9+
mainClassName = 'org.bee.sqlite.DriverMVC'
10+
11+
repositories {
12+
jcenter()
13+
}
14+
15+
dependencies {
16+
testImplementation 'junit:junit:4.13'
17+
implementation 'org.xerial:sqlite-jdbc:3.32.3.2'
18+
}
19+
20+

data/chinook_sqlite.db

1.02 MB
Binary file not shown.

gradle.properties

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generally it is better to set the version only for release builds, build time
2+
# from command line using -Pversion=1.0
3+
#
4+
# Only use SNAPSHOT versions if you need to contribute your binaries to a
5+
# local Maven project.
6+
version=1.0
7+

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'SQLiteDbSample'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.bee.sqlite;
2+
3+
import org.bee.sqlite.controllers.CustomerController;
4+
import org.bee.sqlite.models.Customer;
5+
6+
import java.util.List;
7+
8+
/**
9+
* A class for testing interactions with SQLite database.
10+
*
11+
* This sample demonstrates querying an SQLite database using SQLite JDBC
12+
* driver. As data source, the Chinook database is used in this example.
13+
*
14+
* @see {@link https://www.sqlitetutorial.net/sqlite-sample-database/}
15+
* @see {@link https://github.com/lerocha/chinook-database}
16+
* @see {@link https://github.com/xerial/sqlite-jdbc}
17+
*
18+
* @author Sleiman R.
19+
*/
20+
public class DriverMVC {
21+
22+
public static void main(String[] args) {
23+
CustomerController custController = new CustomerController();
24+
//-- Retrieve th list of customers.
25+
List<Customer> customers = custController.getCustomersList();
26+
printListOfCustomers(customers);
27+
}
28+
29+
private static void printListOfCustomers(List<Customer> customers) {
30+
System.out.println("===========================");
31+
System.out.printf("Total customers found: %d \n", customers.size());
32+
System.out.println("===========================");
33+
for (Customer customer : customers) {
34+
System.out.println(customer);
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.bee.sqlite.controllers;
2+
3+
import org.bee.sqlite.helpers.DBConnectionProvider;
4+
import org.bee.sqlite.models.Customer;
5+
import java.sql.Connection;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
/**
13+
* A class that handles database operations.
14+
* @author Sleiman R.
15+
*/
16+
public class CustomerController {
17+
18+
private final String DATABASE_FILE_NAME = "chinook_sqlite.db";
19+
private final String CUST_TABLE_NAME = "Customer";
20+
//-- Column names
21+
private final String CUSTOMER_ID = "CustomerId";
22+
private final String FIRSTNAME = "FirstName";
23+
private final String LASTNAME = "LastName";
24+
private final String COMPANY = "Company";
25+
private final String ADDRESS = "Address";
26+
private final String CITY = "City";
27+
private final String STATE = "State";
28+
private final String COUNTRY = "Country";
29+
private final String POSTALCODE = "PostalCode";
30+
private final String PHONE = "Phone";
31+
private final String FAX = "Fax";
32+
private final String EMAIL = "Email";
33+
private final String SUPPORTREP_ID = "SupportRepId";
34+
35+
36+
/**
37+
* Retrieves a list of customers stored in an SQLite database table.
38+
*
39+
* @see
40+
* {@link https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html}
41+
* @return the retrieved list of customers.
42+
*/
43+
public List<Customer> getCustomersList() {
44+
List<Customer> customers = new ArrayList<>();
45+
String query = "";
46+
//-- Step 1 & 2) Open a connection to the specified database
47+
//-- and create a prepared statement for executing SQL queries.
48+
try ( Connection dbConnection = DBConnectionProvider.getInstance().getConnection(DATABASE_FILE_NAME); Statement stmt = dbConnection.createStatement()) {
49+
query = String.format("SELECT * FROM %s ", CUST_TABLE_NAME);
50+
try ( ResultSet resSet = stmt.executeQuery(query)) {
51+
//-- Step 3) Process the list of records that are stored in the result set (in memory records).
52+
while (resSet.next()) {
53+
//-- Retreive values from the current record.
54+
Customer customer = new Customer();
55+
customer.setCustomerId(resSet.getInt(CUSTOMER_ID));
56+
customer.setFirstName(resSet.getString(FIRSTNAME));
57+
customer.setLastName(resSet.getString(LASTNAME));
58+
customer.setCompany(resSet.getString(COMPANY));
59+
customer.setCountry(resSet.getString(COUNTRY));
60+
customer.setPostalCode(resSet.getString(POSTALCODE));
61+
customer.setPhone(resSet.getString(PHONE));
62+
customer.setAddress(resSet.getString(ADDRESS));
63+
customer.setCity(resSet.getString(CITY));
64+
customer.setState(resSet.getString(STATE));
65+
customer.setFax(resSet.getString(FAX));
66+
customer.setEmail(resSet.getString(EMAIL));
67+
customer.setSupporterId(resSet.getInt(SUPPORTREP_ID));
68+
//-- Step 4) Add the retrieved customer from the result set to the list.
69+
customers.add(customer);
70+
}
71+
}
72+
} catch (SQLException ex) {
73+
System.err.println("An error has occured while trying to execute the following query: " + query);
74+
System.err.println("Error message: " + ex);
75+
}
76+
return customers;
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.bee.sqlite.helpers;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.SQLException;
6+
7+
/**
8+
* A singleton class that manages an SQLite connection.
9+
*
10+
* @author Sleiman R.
11+
*/
12+
public class DBConnectionProvider {
13+
14+
private static final String DB_FOLDER = "data";
15+
private static DBConnectionProvider instance;
16+
17+
private DBConnectionProvider() {
18+
19+
}
20+
21+
/**
22+
* Opens a connection to an SQLite database.
23+
*
24+
* @param databaseName the name of the SQLite database file (it can also be
25+
* a relative path.
26+
* @return a connection to an SQLite database.
27+
*/
28+
public Connection getConnection(String databaseName) {
29+
try {
30+
String connectionString = String.format("jdbc:sqlite:%s/%s", DB_FOLDER, databaseName);
31+
Class.forName("org.sqlite.JDBC");
32+
Connection dbConnection = DriverManager.getConnection(connectionString);
33+
return dbConnection;
34+
} catch (ClassNotFoundException | SQLException e) {
35+
System.out.println(e);
36+
return null;
37+
}
38+
}
39+
40+
public static DBConnectionProvider getInstance() {
41+
if (instance == null) {
42+
instance = new DBConnectionProvider();
43+
}
44+
return instance;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package org.bee.sqlite.models;
2+
3+
/**
4+
* A class that represents a Customer record stored in the Chinook DB.
5+
*
6+
* @author Sleiman R.
7+
*/
8+
public class Customer {
9+
10+
private int CustomerId;
11+
private String firstName;
12+
private String lastName;
13+
private String company;
14+
private String address;
15+
private String city;
16+
private String state;
17+
private String country;
18+
private String postalCode;
19+
private String phone;
20+
private String fax;
21+
private String email;
22+
private int supporterId;
23+
24+
public Customer() {
25+
}
26+
27+
public int getCustomerId() {
28+
return CustomerId;
29+
}
30+
31+
public void setCustomerId(int CustomerId) {
32+
this.CustomerId = CustomerId;
33+
}
34+
35+
public String getFirstName() {
36+
return firstName;
37+
}
38+
39+
public void setFirstName(String firstName) {
40+
this.firstName = firstName;
41+
}
42+
43+
public String getLastName() {
44+
return lastName;
45+
}
46+
47+
public void setLastName(String lastName) {
48+
this.lastName = lastName;
49+
}
50+
51+
public String getCompany() {
52+
return company;
53+
}
54+
55+
public void setCompany(String company) {
56+
this.company = company;
57+
}
58+
59+
public String getAddress() {
60+
return address;
61+
}
62+
63+
public void setAddress(String address) {
64+
this.address = address;
65+
}
66+
67+
public String getCity() {
68+
return city;
69+
}
70+
71+
public void setCity(String city) {
72+
this.city = city;
73+
}
74+
75+
public String getState() {
76+
return state;
77+
}
78+
79+
public void setState(String state) {
80+
this.state = state;
81+
}
82+
83+
public String getCountry() {
84+
return country;
85+
}
86+
87+
public void setCountry(String country) {
88+
this.country = country;
89+
}
90+
91+
public String getPostalCode() {
92+
return postalCode;
93+
}
94+
95+
public void setPostalCode(String postalCode) {
96+
this.postalCode = postalCode;
97+
}
98+
99+
public String getPhone() {
100+
return phone;
101+
}
102+
103+
public void setPhone(String phone) {
104+
this.phone = phone;
105+
}
106+
107+
public String getFax() {
108+
return fax;
109+
}
110+
111+
public void setFax(String fax) {
112+
this.fax = fax;
113+
}
114+
115+
public String getEmail() {
116+
return email;
117+
}
118+
119+
public void setEmail(String email) {
120+
this.email = email;
121+
}
122+
123+
public int getSupporterId() {
124+
return supporterId;
125+
}
126+
127+
public void setSupporterId(int supporterId) {
128+
this.supporterId = supporterId;
129+
}
130+
131+
@Override
132+
public String toString() {
133+
return "Customer{" + "CustomerId=" + CustomerId + ", firstName=" + firstName + ", lastName=" + lastName + ", company=" + company + ", address=" + address + ", city=" + city + ", state=" + state + ", country=" + country + ", postalCode=" + postalCode + ", phone=" + phone + ", fax=" + fax + ", email=" + email + '}';
134+
}
135+
136+
}

0 commit comments

Comments
 (0)