JDBC and Hibernate Joshua Scotton. Connecting to Relational DBs.

Slides:



Advertisements
Similar presentations
1 JDBC Java Database Connectivity. 2 c.pdf
Advertisements

1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC: Part I Attribution These slides are based on three primary sources: –Sun JDBC Tutorial URL: jdbc/TOC.htmlhttp://java.sun.com/docs/books/tutorial/
Java Database Connectivity JDBC ICW Lecture 12 Errol Thompson.
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.
1 JDBC "Java Database Connectivity". 2 Getting Started Guide: etstart/GettingStartedTOC.fm.html java.sql.
Session-01. Hibernate Framework ? Why we use Hibernate ?
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.
Think Possibility Integrating Web Applications With Databases.
Database Management Systems 1 Oracle Programming.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
CS 405G: Introduction to Database Systems Database programming.
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.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Chapter 8 Databases.
JDBC – Java Database Concentricity
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.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
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.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Tasks Needed for MissionMapEditor Martin Q. Zhao September 18, 2010.
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
JDBC Part II CS 124. More about JDBC Types Statement versus PreparedStatement Timeout NULL values Meta-data close() methods Exceptions Transactions JDBC.
JDBC (Java Database Connectivity)
Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
1 JDBC: Java Database Connectivity. 2 Introduction to JDBC JDBC is used for accessing databases from Java applications Information is transferred from.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
JDBC.
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.
Instructor: Jinze Liu Fall /8/2016Jinze University of Kentucky 2 Database Project Database Architecture Database programming.
Database Activity CEMC Steps 1. Verify Data Tools Platform is installed in Eclipse 2. Configure a Connection to Derby Create necessary table for.
JSP and DB.
CS3220 Web and Internet Programming Database Access with JDBC
EE557: Server-Side Development
Lec - 14.
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
Advanced Web Automation Using Selenium
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Client Access, Queries, Stored Procedures, JDBC
Interacting with Database
JDBC – Java Database Connectivity
Database Programming Using JDBC and MySQL C API
Super Market Management
Bolat Azamat, Kim Dongmin
Using a Database with JDBC
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

JDBC and Hibernate Joshua Scotton

Connecting to Relational DBs

try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); }

try { Connection con = DriverManager.getConnection(url, user, pass); con.close(); } catch (SQLException e) { e.printStackTrace(); }

stmt = con.createStatement(); stmt.executeUpdate(sqlString); stmt.close(); stmt.executeQuery(sqlString);

Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(url, user, pass); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sqlString); while (rs.next()) { System.out.println(rs.getString("username")); } stmt.close(); con.close();

 next() - moves the cursor forward one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned after the last row.  previous() - moves the cursor backwards one row. Returns true if the cursor is now positioned on a row and false if the cursor is positioned before the first row.  first() - moves the cursor to the first row in the ResultSet object. Returns true if the cursor is now positioned on the first row and false if the ResultSet object does not contain any rows.  last() - moves the cursor to the last row in the ResultSet object. Returns true if the cursor is now positioned on the last row and false if the ResultSet object does not contain any rows.

 /register?action=new-user ◦ New user entry form  /register?action=register ◦ Saves new user to database

String sql1="insert into user (username, password) values ('"+username+"','"+password+"')"; String sql2="insert into role (username, role) values ('"+username+"','user')"; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(url, user, pass); Statement stmt = con.createStatement(); stmt.executeUpdate(sql1); stmt.executeUpdate(sql2); stmt.close(); con.close();

Hacking the Registration Form

 Username: “Josh”  "insert into role (username, role) values ('"+username+"','user')“  SQL: insert into role (username, role) values (‘Josh','user')

 Username: “Josh’,’admin’) -- “  "insert into role (username, role) values ('"+username+"','user')“  SQL: insert into role (username, role) values (‘Josh',’admin’) -- 'user')

sql="insert into user (username, password) values (?,?)”; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.executeUpdate();

try { con.setAutoCommit(false); PreparedStatement pstmt = con.prepareStatement(sql1); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.executeUpdate(); pstmt = con.prepareStatement(sql2); pstmt.setString(1, username); pstmt.executeUpdate(); con.commit(); } catch ( SQLException e ) { con.rollback(); }

Database Abstraction Layer

 Download jar from  Create the Java Objects  Create Mapping Files  Create the Hibernate Configuration File  Create a Session management class

 In TestBean:  In TestQuestionBean:

 This defines the database configuration  Hibernate will work with many different database types including: ◦ MySQL ◦ HSQL DB ◦ Oracle ◦ MS SQL Server

com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/quizmaster username password org.hibernate.dialect.MySQLDialect true thread create <mapping resource="webdev/quizmaster/HibernateMapping.hbm.xml" />

 Not mandatory but used in most cases  Handles session creation

Session session = HibernateUtil.getCurrentSession(); session.beginTransaction(); TestBean tBean = new TestBean(); session.save(tBean); session.getTransaction().commit();

Session session = HibernateUtil.getSessionFactory().openSession(); Transaction trans = session.beginTransaction(); List tests = session.createQuery("from Test as t order by t.test_title asc").list(); trans.commit(); session.close();

 You work with objects in your system (if your system has been designed well). Even if using JDBC, you will end up making some translation layer, so that you transfer your data to your objects. Unless you are extremely good Hibernate will be better at translation than any custom-made solution.  It doesn't deprive you of control. You can control things in very small details, and if the API doesn't have some remote feature - execute a native query and you have it. However:  ORMs do add a small performance overhead, which in some cases can't be ignored. It will depend on your application and whether this overhead is significant enough to outweigh the benefits of using an ORM.

 Stability - being around for so many years, it lacks any major problems  dictates the standards in the ORM field  Documentation – There are many tutorials, common problem solutions, etc  Powerful - you can translate a very complex object model into a relational model.  Database Support - it has support for any major and medium RDBMS

Connecting to the Database

 Added JavaBeans for Result, ResultAnswer  Added Hibernate mapping for all Beans  viewTest now allows the user to take the test  saveResult saves score  Added new view for listTests to index  Added new view for listResults to admin  Added TestManager