Database Programming Basic JDBC Programming Concepts.

Slides:



Advertisements
Similar presentations
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
Advertisements

Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
JDBC. JDBC (Java Database Connectivity): JDBC is an API for the Java programming language that defines how a client may access a database. It provides.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Advanced Java Programming and Database connection Eran.
TUTORIAL (1) Software installation Written by: Eng. Ahmed Mohamed Abdel Ghafar, Edited by Eng. Muhammed Hammad, Eng. Hamdy Soltan & Eng. Osama Talaat.
Java MS Access database connectivity Follow these steps: 1)Go to the start->Control Panel->Administrative Tools- > data sources. 2)Click Add button and.
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
CSCI 6962: Server-side Design and Programming
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Views, Indexes and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Haiman Tian 1.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
JSP program that interacts with HTML form & Access Data Base.
Chapter 8 Databases.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
Unit 5 Microsoft SQL Server and MySQL. Key Concepts DBMS variations SQL Server features SQL Server Management Studio MySQL features Scripts Queries Database.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
EXAMPLE I An application showing JDBC access to Cloudscape.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Basics of JDBC.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
1. Writing a Java program to connect to SQL Server 2008 and Create a table Populate the table (insert data) Perform queries to retrieve information from.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
The Bookstore Problem. Contents I.Problem Description II.Solution III.Exercise.
Sending . Contents A)Sending Mail Using Command Line B)Sending Mail Using GUI.
MYSQL APPLICATIONS USING JAVA & JDBC CIS 430 –Database Concepts Victor Matos.
JSP and DB.
ASP.NET Programming with C# and SQL Server First Edition
CS3220 Web and Internet Programming Database Access with JDBC
Java Access to RDB Relational databases (RDBs) dominate today, due to:
JDBC 15-Apr-18.
CS320 Web and Internet Programming Database Access with JDBC
Advanced Web Automation Using Selenium
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
JDBC 15-Nov-18.
MSIS 655 Advanced Business Applications Programming
Bolat Azamat, Kim Dongmin
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Database Programming Basic JDBC Programming Concepts

Contents I.Preparation II.The Greetings Problem

I. Preparation 1.MySQL 2.Eclipse

1. MySQL Download and Install MySQL Server – mysql-essential win32.msi – Username: root; password: root Download and Install MySQL GUI Tools – mysql-gui-tools-5.0-r12-win32.msi Download MySQL JDBC Driver – mysql-connector-java bin.jar

2. Eclipse Download and Uncompress Install Eclipse Data Tools Platform (DTP) Plugin – Running Eclipse – Help > Software Updates… – Available Software > Expand Ganymede Check Database Development > Install… Next > I accept… > Finish

II. The Greetings Problem Develop a database called GREETINGS that has a table called Greetings. Try to put “Hello,World!” to this database. After that, try to show all greetings stored in the database, and then delete the Greetings table

Solution Using Eclipse Project name: Greetings 1. Solution Using SQL Script 2. Solution Using Java

1. Solution Using SQL Script 1.1. Creating an Empty Database 1.2. Creating a Connection Profile 1.3. Creating a SQL Script 1.4. Executing the SQL Script

1.1. Creating an Empty Database Run MySQL Query Browser Right-click to an empty area of Schemata > Create New Schema – Schema name: greetings – OK

1.2. Creating a Connection Profile Open Database Development Perspective – Window > Open Perspective > Other… – Database Development > OK Data Source Explorer – Databases > Icon New Connection Profile – MySQL > Name: MySQL > Next – Icon New Driver Definition – Name/Type: 5.1 – Jar List: mysql-connector-java bin.jar – Properties: Connection URL: jdbc:mysql://localhost:3306/GREETINGS Database: corejava Password: ???

1.3. Creating a SQL Script File > New > Other... – SQL Development > SQL File > Next – File name: greetings.sql – Database server type: MySql_5.1 – Connection profile name: MySQL – Database name: GREETINGS – Finish

Content DROP TABLE IF EXISTS Greetings; CREATE TABLE Greetings( greeting_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, message VARCHAR(255) ); INSERT INTO Greetings(message) VALUES ('Hello, World!'); SELECT * FROM Greetings; DROP TABLE Greetings;

1.4. Executing SQL Script Right-click to an empty area of greetings.sql > Execute All

2. Solution Using Java 2.1. Creating an Empty Database 2.2. Copying the JDBC Driver JAR File to the Project Folder 2.3. Creating a Database Configuration File 2.4. Developing a Utility to Get a DB Connection 2.5. Developing the Test Class

2.1. Creating an Empty Database Same as the Solution Using SQL Script

2.2. Copying the JDBC Driver JAR File to the Project Folder JAR File: mysql-connector-java bin.jar In Eclipse – Create the lib folder: Right-click the project's root folder > New > Folder > Folder name: lib > Finish – Copy the JAR file to the lib folder – Right-click the JAR file > Build Path > Add to Build Path

2.3. Creating a Database Configuration File Right-click the project's root folder > New > File > File name: persistence.properties > Finish drivers=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/GREETINGS username=root password=root

2.4. Developing a Utility to Get a DB Connection

2.5. Developing the Test Class

Executing SQL Statements To execute a SQL statement, we first create a Statement object. – To create statement objects, use the Connection object that we obtained from DriverManager.getConnection. Statement stat = conn.createStatement(); Next, place the statement that we want to execute into a string String command = “INSERT INTO Greetings(message) VALUES('Hello World!')”;

String query = “SELECT * FROM Greetings”; Execute a statement: stat.executeUpdate(command); stat.executeQuery(query); – executeUpdate : can execute actions such as INSERT, UPDATE, and DELETE as well as data definition statements such as CREATE TABLE and DROP TABLE returns a count of the rows that were affected by the SQL statement, or zero for statements that do not return a row count

– executeQuery : executes SELECT queries returns an object of type ResultSet that you use to walk through the result one row at a time. ResultSet rs = stat.executeQuery("SELECT * FROM Greetings"); The basic loop for analyzing a result set looks like this: while (rs.next()) { //look at a row of the result set }

ResultSet – The order of the rows in a result set is completely arbitrary (unless you specifically ordered the result with an ORDER BY clause) – When inspecting an individual row, you will want to know the contents of the fields. – There are accessors for various types, such as getInt, getString and getDouble, getDate.... Each accessor has two forms one that takes a numeric argument: the first column is 1. one that takes a string argument.

try { Connection conn =...; try { Statement stat = conn.createStatement(); ResultSet result = stat.executeQuery(queryString); //process query result } catch(SQLException ex) { for (Throwable t : ex) { //do something with t t.printStackTrace(); } finally { conn.close(); } } catch (SQLException ex) { for (Throwable t : ex) { //do something with t t.printStackTrace(); }

References 1.Database Development in Eclipse with the Data Tools Platform (DTP) Core Java, Volume II – Advanced Features, Eighth Edition, Chapter 4. Cay S. Horstmann and Gary Cornell. Prentice Hall, 2008