hibernate.docx - What is JDBC JDBC stands for Java Database...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
What is JDBC? JDBC stands for Java Database Connectivity and provides a set of Java API for accessing he relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL compliant database. JDBC provides a flexible architecture to write a database independent application that can run on different platforms and interact with different DBMS without any modification. Pros and Cons of JDBC Pros of JDBC Cons of JDBC Clean and simple SQL processing Good performance with large data Very good for small applications Simple syntax so easy to learn Complex if it is used in large projects Large programming overhead No encapsulation Hard to implement MVC concept Query is DBMS specific Example to connect to the Oracle database in java For connecting java application with the oracle database, you need to follow 5 steps to perform database connectivity. In this example we are using Oracle10g as the database. So we need to know following information for the oracle database: 1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver . 2. Connection URL: The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver, localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port number and XE is the Oracle service name. You may get all these information from the tnsnames.ora file. 3. Username: The default username for the oracle database is system . 4. Password: Password is given by the user at the time of installing the oracle database. Let's first create a table in oracle database.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
create table emp(id number(10),name varchar2(40),age number(3)); In this example, system is the username and oracle is the password of the Oracle database. import java.sql.*; class OracleCon{ public static void main(String args[]){ try{ //step1 load the driver class Class.forName("oracle.jdbc.driver.OracleDriver"); //step2 create the connection object Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe"," system","oracle"); //step3 create the statement object Statement stmt=con.createStatement(); //step4 execute query ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); //step5 close the connection object con.close(); }catch(Exception e){ System.out.println(e);}
Image of page 2
} } Why Object Relational Mapping (ORM)? When we work with an object-oriented systems, there's a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object- oriented languages, such as Java or C# represent it as an interconnected graph of objects.
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern