GithubHelp home page GithubHelp logo

courselist's Introduction

Database Drivers for JDBC

Object and Database Design

A Course has these attributes:

Attribute Meaning Example
id The id or Primary Key 0
courseNumber Course number, may begin with "0" 01204111
title The course name "Programming for Dummies"
credits The course credits 3
difficulty The difficulty rating (0-10) 0.5

A difficulty of 1 is very easy, 10 is very hard; 0 means no difficulty rating assigned yet.

Create database schema and import data

Using SQLite and the sqlite3 command line utility:

# create "sample.db" using schema commands in a file
sqlite3 sample.db < sample.schema

# start the sqlite command line utility (or use a GUI tool)
# then import course data from a CSV file.
ubuntu$ sqlite3 sample.db
sqlite> .mode csv
sqlite> .import data/courses.csv courses

You can use the command line utility to explore the database and issue SQL commands:

sqlite> .tables
course     enrollment
sqlite> .schema courses
CREATE TABLE courses (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    course_number TEXT    NOT NULL,
    title         TEXT    NOT NULL,
    credits       INTEGER DEFAULT 0,
    difficulty    REAL    DEFAULT 0
);
sqlite> .mode column
sqlite> SELECT * FROM courses LIMIT 5;
shows the course data
sqlite> .quit

Browsing tables and modifying the database structure can also be done using a GUI tool such as DBeaver or SqliteBrowser (introduced in ISP).

Access the Database Programmatically using JDBC

Using the lowest level interface possible (JDBC) we can access the table data directly using JDBC:

  1. Create a database Connection (java.jdbc.Connection)
  2. Create a database Statement or PreparedStatement. A Prepared Statement is safer and in some cases more efficient.
  3. Execute the statement and get a ResultSet.
  4. Iterate over the ResultSet, parse data, and print it.

This demonstrates how to use JDBC, but it's not a good design for software.

Design and Implement a Data Access Object

Let's create a persistence component as a Data Access Object (DAO), for the Course class only.

A DAO should provide the basic CRUD+ operations:

Method Meaning
get(id) Get one object using its primary key (id).
save(course) Add a new course to the database. The course object should have id 0, and the DAO will assign the id to the primary key value.
update(course) Update an existing course in the database. The course id must be the primary key value in the database.
delete(course) Delete an existing course from the database. The course id must be the primary key value in the database. After completion, the id field is set to 0.

Two other operations that most persistence frameworks provide are:

Method Meaning
count() Return the number of objects in the courses table.
findAll() Return a list of all persisted course objects.

We are writing a DAO for only one table with no relationships, and not concerning ourselves with guaranteeing object uniqueness. You can imagine how much work writing a production-quality DAO can be.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.