Top Banner
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
27

Java CRUD Mechanism with SQL Server Database

Mar 03, 2017

Download

Technology

Dudy Ali
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java CRUD Mechanism with SQL Server Database

Q7M1 – SC Dudy Fathan Ali S.Kom

Java CRUD Mechanismwith SQL Server Database

Q7M1

Dudy Fathan Ali, S.Kom (DFA)2017

CEP - CCITFakultas Teknik Universitas Indonesia

Page 2: Java CRUD Mechanism with SQL Server Database

Repository

Q7M1 – SC Dudy Fathan Ali S.Kom

http://bit.ly/4sc5-repo

Download Presentation Slide

Page 3: Java CRUD Mechanism with SQL Server Database

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

Page 4: Java CRUD Mechanism with SQL Server Database

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 DSNo Specifying the connection details for a DSNo Testing the created DSN

Page 5: Java CRUD Mechanism with SQL Server Database

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

Page 6: Java CRUD Mechanism with SQL Server Database

Configuring JDBC-ODBC Data Source Name

Q7M1 – SC Dudy Fathan Ali S.Kom

Selecting the required driver for a DSN

o Select System DSN Tabo Add New System Data Sources

Page 7: Java CRUD Mechanism with SQL Server Database

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

Page 8: Java CRUD Mechanism with SQL Server Database

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

Page 9: Java CRUD Mechanism with SQL Server Database

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

Page 10: Java CRUD Mechanism with SQL Server Database

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

Page 11: Java CRUD Mechanism with SQL Server Database

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

Page 12: Java CRUD Mechanism with SQL Server Database

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

Page 13: Java CRUD Mechanism with SQL Server Database

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.

Page 14: Java CRUD Mechanism with SQL Server Database

Using JDBC API

Q7M1 – SC Dudy Fathan Ali S.Kom

o The steps to create JDBC application are:o Load a drivero Connect to a databaseo Create and execute JDBC statementso Handle SQL exceptions

Page 15: Java CRUD Mechanism with SQL Server Database

Connecting to a Database

Q7M1 – SC Dudy Fathan Ali S.Kom

Code Example [Koneksi.java]:

Page 16: Java CRUD Mechanism with SQL Server Database

Creating & Executing JDBC Statements

Q7M1 – SC Dudy Fathan Ali S.Kom

Code Example [ProductCRUD.java]:

Inserting Rows in a Table

Page 17: Java CRUD Mechanism with SQL Server Database

Creating & Executing JDBC Statements

Q7M1 – SC Dudy Fathan Ali S.Kom

Querying a Table

Page 18: Java CRUD Mechanism with SQL Server Database

Creating & Executing JDBC Statements

Q7M1 – SC Dudy Fathan Ali S.Kom

Updating Rows in a Table

Page 19: Java CRUD Mechanism with SQL Server Database

Creating & Executing JDBC Statements

Q7M1 – SC Dudy Fathan Ali S.Kom

Deleting Rows in a Table

Page 20: Java CRUD Mechanism with SQL Server Database

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.

Page 21: Java CRUD Mechanism with SQL Server Database

Querying & Modifying Data Using PreparedStatement Object

Q7M1 – SC Dudy Fathan Ali S.Kom

o Methods of the PreparedStatement Interfaceo 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.

Page 22: Java CRUD Mechanism with SQL Server Database

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();

Page 23: Java CRUD Mechanism with SQL Server Database

Querying & Modifying Data Using PreparedStatement Object

Q7M1 – SC Dudy Fathan Ali S.Kom

Retrieving Rows

Page 24: Java CRUD Mechanism with SQL Server Database

Querying & Modifying Data Using PreparedStatement Object

Q7M1 – SC Dudy Fathan Ali S.Kom

Inserting Rows

Page 25: Java CRUD Mechanism with SQL Server Database

Querying & Modifying Data Using PreparedStatement Object

Q7M1 – SC Dudy Fathan Ali S.Kom

Updating Rows

Page 26: Java CRUD Mechanism with SQL Server Database

Querying & Modifying Data Using PreparedStatement Object

Q7M1 – SC Dudy Fathan Ali S.Kom

Deleting Rows

Page 27: Java CRUD Mechanism with SQL Server Database

Q7M1 – SC Dudy Fathan Ali S.Kom

Thank You!Dudy Fathan Ali, S.Kom

[email protected]