Lecture 11 Database Connection

Slides:



Advertisements
Similar presentations
Basics of Database Programming with VB6
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Concepts of Database Management Sixth Edition
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Introduction to JDBC (Java Database Connectivity).
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. –
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
ASP.NET Programming with C# and SQL Server First Edition
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 13 Storing Information, Object Serialization,
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Views, Indexes and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Haiman Tian 1.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
Concepts of Database Management Seventh Edition
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.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
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.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Basics of JDBC Session 14.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
SQL and Java The vision for Java is to be the concrete and nails that people use to build this incredible network system that is happening all around us.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
3 A Guide to MySQL.
ASP.NET Programming with C# and SQL Server First Edition
DEPTT. OF COMP. SC & APPLICATIONS
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
JDBC Database Management Database connectivity
 2012 Pearson Education, Inc. All rights reserved.
CS320 Web and Internet Programming Database Access with JDBC
Server-Side Application and Data Management IT IS 3105 (FALL 2009)
JDBC.
Objectives In this lesson, you will learn about:
Chapter 8 Working with Databases and MySQL
Databases - בסיסי נתונים
Interacting with Database
CIS16 Application Programming with Visual Basic
CodePainter Revolution Trainer Course
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Lecture 11 Database Connection Jaeki Song

Outline Introduction Java Database Connectivity

Introduction File processing Database systems Random access or sequential Only allow access to data Cannot query Database systems Mechanisms to organize and store data Allow sophisticated queries Relational database - most popular style Microsoft Access, Sybase, Oracle Structured Query Language (SQL, "sequel") Queries relational databases Can write Java programs to use SQL queries

Database Language Database language Used to access database Can use high-level languages Java, C, C++, Visual Basic, COBOL, PL/I, Pascal Make requests using a specially designed query language Host language

Relational Database Model Database models Hierarchal, network, relational (most popular) Focus on relational Relational Database Model Composed of tables Rows called records Columns are fields (attributes) First field usually primary key Unique for each record Primary key can be more than one field (column) Primary key not required

Relational Database Structure Table: Employee Number Name Department Salary Location 23603 JONES, A. 413 1100 NEW JERSEY 24568 KERWIN, R. 413 2000 NEW JERSEY A record 34589 LARSON, P. 642 1800 LOS ANGELES 35761 MYERS, B. 611 1400 ORLANDO 47132 NEUMANN, C. 413 9000 NEW JERSEY 78321 STEPHENS, T. 611 8000 ORLANDO Primary Key A column

Advantages Tables easy to use, understand, and implement Easy to convert other database structures into relational scheme Universal Projection and join operations easy to implement Searches faster than schemes with pointers Easy to modify - very flexible Greater clarity and visibility than other models

Java Database Connectivity You can access data files in Java using JDBC JDBC classes handle communication between your Java program and a database driver Java uses JDBC-ODBC Bridge

Data Source Name (DSN) Before using JDBC in a Window environment, you need to create a DSN DSN registers your database files on the computer Setting up a DSN Step1: From the start menu, select control panel Step2: select ODBC Data source Step3: On the User DSN tab, click on the Add button

Data Source Name (DSN)

Data Source Name (DSN) Step4: Select the database type (Microsoft Access database), and click Finish

Data Source Name (DSN) Step5: Type in the Data Source Name

Data Source Name (DSN) Step6: Use the Select (or Browse) button to locate the file, then close the dialog boxes

JDBC-ODBC Bridge Driver In Java code, you must connect to the driver using the Class.forName method Connect to Microsoft’s driver Class.forName(“com.ms.jdbc.odbc.JdbcOdbcDriver”); Connect to Sun’s driver Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

JDBC-ODBC Bridge Driver If you need to be able to load either driver, you can use try/catch block try { Class.forName(“com.ms.jdbc.odbc.JdbcOdbcDriver”); } catch { try {Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);} catch(ClassNotFoundException err) { System.err.println(“Driver did not load properly”);} }

Connecting to the Database Next step is to connect to the database using the name that your registered as a DSN “jdbc:odbc:EX1” Use DriverManager.getConnection method and assign it to a Connection object conEmployees = DriverManager.getConnection(“jdbc:odbc:EX1”);

Connecting to the Database //Declare a Connection object Connection conEmployees; //Connect to the database try { conEmployees = DriverManager.getConnection (“jdbc:odbc:EX1”); } catch(SQLException err) statement // “No connection to database”

Creating a ResultSet A ResultSet object contains a collection of records from the database To create ResultSet, you must declare a Statement object and call the Connection objects’ createStatement method Statement cmdEmployees; ResultSet rsEmployees; You create a ResultSet with an SQL query with executeQuery method Select * from Employees cmdEmployees.executeQuery(“Select * from Employees”);

Example try { Connection conEmployees; conEmployees = DriverManager.getConnection (“jdbc:odbc:EX1”); //create a ResultSet Statement cmdEmployees = conEmployees.createStatement(); ResultSet rsEmployees = cmdEmployees.executeQuery (“Select * from Employees”); } catch(SQLException err) statement // “No connection to database”

Example You can create an SQL statement to create a ResultSet that matches a given condition ResultSet rsEmployees = cmdEmployees.executeQuery ( “Select * from Employees Where [Last Name] = ‘Song’ ”); ResultSet rsEmployees = cmdEmployees.executeQuery ( “Select * from Employees Where [Last Name] = ‘ “ + strLastName + ” ’ ”);

Retrieving a Record Looping through a ResultSet Displaying the fields from a record

Retrieving a Record When you first open a ResutSet, the current-record is located just prior to the first record You call the ResultSet’s next method The next method moves the current-record pointer to the first record and returns boolean value true: the next record exists false: no more records exists

Example try { rsEmployees.next( ); The getString method retrieves the data for the specified string field try { rsEmployees.next( ); lstNames.add(rsEmployees.getString (“Last Name”)); } catch (SQLException err) System.err.println(“Error : “ + err.toString());

Looping through a ResultSet You can step through all records in a ResultSet using a loop while (rsResultSet.next( )) to control the loop The boolean condition tests true as long as records remain in the ResultSet while(rsEmployees.next( )) lstNames.add(rsEmployees.getString(“Last Name”));

Displaying the Fields try { //display information if (rsEmployees.next( )) { lblFirstName.setText(rsEmployees.getString(“ First Name”); lblSSN.setText(rsEmployees.getString(“SSN”)); lblSSN.setText(rsEmployees.getString(“Phone Number”)); } else //Statement catch (SQLException err) System.err.println(“Error : “ + err.toString());

Accessing the Data Fields Access the data in database fields by using the appropriate method for the data type getString for string field getFloat or getInt for float or int fields Convert the value to a string format to display the value in a label or text field

Closing the Connection Make sure that the database connection is closed at the termination of the program Place the statement in the stop method Place the code in your exit routine public void stop( ) { if (conEmployees != null) conEmployees.close( ); }

SQL Java’s JDBC uses Structured Query Language (SQL) to create a ResultSet ResultSet rsEmployees = cmdEmployees.executeQuery(“Select * from Employees”); ResultSet rsEmployees = cmdEmployees.executeQuery(“Select * from Employees where [Last Name] = ‘ “ + strLastName + ” ’; ”);

Types of SQL Statement For the field(s), you can list the field names or use an asterisk to indicate all fields from the named table. Multiple-world field names must be enclosed in square brackets or accent grave marks (‘) The closing semicolon(;) is specified in the SQL standards

SQL Examples SELECT Where “Select * from Employees;” “Select * from Employees where [Last Name] = ‘ ABC’ ”; “Select * from Employees where [Last Name] = ‘ “ + strLastName + ”’; ”

Adding a Record SQL insert statement-General format Insert Into TableName (FieldList) Values (ListOfValue) Use the statement object’s executeUpdate method to excute the SQL Insert Into statement

Example cmdEmplyees.executeUpdate (“ Insert Into Employees” + “( [Last Name] , [First Name], SSN, [Phone Number]) ” + “Values( ‘ “ + txtLastName.getText() + ” ’, ‘ “ + txtFirstName.getText( ) + ” ’)”);

Finding a Record SQL Statement strLastName = txtLastName.getText(); String query = “Select *from Employees where [Last Name] = ‘ “ + strLastName + ’ ” ”; rsEmployees = statement.executeQuery(query);