DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

Similar documents
Lab # 9. Java to Database Connection

Working with Databases and Java

PERSİSTENCE OBJECT RELATİON MAPPİNG

Lab # 6. Data Manipulation Language (DML)

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

Accessing databases in Java using JDBC

Running SQL in Java and PHP

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

Running SQL in Java and PHP

Database Lab Lab 6 DML part 3

Database Lab#9 Connecting to postgresql database from java programs

JAVA AND DATABASES. Summer 2018

DB I. 1 Dr. Ahmed ElShafee, Java course

Lab # 1. Introduction to Oracle

COP4540 TUTORIAL PROFESSOR: DR SHU-CHING CHEN TA: H S IN-YU HA

SQL in a Server Environment

13 Creation and Manipulation of Tables and Databases

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

Programming in Java

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

SQL and Java. Database Systems Lecture 20 Natasha Alechina

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

CMPUT 391 Database Management Systems. JDBC in Review. - Lab 2 -

Java Database Connectivity

Servlet 5.1 JDBC 5.2 JDBC

Data Manipulation Language (DML)

Database Application Development

Lab # 4. Data Definition Language (DDL)

Databases and MySQL. COMP 342: Programming Methods. 16 September Databases and MySQL

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

O ne of the most important features of JavaServer

Java Database Connectivity (JDBC) 25.1 What is JDBC?

JDBC. Sun Microsystems has included JDBC API as a part of J2SDK to develop Java applications that can communicate with databases.

Computer Programming: C++

Server-side Web Programming

Real SQL Programming 1

JDBC Java Database Connectivity is a Java feature that lets you connect

Pieter van den Hombergh. March 25, 2018

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Embedded SQL. csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Meraji Winter 2018

Types of Databases. Types of Databases. Types of Databases. Databases and Web. Databases and Web. Relational databases may also have indexes

Embedded SQL. csc343, Introduction to Databases Diane Horton with examples from Ullman and Widom Fall 2014

Lab # 4. Files & Queues in C

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Introduction to SQL & Database Application Development Using Java

Lecture 9&10 JDBC. Mechanism. Some Warnings. Notes. Style. Introductory Databases SSC Introduction to DataBases 1.

Visit for more.

Top 50 JDBC Interview Questions and Answers

JDBC [Java DataBase Connectivity]

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

UNIT III - JDBC Two Marks

SQL DML and DB Applications, JDBC

Lecture 2. Introduction to JDBC

JDBC, Transactions. Niklas Fors JDBC 1 / 38

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

Chapter 9 SQL in a server environment

SQL: Programming Midterm in class next Thursday (October 5)

Database Application Development

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

Databases and SQL Lab EECS 448

Lab 6-1: MySQL Server

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

Instructor: Jinze Liu. Fall 2008

Tiers (or layers) Separation of concerns

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

Web Applications and Database Connectivity using JDBC (Part II)

Database Design and Programming

Accessing a database from Java. Using JDBC

JDBC Programming: Intro

COMP102: Introduction to Databases, 23

INTRODUCTION TO JDBC - Revised Spring

Chapter 10 Java and SQL. Wang Yang

Disk Operating System

Databases 2012 Embedded SQL

Unit 3 - Java Data Base Connectivity

INTRODUCTION TO JDBC - Revised spring

UNIT-3 Java Database Client/Server

Logging and Recovery. 444 Section, April 23, 2009

Arrays. Eng. Mohammed Abdualal

IBM DB DB2 application development Hands-On Lab. Information Management Cloud Computing Center of Competence. IBM Canada Lab

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Enterprise Systems. Lecture 02: JDBC. Behzad BORDBAR

Embedded SQL. Introduction

Embedded SQL. Davood Rafiei

The Web Application Developer s. Red Hat Database. View. October 30, Webcast. Patrick Macdonald, Fernando Nasser. Red Hat Database Engineering

An introduction for the novice. David Lawrence, JLab. 5/8/07 MySQL David Lawrence 1/36

Database Application Programs PL/SQL, Java and the Web

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Transcription:

