Download presentation
Presentation is loading. Please wait.
Published byFranklin Wilkins Modified over 9 years ago
1
JDBC and Hibernate Joshua Scotton
2
Connecting to Relational DBs
3
try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); }
4
try { Connection con = DriverManager.getConnection(url, user, pass); con.close(); } catch (SQLException e) { e.printStackTrace(); }
5
stmt = con.createStatement(); stmt.executeUpdate(sqlString); stmt.close(); stmt.executeQuery(sqlString);
6
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();
7
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.
8
/register?action=new-user ◦ New user entry form /register?action=register ◦ Saves new user to database
9
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();
10
Hacking the Registration Form
11
Username: “Josh” "insert into role (username, role) values ('"+username+"','user')“ SQL: insert into role (username, role) values (‘Josh','user')
12
Username: “Josh’,’admin’) -- “ "insert into role (username, role) values ('"+username+"','user')“ SQL: insert into role (username, role) values (‘Josh',’admin’) -- 'user')
13
sql="insert into user (username, password) values (?,?)”; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.executeUpdate();
14
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(); }
15
Database Abstraction Layer
17
Download jar from http://www.hibernate.org/downloads Create the Java Objects Create Mapping Files Create the Hibernate Configuration File Create a Session management class
20
In TestBean: In TestQuestionBean:
21
This defines the database configuration Hibernate will work with many different database types including: ◦ MySQL ◦ HSQL DB ◦ Oracle ◦ MS SQL Server
22
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" />
23
Not mandatory but used in most cases Handles session creation
24
Session session = HibernateUtil.getCurrentSession(); session.beginTransaction(); TestBean tBean = new TestBean(); session.save(tBean); session.getTransaction().commit();
25
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();
26
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.
27
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
28
Connecting to the Database
29
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.