Bolat Azamat, Kim Dongmin

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
1 Sub-queries and Views. 2 A Complex Query We would like to create a table containing 3 columns: –Sailor id –Sailor age –Age of the oldest Sailor How.
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.
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.
Think Possibility Integrating Web Applications With Databases.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
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.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
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.
Views, Indexes and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Haiman Tian 1.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
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.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
Index and JDBC/JSP tutorial Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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. 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.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
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 Session 14.
JDBC (Java Database Connectivity)
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
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.
Database Programming Basic JDBC Programming Concepts.
MYSQL APPLICATIONS USING JAVA & JDBC CIS 430 –Database Concepts Victor Matos.
CS3220 Web and Internet Programming Database Access with JDBC
Interacting with Database
Lec - 14.
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
How to connect natively?
JDBC 21-Aug-18.
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Introduction to Server-Side Web Development using JSP and Databases
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
JAVA DATABaSE CONNECTIVITY
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Bolat Azamat, Kim Dongmin JDBC Basics Bolat Azamat, Kim Dongmin

In this lesson you will learn basics of the JDBC API Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. Database Java application JDBC Java application uses JDBC to connect to a database.

JDBC Driver Java application We need this driver JDBC Oracle JDBC Driver for Oracle Microsoft SQL JDBC Driver for Microsoft SQL Java application Native ODBC Calls JDBC MySQL JDBC Driver for MySQL PostgreSQL JDBC Driver for PostgreSQL We need this driver

I kindly suggest you to use Java 8!!! Driver download Go the page https://jdbc.postgresql.org/download.html#current and download the current version 42.2.5 PostgreSQL JDBC 4.2 Driver, 42.2.5 – for Java 8 PostgreSQL JDBC 4.1 Driver, 42.2.5.jre7 – for Java 7 PostgreSQL JDBC 4.0 Driver, 42.2.5.jre6 – for Java 6 I kindly suggest you to use Java 8!!!

Development environment In this tutorial we use Eclipse IDE You can download it from https://www.eclipse.org/downloads/ It is free and most widely used one Also give a look at Intellij IDEA Apply for student license and save $499.00 https://www.jetbrains.com/student/

Create project File -> New -> Java Project Set project name as you want JRE – set your java 8 environment Press Finish

Project-> Properties Registering driver 1. 2. 3. Project-> Properties Drag and drop your downloaded jar to project folder Java Build Path -> Libraries -> Add JARs.. -> your file -> OK

Create main class src -> New -> Class Input any name you want and press Finish

Let’s check did you register driver public class HelloJDBC { public static void main(String[] args) { try { Class.forName("org.postgresql.Driver"); System.out.println(”Driver registered."); } catch (ClassNotFoundException e) { System.out.println("You didn't register driver"); } You should get the following output: Driver registered.

Connection to database DriverManager is java class, basic service for managing a JDBC drivers, it has the following method: getConnection(String url, String user, String password); url – database url user – database user password – database password url Connection con = DriverManager.getConnection(”jdbc:postgresql://localhost/jdbc_basics”, “postgres”, “zxc123”); IP address database name user password

Connection to database Add this code: //get connection Connection con = null; //initialize variable try { con = DriverManager.getConnection("jdbc:postgresql://localhost/jdbc_basics", "postgres", "zxc123"); System.out.println("Successfully connected."); } catch (SQLException e) { System.out.println("Problem with your connection"); } You should get the following output: Driver registered. Successfully connected.

Executing SQL Statements Statement – interface contains functions for executing SQL commands. Statement stmt = con.createStatement(); Executing SQL instructions can be done through the use of three methods: 1. executeQuery(); - for SELECT instructions; 2. executeUpdate(); - for updating the data or the database structure; 3. execute(); - can be used for both cases when result is unknown;

Creating the table To create table with students, we need to make SQL query : CREATE TABLE IF NOT EXISTS students( student_id SERIAL, name varchar(30), score numeric )

Run query Let’s use the execute() method to add a students table to our database: stmt = con.createStatement(); String tableSql = "CREATE TABLE IF NOT EXISTS students(\n" +  " student_id SERIAL, \n" + " name varchar(30),\n" + " score numeric\n" + ")"; stmt.execute(tableSql);

Insert into a table Let’s insert a row into our table, using following SQL: INSERT INTO students(name, score) VALUES('dongmin', 50.0); Next, let’s add a record to our table using the executeUpdate() method: String insertSql = "INSERT INTO students(name, score) VALUES('dongmin', 50.0);";  stmt.executeUpdate(insertSql);

Select from a table We can retrieve the records from the table using the executeQuery() method which returns an object of type ResultSet: String selectSql = "SELECT * FROM students"; ResultSet resultSet = stmt.executeQuery(selectSql); while(resultSet.next()) { System.out.println("Name: " + resultSet.getString("name")+ " Score: " +resultSet.getDouble("score")); }

PreparedStatements PreparedStatement objects contain precompiled SQL sequences. They can have one or more parameters denoted by a question mark. Let’s create a PreparedStatement which updates records in the students table based on given parameters: String updatePositionSql = "UPDATE students SET score=? WHERE student_id=?"; PreparedStatement pstmt = con.prepareStatement(updatePositionSql); To add parameters to the PreparedStatement, we can use simple setters – setX() – where X is the type of the parameter, and the method arguments are the order and value of the parameter: pstmt.setDouble(1, 99.0); pstmt.setInt(2, 1); The statement is executed with one of the same three methods described before: executeQuery(), executeUpdate(), execute() without the SQL String parameter: int rowsAffected = pstmt.executeUpdate();

Update table //update table String updatePositionSql = "UPDATE students SET score=? WHERE student_id=?"; PreparedStatement pstmt = con.prepareStatement(updatePositionSql); pstmt.setDouble(1, 99.0); //set new score pstmt.setInt(2, 1); //where student_id equal to 1 int rowsAffected = pstmt.executeUpdate(); System.out.println("User update, rows affected: "+rowsAffected);

Delete record from the table Delete record is also action of updating table! //delete record String deleteRecord = "DELETE from students WHERE student_id=?"; pstmt = con.prepareStatement(deleteRecord); pstmt.setInt(1, 1); rowsAffected = pstmt.executeUpdate(); System.out.println("User deleted, rows affected: "+rowsAffected);

Delete whole table Drop table from database, be carefully this action is irreversible! //delete whole table String drop = "DROP TABLE students;"; stmt.executeUpdate(drop);

Close your connection When you finished all your operations with database, please close your connection! con.close(); Releases Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.

Thank you for your attention! Further readings: - Official Java documentation: https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html - Nice and easy tutorials on JAVA JDBC: https://www.tutorialspoint.com/jdbc/