In the name of Allah Islamic University of Gaza Faculty of Engineering Computer Engineering Department ECOM 4113 DataBase Lab Lab # 9 JAVA-DATABASE CONNECTION El-masry 2013

Objective In this lab, we turn our attention to how SQL can be called from the Java object-oriented programming language. JDBC Java Data Base Connectivity (JDBC) is: An API for the Java programming language that defines how a client may access a database. The function libraries for SQL access from Java. It provides methods for querying and updating data in a database. JDBC Driver is basically an implementation of the function calls specified in the JDBC API. DataBase Connection from Java 1- Add JDBC Driver to the Java project. Right click on the project Name >> properties. 2

From the categories on left side, select Libraries. And on right side in Compile tab, click Add JAR/Folder. Browse to JDBC Driver >> Open. 3

OK. JDBC Driver is added to the project libraries. 4

2- Create a new class, specially for Connection. Let it to be Connect Class. Connect Class 5

Connect Class Description import java.sql.*: The JDBC library of classes must be imported into the Java program. These classes are called java.sql.*, and can be imported. Class.forName( org.postgresql.driver ): The method Class.forName(String) is used to load the JDBC driver class. Notes: When a Driver class is loaded, it creates an instance of itself and registers it with the DriverManager. The Java exception ClassNotFoundException occurs if the driver is not loaded successfully. The Connection object: a Connection object is created using the getconnection function of the DriverManager class of JDBC. Connection con=drivermanager.getconnection(url, UserName, Password) Connection Parameters 1- URL With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL, this takes the following form: jdbc:postgresql://host:port/database The parameters have the following meanings: Host: Database server IP, or Name. Defaults to localhost. To specify an IPv6 address your must enclose the host parameter with square brackets. Port: The port number the server is listening on. Defaults to the PostgreSQL standard port number (5432). Database: The database name. 2- UserName: The database user on whose behalf the connection is being made. 3- Password: The database user's password. 6

Note: The Statement object: In JDBC, there is a basic statement class, Statement. The Statement object is created using the createstatement function of the Connection class of JDBC. st= con.createstatement(); Declare Statement object as static, to be shared object between the project classes. Create an instance of Connect class in your main method to connect to the database. Connect connect=new Connect(); Run the file that contains the main method to ensure from your connection. 7

DataBase Access Anywhere you want to access the database, you need to do the following steps: 1- Define a String holds SQL Statement that you want to execute it. Examples: String S1= select * from \ EMPLOYEE\ String S2= update \EMPLOYEE\ set \ FNAME\ =\ Haneen\ where \ FNAME\ =\ Alicia\ String S3= insert into \ EMPLOYEE\ (\ SSN\,\ FNAME\ ) values( \ 123321123\, \ Mona\ ) String S4= delete from \ EMPLOYEE\ where \ SSN\ =\ 123321123\ 2- Execute SQL Statement If the statement is a Query (Select Statement), execute it by executequery method of the statement object that takes the Query as its parameter. ResultSet rs= Connect.st.executeQuery(S1) executequery method returns an object of type ResultSet, that resembles a twodimensional array or a table, where the tuples are the rows and the attributes returned are the columns. When the query is executed, rs refers to a tuple before the first tuple in the query result. The rs.next() function moves to the next tuple (row) in the ResultSet object and returns NULL if there are no more objects. This is used to control the looping. You can refer to the attributes in the current tuple using (getstring, getinteger, getdouble, and so on) functions that depend on the type of each attribute. Also you can either use the attribute positions (1, 2) or the actual attribute names. 8

9

If the SQL statement is insert, update or delete statement, execute it by executeupdate method of the statement object that takes the Query as its parameter. int rowcount= Connect.st.executeUpdate(S2) executeupdate method returns an integer value indicating the number of tuples that were affected. 10

First Run Second Run 11

Exercise Add an Authentication table to any of yours Databases, then Design a simple Java GUI application that authenticate the user who want to access the DataBase. If he is authenticated, let him to access the Database using various DML statements. Authentication Table Example Your work can be like the following: FName LName Password snow white 123456 12

13