CS3220 Web and Internet Programming Database Access with JDBC

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

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.
1 JDBC Java Database Connectivity. 2 c.pdf
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
1 JDBC "Java Database Connectivity". 2 Getting Started Guide: etstart/GettingStartedTOC.fm.html java.sql.
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.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
JDBC and Hibernate Joshua Scotton. Connecting to Relational DBs.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
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.
1 JDBC – Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Chapter 8 Databases.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
JDBC – Java Database Concentricity
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
JDBC Part II CS 124. More about JDBC Types Statement versus PreparedStatement Timeout NULL values Meta-data close() methods Exceptions Transactions JDBC.
Basics of JDBC.
JDBC (Java Database Connectivity)
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
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.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
JDBC.
1 JDBC – Java Database Connectivity THETOPPERSWAY.COM.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JSP and DB.
Database: JDBC Overview
DEPTT. OF COMP. SC & APPLICATIONS
CS3220 Web and Internet Programming Database Access with JDBC
Lec - 14.
Chengyu Sun California State University, Los Angeles
CS320 Web and Internet Programming Database Access with JSTL SQL
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
Database JDBC Overview CS Programming Languages for Web Applications
Advanced Web Automation Using Selenium
HW#4 Making Simple BBS Using JDBC
Introduction to Server-Side Web Development using JSP and Databases
Interacting with Database
Bolat Azamat, Kim Dongmin
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

CS3220 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles

Client-Server Architecture of Databases mysql MySQL Workbench PHPMyAdmin … Applications DB DB DB A library that sends SQL and other commands from client to server, and get the results from server to client.

JDBC Java Program JDBC Driver DB Server JDBC API DBMS MySQL Independent MySQL

About JDBC Java DataBase Connectivity JDBC API is DBMS independent JDBC Driver implements JDBC API for a particular DBMS

Example: HelloJDBC.java Make connections Execute queries Process results Handle exceptions (and close connections)

JDBC Basics … import java.sql.*; Create connection URL jdbc:mysql://[host:port]/[database][?user=cs3220stu31&password=abcd] DriverManager.getConnection( URL ) DriverManager.getConnection( URL, user, pass )

… JDBC Basics Create statement Get result back Statement stmt = c.createStatement(); stmt.executeQuery(String sql) stmt.executeUpdate(String sql) Get result back ResultSet rs http://docs.oracle.com/javase/tutorial/jdbc/index.html

About Loading JDBC Driver … try { Class.forName( "com.mysql.jdbc.Driver" ); } catch( ClassNotFoundException e ) throw new ServletException( e ); See an example at https://csns.calstatela.edu/download?fileId=5899744

… About Loading JDBC Driver Needed for older version of Java (before version 6) Needed for older version of MySQL JDBC driver (before version 8) Only needs to be done once per project (not once per servlet)

DB Query Results In a program, we want to Access each record Access each attribute in a record Access the name of each attribute select * from items; name price quantity Milk 3.89 2 Beer 6.99 1

JDBC ResultSet – Row Access Cursor Record 1 Record 2 Record 3 … Results next() – move cursor down one row Cursor starts from before the 1st record true if the current record is valid false if no more records

Common Code for Processing ResultSet Process each row while(rs.next()) {…} Check whether a result set is empty if(rs.next()) {…}

JDBC ResultSet – Column Access Access the columns of current row getXxx( String columnName ) E.g. getString( “user” ); getXxx( int columnIndex ) columnIndex starts from 1 E.g. getString( 1 ); http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

JDBC ResultSet – Access Column Names ResultSetMetaData meta = rs.getMetaData(); ResultSetMetaData getColumnName( columnIndex ) Column name getColumnLabel( columnIndex ) Column title for display or printout

Handle Exceptions catch( SQLException e ) { e.printStackTrace(); } finally try if( c != null ) c.close();

Example: GuestBook (JDBC) – Display Create a guest_book table Retrieve the entries in a servlet Display the entries in a JSP

Example: GuestBook (JDBC) – Add Save new guest book entries to the database executeQuery() vs. executeUpdate() Potential problems of handing user input Special characters SQL injection attack

Example: SQL Injection Attack User input should NOT be trusted Regular user input Username: cysun Password: abcd Malicious user input Username: someuser Password: something’ or ‘1 Prevent SQL injection attack?

Prepared Statements Statements with parameters String sql = “insert into items values (?, ?, ?)”; PreparedStatement pstmt =c.prepareStatement(sql); pstmt.setString(1, “orange”); pstmt.setBigDecimal(2, 0.59); pstmt.setInt(3, 4); pstmt.executeUpdate();

Benefits of Prepared Statements Special characters are properly handled Secure if the SQL statement is constructed from user input The SQL statement is more readable Better performance

Beyond the Basics ... Transaction ACID transaction disable auto commit send queries/updates commit enable auto commit exception rollback

... Beyond the Basics ... It’s rather expensive to open a db connection So how about once we open a connection, we leave it open forever?? Connection Pool Max number of connections Max number of idle connections And many other configurable parameters http://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html

... Beyond the Basics Mismatch between an OO design and a relational design Object-Relational Mapping JPA and Hibernate - http://hibernate.org/orm/