JDBC 15-Apr-18.

Slides:



Advertisements
Similar presentations
Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
Advertisements

Database programming in Java An introduction to Java Database Connectivity (JDBC)
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
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.
1 JDBC Java Database Connectivity. 2 c.pdf
Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Advanced Java Programming and Database connection Eran.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
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. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
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.
Think Possibility Integrating Web Applications With Databases.
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.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JAVA Database Access. JDBC The Java Database Connectivity (JDBC) API is the industry standard for database- independent connectivity between the Java.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
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.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
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.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
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.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
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.
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
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.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
MySQL root 암호 $ mysqladmin -u root -p password new-password $ mysql -u root mysql mysql> update user set password = password('new-password') where user.
Database Programming Basic JDBC Programming Concepts.
MYSQL APPLICATIONS USING JAVA & JDBC CIS 430 –Database Concepts Victor Matos.
Java Access to RDB Relational databases (RDBs) dominate today, due to:
Interacting with Database
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
JDBC 15-Nov-18.
Interacting with Database
Bolat Azamat, Kim Dongmin
Using a Database with JDBC
JAVA DATABaSE CONNECTIVITY
JDBC API.
JDBC Example.
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

JDBC 15-Apr-18

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 of SQL JDBC is a means of accessing SQL databases from Java JDBC is a standardized API for use by Java programs JDBC is also a specification for how third-party vendors should write database drivers to access specific SQL versions

Driver types There are four types of drivers: JDBC Type 1 Driver -- JDBC/ODBC Bridge drivers ODBC (Open DataBase Connectivity) is a standard software API designed to be independent of specific programming languages Sun provides a JDBC/ODBC implementation JDBC Type 2 Driver -- use platform-specific APIs for data access JDBC Type 3 Driver -- 100% Java, use a net protocol to access a remote listener and map calls into vendor-specific calls JDBC Type 4 Driver -- 100% Java Most efficient of all driver types

Connector/J Connector/J is a JDBC Type 4 Driver for connecting Java to MySQL Installation is very simple: Download the “Production Release” ZIP file from http://dev.mysql.com/downloads/connector/j/3.1.html Unzip it Put the JAR file where Java can find it Add the JAR file to your CLASSPATH, or In NetBeans: Project  Libraries  Add Jar In Eclipse: Project --> Properties --> Java Build Path --> Libraries --> Add External Jars...

Connecting to the server First, make sure the MySQL server is running In your program, import java.sql.Connection; // not com.mysql.jdbc.Connection import java.sql.DriverManager; import java.sql.SQLException; Register the JDBC driver, Class.forName("com.mysql.jdbc.Driver").newInstance(); Invoke the getConnection() method, Connection con = DriverManager.getConnection("jdbc:mysql:///myDB", myUserName, myPassword);

A complete program import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JdbcExample1 { public static void main(String args[]) { Connection con = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql:///test", "root", ”rootpswd"); if (!con.isClosed()) System.out.println("Successfully connected to MySQL server..."); } catch(Exception e) { System.err.println("Exception: " + e.getMessage()); } finally { try { if (con != null) con.close(); } catch(SQLException e) {} } } } Adapted from http://www.stardeveloper.com/articles/display.html?article=2003090401

Using the Connection object public Statement createStatement() throws SQLException Creates a Statement object for sending SQL statements to the database. SQL statements without parameters are normally executed using Statement objects. The Statement object may be reused for many statements public PreparedStatement prepareStatement(String sql) throws SQLException Creates a PreparedStatement object for sending parameterized SQL statements to the database. A SQL statement with or without IN parameters can be pre-compiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times.

Issuing queries The following are methods on the Statement object: int executeUpdate() -- for issuing queries that modify the database and return no result set Use for DROP TABLE, CREATE TABLE, and INSERT Returns the number of rows in the resultant table ResultSet executeQuery() -- for queries that do return a result set. Returns results as a ResultSet object

Creating a table This example is from http://www.kitebird.com/articles/jdbc.html CREATE TABLE animal ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), name CHAR(40), category CHAR(40) ) Statement s = conn.createStatement (); s.executeUpdate ("DROP TABLE IF EXISTS animal"); s.executeUpdate ( "CREATE TABLE animal (" + "id INT UNSIGNED NOT NULL AUTO_INCREMENT," + "PRIMARY KEY (id),“ + "name CHAR(40), category CHAR(40))");

Populating the table int count; count = s.executeUpdate ( "INSERT INTO animal (name, category)" + " VALUES" + "('snake', 'reptile')," + "('frog', 'amphibian')," + "('tuna', 'fish')," + "('racoon', 'mammal')"); s.close (); System.out.println (count + " rows were inserted");

ResultSet executeQuery() returns a ResultSet ResultSet has a very large number of getXXX methods, such as public String getString(String columnName) public String getString(int columnIndex) Results are returned from the current row You can iterate over the rows: public boolean next() ResultSet objects, like Statement objects, should be closed when you are done with them public void close()

Example, continued Statement s = conn.createStatement (); s.executeQuery ("SELECT id, name, category " + "FROM animal"); ResultSet rs = s.getResultSet (); int count = 0; // Loop (next slide) goes here rs.close (); s.close (); System.out.println (count + " rows were retrieved");

Example, continued while (rs.next ()) { int idVal = rs.getInt ("id"); String nameVal = rs.getString ("name"); String catVal = rs.getString ("category"); System.out.println ( "id = " + idVal + ", name = " + nameVal + ", category = " + catVal); ++count; }

Prepared statements Prepared statements are precompiled, hence much more efficient to use PreparedStatement s; s = conn.prepareStatement ( "INSERT INTO animal (name, category VALUES(?,?)"); s.setString (1, nameVal); s.setString (2, catVal); int count = s.executeUpdate (); s.close (); System.out.println (count + " rows were inserted");

Error handling try { Statement s = conn.createStatement (); s.executeQuery ("XYZ"); // issue invalid query s.close (); } catch (SQLException e) { System.err.println ("Error message: " + e.getMessage ()); System.err.println ("Error number: " + e.getErrorCode ()); }

The End