Download - 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
Repository
Q7M1 – SC Dudy Fathan Ali S.Kom
http://bit.ly/4sc5-repo
Download Presentation Slide
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
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
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
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
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
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
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
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
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
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
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.
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
Connecting to a Database
Q7M1 – SC Dudy Fathan Ali S.Kom
Code Example [Koneksi.java]:
Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Code Example [ProductCRUD.java]:
Inserting Rows in a Table
Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Querying a Table
Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Updating Rows in a Table
Creating & Executing JDBC Statements
Q7M1 – SC Dudy Fathan Ali S.Kom
Deleting Rows in a Table
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.
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.
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();
Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Retrieving Rows
Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Inserting Rows
Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Updating Rows
Querying & Modifying Data Using PreparedStatement Object
Q7M1 – SC Dudy Fathan Ali S.Kom
Deleting Rows
Q7M1 – SC Dudy Fathan Ali S.Kom
Thank You!Dudy Fathan Ali, S.Kom