JDBC: A simple tutorial

Similar documents
Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Brazil + JDBC Juin 2001, douin@cnam.fr

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

The JAVA Way: JDBC and SQLJ

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

DATABASDESIGN FÖR INGENJÖRER - 1DL124

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

CS/CE 2336 Computer Science II

JDBC (Java / SQL Programming) CS 377: Database Systems

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

Security Module: SQL Injection

Tutorial for Spring DAO with JDBC

CSc 230 Software System Engineering FINAL REPORT. Project Management System. Prof.: Doan Nguyen. Submitted By: Parita Shah Ajinkya Ladkhedkar

CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

Applets, RMI, JDBC Exam Review

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

Java and Microsoft Access SQL Tutorial

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

Package sjdbc. R topics documented: February 20, 2015

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

SQL and programming languages

How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu (Amd66) On Microsoft

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

Using the SQL TAS v4

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

LSINF1124 Projet de programmation

Using Netbeans and the Derby Database for Projects Contents

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Microsoft SQL Server Features that can be used with the IBM i

LAB 6: Code Generation with Visual Paradigm for UML and JDBC Integration

How To Let A Lecturer Know If Someone Is At A Lecture Or If They Are At A Guesthouse

Java Server Pages and Java Beans

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

A Brief Introduction to MySQL

Introduction Web Portal Main Page Group Management Create group Modify Group Member List... 5

1 SQL Data Types and Schemas

Precondition for a good understanding: knowledge of a higher programming language (e.g. C, Java, Perl, Pascal, Basic,... ) basic knowledge of html

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

2. Follow the installation directions and install the server on ccc

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

CS346: Database Programming.

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

Apéndice C: Código Fuente del Programa DBConnection.java

Using SQL Server Management Studio

FileMaker 14. ODBC and JDBC Guide

DEVELOPING MULTITHREADED DATABASE APPLICATION USING JAVA TOOLS AND ORACLE DATABASE MANAGEMENT SYSTEM IN INTRANET ENVIRONMENT

Custom Encryption in Siebel & Siebel Web Service Security Test Guide 1.0

Abstract. Introduction. Web Technology and Thin Clients. What s New in Java Version 1.1

FileMaker 13. ODBC and JDBC Guide

Database Application Development. Overview. SQL in Application Code. Chapter 6

Introduction to Web Application Security. Microsoft CSO Roundtable Houston, TX. September 13 th, 2006

CHAPTER 3. Relational Database Management System: Oracle. 3.1 COMPANY Database

FileMaker 12. ODBC and JDBC Guide

Accesssing External Databases From ILE RPG (with help from Java)

FileMaker 11. ODBC and JDBC Guide

Object Oriented Design with UML and Java. PART XVIII: Database Technology

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

Using JML to protect Java code against SQL injection. Johan Janssen June 26, 2007

MAKING ORACLE AND SQLJ WORK FOR YOU John Jay King, King Training Resources

SQL Injection Attack Lab Using Collabtive

Specialized Programme on Web Application Development using Open Source Tools

Specialized Programme on Web Application Development using Open Source Tools

Application note: Connecting the to a Database

Self-test Database application programming with JDBC

Mini Project Report ONLINE SHOPPING SYSTEM

Configuring an Alternative Database for SAS Web Infrastructure Platform Services

DIPLOMA IN WEBDEVELOPMENT

Introduction to Hadoop on the cloud using BigInsights on BlueMix Feb , 2014

Performance Tuning for the JDBC TM API

Exploiting Java Technology with the SAS Software Barbara Walters, SAS Institute Inc., Cary, NC

Java Programming. JDBC Spring Framework Web Services

Using the DataDirect Connect for JDBC Drivers with WebLogic 8.1

CSCI110 Exercise 4: Database - MySQL

Using DOTS as Apache Derby System Test

Java MySQL Connector & Connection Pool

Hibernate Language Binding Guide For The Connection Cloud Using Java Persistence API (JAP)

Chapter 1 JDBC: Databases The Java Way! What Is The JDBC? The JDBC Structure ODBC s Part In The JDBC Summary

Developing SQL and PL/SQL with JDeveloper

Part IV: Java Database Programming

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

Spring Data JDBC Extensions Reference Documentation

Comparing the Effectiveness of Penetration Testing and Static Code Analysis

Tutorial: How to Use SQL Server Management Studio from Home

Security Code Review- Identifying Web Vulnerabilities

3D VIRTUAL DESKTOP APPLICATION Of UNSW. Introduction. What we propose to do 2/4. What we propose to do 1/4. What we propose to do 4/4

ESET Secure Authentication Java SDK

Brekeke SIP Server Version 3 Using a Third-Party Database Tutorial Brekeke Software, Inc.

IBM DB2 XML support. How to Configure the IBM DB2 Support in oxygen

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

SINGLE SIGNON FUNCTIONALITY IN HATS USING MICROSOFT SHAREPOINT PORTAL

Java and RDBMS. Married with issues. Database constraints

Transcription:

JDBC: A simple tutorial The following tutorial will be useful if you are developing the application in Java. Note, you are not restricted to using the java environment only. Java provides the programmers an API popularly called the JDBC (Java Database connectivity) to connect and. We will in this document provide the details of utilizing the power of this API. Installing the JDBC driver: The first step is to install the JBDC driver. The MySQL JDBC driver is available at: http://dev.mysql.com/downloads/connector/j/3.0.html The JBDC driver is a jar file. All you need to do is to place this jar file in the classpath. Once this is done, you will be able to access the functionality provided by the API. Simple Test Program: Once you are done installing the JDBC driver. You are in a position to execute the following simple java program. This java program illustrates the following aspects of JDBC: a) creation of a table b) deletion of a table c) inserting into a table and d) querying a table. import java.sql.*; public class DBConnector { private Connection con = null; private String username = "rjammala"; private String password = "********"; private String url = "jdbc:mysql://mycomputer_ip/project2"; public void ConnecToDB() { try { Statement stmt; ResultSet rs; //Register the JDBC driver for MySQL. Class.forName("com.mysql.jdbc.Driver"); //Get a connection to a database... con = DriverManager.getConnection( url, username, password); catch(exception e){ System.out.println(e);

return ; public Connection ReturnConnectionObject() { return con; public static void main ( String args[]) throws Exception { DBConnector DBCon = new DBConnector(); DBCon.ConnecToDB(); Connection con = DBCon.ReturnConnectionObject(); Statement stmt = con.createstatement(); String SQL = "DROP TABLE simple"; SQL = "CREATE TABLE simple( temp varchar(300))"; System.out.println("table created"); // Inserting something into the SQL = "Insert into simple values('10')"; System.out.println("Tuple inserted in the table"); SQL = "Select * from simple"; ResultSet rs; rs = stmt.executequery(sql); while ( rs.next()) { System.out.println( "temp:" + rs.getstring(1)); System.out.println("Congratulations you are done");

We will now explain some parts of the above java program. Class.forName("com.mysql.jdbc.Driver"); The above statement loads all the classes related to the JDBC driver in the program. Now you will able to access all the required classes in JDBC. private String username = "rjammala"; private String password = "********"; private String url = "jdbc:mysql://mycomputer_ip/project2"; The above information provides the java program with the necessary information to connect to your database. Assume that you end up creating a Project2 database in your MYSQL server. Then string url specified above will direct the java program to look at the right place for storing and retrieving data. MyComputer_IP represents the IP address of the machine on which the SQL server is running. Variables username and password have the obvious meaning. Such information is required for the user to authenticate with the SQL database. con = DriverManager.getConnection( url, username, password); The above statement creates a connection object that can now communicate with your database. Now let us see what can be done with the con object. Consider the following statements: Statement stmt = con.createstatement(); String SQL = "Select * from simple"; rs = The connection class will allow you to create statement objects that can now directly execute the SQL statements, as illustrated in the above example. After executing the queries, JDBC allows you to capture the output in the form of a resultset object. Use can then use the resultset object to print out the final results.

while ( rs.next()) { System.out.println( "temp:" + rs.getstring(1)); The above code loops through the resultset object to printout the relevant results. The getstring(i) function returns the string representation of the value of i th column in table. Note, that you need to make sure when you use getstring function, that the corresponding column in the table is a string. There are other functions available that other data types. Please contact me rjammala@ics.uci.edu if you have further questions about the material presented above.