1. Q7M1 – SC Dudy Fathan Ali S.Kom
Java CRUD Mechanism
with SQL Server Database
Q7M1
Dudy Fathan Ali, S.Kom (DFA)
2017
CEP - CCIT
Fakultas Teknik Universitas Indonesia
3. Create a Database in SQL Server
Q7M1 – SC Dudy Fathan Ali S.Kom
To follow this lesson, you need to create a database based on the
following schematic database diagram:
Database name: GoShop
4. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
You need to perform the following steps to create a Data Source
Name:
o Select the required driver for a DSN
o Specifying the connection details for a DSN
o Testing the created DSN
5. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Selecting the required driver for a DSN
o Open Administrative Tools in
Control Panel
o Double-click the ODBC Data
Sources Icon
6. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Selecting the required driver for a DSN
o Select System DSN Tab
o Add New System Data Sources
7. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Selecting the required driver for a DSN
o In the Create New Data Source
dialog box, select SQL Server
option
o Click the Finish button
8. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Specifying the connection details for a DSN
o Type the name of the Data
Source in the Name text box
o Select required SQL Server
name from the Server drop-
down list
o Click the Next button
9. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Specifying the connection details for a DSN
o To indicate how the SQL Server
should authenticate a login ID,
select With SQL Server
authentication.. option
o Type the username in the
Login ID textbox and the
password in the Password
textbox
o Click the Next button
10. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Specifying the connection details for a DSN
o Select the name of the
database from drop-down
listbox
o Click the Next button
11. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Specifying the connection details for a DSN
o Click the Finish button
12. Configuring JDBC-ODBC Data Source Name
Q7M1 – SC Dudy Fathan Ali S.Kom
Testing the created DSN
o To test the created DSN for the connection with database, click the Test Data Source
button
13. Using JDBC API
Q7M1 – SC Dudy Fathan Ali S.Kom
o The JDBC API classes and interfaces are available in the java.sql
and the javax.sql packages.
o The commonly used classes and interfaces in the JDBC API are:
o DriverManager class: Loads the driver for a database.
o Driver interface: Represents a database driver. All JDBC driver classes
must implement the Driver interface.
o Connection interface: Enables you to establish a connection between a
Java application and a database.
o Statement interface: Enables you to execute SQL statements.
o ResultSet interface: Represents the information retrieved from a
database.
o SQLException class: Provides information about the exceptions that
occur while interacting with databases.
14. Using JDBC API
Q7M1 – SC Dudy Fathan Ali S.Kom
o The steps to create JDBC application are:
o Load a driver
o Connect to a database
o Create and execute JDBC statements
o Handle SQL exceptions
15. Connecting to a Database
Q7M1 – SC Dudy Fathan Ali S.Kom
Code Example [Koneksi.java]:
16. Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Code Example [ProductCRUD.java]:
Inserting Rows in a Table
17. Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Querying a Table
18. Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Updating Rows in a Table
19. Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Deleting Rows in a Table
20. Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
o The PreparedStatement interface is derived from the Statement
interface and is available in the java.sql package.
o The PreparedStatement object:
o Allows you to pass runtime parameters to the SQL statements to query
and modify the data in a table.
o Is compiled and prepared only once by JDBC. The future invocation of
the PreparedStatement object does not recompile the SQL
statements.
o Helps in reducing the load on the database server and thus improving
the performance of the application.
21. Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
o Methods of the PreparedStatement Interface
o The PreparedStatement interface inherits the following
methods to execute SQL statements from the Statement
interface:
o ResultSet executeQuery(): Executes a SELECT statements and
returns the result in a ResultSet object.
o int executeUpdate(): Executes an SQL statement, INSERT,
UPDATE, or DELETE and returns the count of the rows affected.
o boolean execute(): Executes an SQL statement and returns a
boolean value.
22. Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
o The prepareStatement() method of the Connection object is
used to submit parameterized query to a database.
o The SQL statement can contain ‘?’ symbol as placeholders that can
be replaced by input parameters at runtime.
o The value of each ‘?’ parameter is set by calling an appropriate
setXXX() method, where XXX is the data type of the parameter.
stat = con.prepareStatement("SELECT * FROM authors WHERE au_id = ?");
stat.setString(1,"1001");
ResultSet result=stat.executeQuery();
23. Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Retrieving Rows
24. Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Inserting Rows
25. Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Updating Rows
26. Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Deleting Rows