P0 Is a console App that will be extended to a full Java-Angular Web App in P1 and P2
Java Database Connectivity
- Register the driver
- create connection
- create statement
- execute queries
- close the connection
- It act as an interface between the user and driver
- it keeps track of the drivers that are available
- handle eastablising the connection between the dataabse and appropriate drivers
DriverManager class method
- registerDriver(Driver)
- deregisterDriver(Driver)
- getConnection(url)
- getConnection(url, username, password
- A connection is the session between the java application and dataabse
- the connection interface is a factory of the statement, prepareStatement and DatabaseMetaData
- createConnection()
- createConnection(resultSetType, resultSetConcurrency)
- setAuthCommit(boolean: status) // default true
- commit()
- rollback()
- close()
- it provides method to execute queries with the database
- a statement interface is a factory of resultSet which provide a factory method to get the object of ResultSet
statement interface common method
- ResultSet executeQuery(String query) - is used to execute the select query and return the object of result
- int executeUpdate(String query) - is used to execute the specified query, create, drop, alter, update, delete, insert etc
- boolean execute(String query) - it is used to execute queries that may return multiple result
- int[] executeBatch() - it is used to execute batch of commands
- it maintain a cursor pointing to a row of a table
- initially the cursor point to the first row
ResultSet interface common method
- next()
- previous()
- first()
- last()
- it is a sub interface of statement
- it is used to execute parameterized query
"insert into employee values (1, 'mark', 'm@gmail.com')" "insert into employee values (?, ?, ?)"
we can pass parameters (?) for the value
- improve performance - as it will be faster because the query is compiled only once
- Security - it avoid SQL injection
- Employee create a class as Employee which represent table and class field represent column Employee
- Id
- Name
-
EmployeeDao (Data Access Object) it is an interface that lists all the methods we want to implement in our application
-
EmployeeDaoImpl this is a class that implement our EmployeeDao interface and provide the implementation of all the method we want to write JDBC code to perform database opeartion
-
EmployeeDaoFactory this is a class that contain a method and return EmployeeDao interface, and the idea is to decouple interface and implementation class so that if we change the implmentation we can easily do it
-
dbConfig.properties this is a particular file in java used to define configuration in a simple formart and also this can be central place which can be accessed from anywhere to get configuration
-
ConnectionFactory this is a class responsable for returning connection class object
-
main in the main class, get the instance of the Dao and call the appropritate method to perform database opeartion
Step:
- create new java project
- add JDBC jar file
- create employee class
- create employeeDao
- create EmployeeDaoImpl
- create EmployeeDaoFactory
- create dbConfig
- create ConnectionFactory
- perform opeartion in main class