JDBC
|
Hibernate
|
With JDBC, developer has to write code to map an object model's data representation to a relational data model and its corresponding database schema.
|
Hibernate itself takes care of this mapping using XML filesso developer does not need to write code for this.
|
With JDBC, the automatic mapping of Java objects with database tables and vice versa conversion is to be taken care of by the developer manually with lines of code.
|
Hibernate provides transparent persistence and developer does not need to write code explicitly to map database tables tuples to application objects during interaction with RDBMS.
|
JDBC supports only native Structured Query Language (SQL). Developer has to find out the efficient way to access database.
|
Hibernate provides HQL that is expressed in a familiar SQL like syntax. Hibernate also supports native SQL statements.
|
As table changes or database is changed then it’s essential to change object structure as well as to change code written to map table-to-object/object-to-table.
|
If there is change in Database or in any table then the only need to change XML fileproperties.
|
In JDBC, mapping between Java objects and database tables is done manually.
|
It relieves programmer from manual handling of persistent data, hence reducing the development time and maintenance cost.
|
With JDBC, caching is maintained by hand-coding.
|
Hibernate, with Transparent Persistence, cache is set to application work space. Relational tuples are moved to this cache as a result of query. Automatic Transparent Persistence allows the developer to concentrate more on business logic rather than this application code.
|
In JDBC there is no check that always every user has updated data. This check has to be added by the developer.
|
Hibernate enables developer to define version type field to application, due to this defined field Hibernate updates version field of database table every time relational tuple is updated in form of Java class object to that table.
|
Showing posts with label JDBC. Show all posts
Showing posts with label JDBC. Show all posts
Thursday, 19 January 2017
What is the advantage of Hibernate over JDBC?
Monday, 12 January 2015
MySQL and Java JDBC ,Connection to database with Java
By Sitansu S Swain
package com.demo.mysqlaccess;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySqlAccess {
private Connection connection = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception {
try {
// This will load the MySql Driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup connection with the DB
connection = DriverManager.getConnection("jdbc:mysql://localhost/springdemoproject?" + "user=root&password=");
// Statement allow to issue the Sql queries to the Database
statement = connection.createStatement();
// ResultSet gets the result of the Sql Query.
resultSet = statement.executeQuery("SELECT * FROM login");
writeResultSet(resultSet);
// preparestatement can use variables and more efficient
preparedStatement = connection.prepareStatement("insert into login values(default,?,?,?,?)");
// Here parameter start with index 1
preparedStatement.setString(1, "Sitansu");
preparedStatement.setString(2, "sitansudev");
preparedStatement.setInt(3, 97189);
preparedStatement.setString(4, "orissa");
preparedStatement.executeUpdate();
preparedStatement = connection.prepareStatement("select name,password from login");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
// Remove again the insert login
preparedStatement = connection.prepareStatement("delete from login where name=? ;");
preparedStatement.setString(1, "Sitansu");
preparedStatement.executeUpdate();
resultSet = preparedStatement.executeQuery("select * from login");
metaDateResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void metaDateResultSet(ResultSet resultSet) throws SQLException {
// Now get the metadata from thye database
System.out.println("The column in the table are....");
System.out.println("Table :" + resultSet.getMetaData().getTableName(1));
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
System.out.println("column :" + i + " " + resultSet.getMetaData().getColumnClassName(i));
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
// resultSet initialised before the first data set
while (resultSet.next()) {
// It is possible to get the columns via name
// also posssible to get the column via column no
// which starts at 1
// e.g., resultSet.getSTring(2);
System.out.println("User:" + resultSet.getString("name"));
System.out.println("password:" + resultSet.getString("password"));
}
}
// you need to close all three to make sure
private void close() {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.demo.mysqlaccess;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySqlAccess {
private Connection connection = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception {
try {
// This will load the MySql Driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup connection with the DB
connection = DriverManager.getConnection("jdbc:mysql://localhost/springdemoproject?" + "user=root&password=");
// Statement allow to issue the Sql queries to the Database
statement = connection.createStatement();
// ResultSet gets the result of the Sql Query.
resultSet = statement.executeQuery("SELECT * FROM login");
writeResultSet(resultSet);
// preparestatement can use variables and more efficient
preparedStatement = connection.prepareStatement("insert into login values(default,?,?,?,?)");
// Here parameter start with index 1
preparedStatement.setString(1, "Sitansu");
preparedStatement.setString(2, "sitansudev");
preparedStatement.setInt(3, 97189);
preparedStatement.setString(4, "orissa");
preparedStatement.executeUpdate();
preparedStatement = connection.prepareStatement("select name,password from login");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);
// Remove again the insert login
preparedStatement = connection.prepareStatement("delete from login where name=? ;");
preparedStatement.setString(1, "Sitansu");
preparedStatement.executeUpdate();
resultSet = preparedStatement.executeQuery("select * from login");
metaDateResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void metaDateResultSet(ResultSet resultSet) throws SQLException {
// Now get the metadata from thye database
System.out.println("The column in the table are....");
System.out.println("Table :" + resultSet.getMetaData().getTableName(1));
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
System.out.println("column :" + i + " " + resultSet.getMetaData().getColumnClassName(i));
}
}
private void writeResultSet(ResultSet resultSet) throws SQLException {
// resultSet initialised before the first data set
while (resultSet.next()) {
// It is possible to get the columns via name
// also posssible to get the column via column no
// which starts at 1
// e.g., resultSet.getSTring(2);
System.out.println("User:" + resultSet.getString("name"));
System.out.println("password:" + resultSet.getString("password"));
}
}
// you need to close all three to make sure
private void close() {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.demo.mysqlaccess;
public class TestDataBase {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
MySqlAccess mySqlAccess = new MySqlAccess();
mySqlAccess.readDataBase();
}
}
Subscribe to:
Posts (Atom)