123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> General >> View Article

How To Connect Database In Java?

Profile Picture
By Author: infocampus
Total Articles: 33
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

For connecting a database with the application 5 steps are given -
1) Register the driver class
2) Creating connection
3)Creating statement
4)Executing queries
5) Closing connection
1) Register the driver class
To register the driver class forName() method of Class class is used. This method is mainly used to dynamically load the driver class.
Syntax :
public static void forName(String className)throws
ClassNotFoundException

Example to register the Oracle driver class :

1. Class.forName("oracle.jdbc.driver.OracleDriver");
2. Java/j2ee classes Bangalore
2) Create connection object
To establish connection with the database getConnection() method of DriverManager class is used.
Syntax :
1. 1) public static Connection getConnection(String url)throws SQLException
2. 2) public static Connection getConnection(String url,String name,
3. String password) throws SQLException
Example to establish connection with the Oracle database
1. Connection con=DriverManager.getConnection
2. ...
... ( "jdbc:oracle:thin:@localhost:1521:xe","system","password");
3) Create Statement object
To create statement object The createStatement() method of Connection interface is used. The object of statement is mainly used to execute queries with the database. Advanced java training in bangalore

1. Syntax : public Statement createStatement()throws SQLException
Example to create the statement object
1. Statement stmt=con.createStatement();
4) Execute the query
To execute queries to the database executeQuery() method of Statement interface is used. This method returns the object of ResultSet that can be used to get all the records of a table.
Syntax of executeQuery() method
1. public ResultSet executeQuery(String sql)throws SQLException
Example to execute query
1. ResultSet rs=stmt.executeQuery("select * from emp");
2.
3. while(rs.next()){
4. System.out.println(rs.getInt(1)+" "+rs.getString(2));
5. }
5) Close the connection object
By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close java course in Bangalore the connection.

Syntax :
public void close()throws SQLException
Example to close connection
con.close();
Connection with Oracle database :
To connect java application with the oracle database, we need to follow 5 steps to perform database connectivity. Before that some information is needed :
1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
2. Connection URL: The connection URL for the oracle 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, , 1521 is the port number and XE is the Oracle service name. Username: The default username for the oracle database is system. Java course in Bangalore
3. Password: Password is given by the user at the time of installing the oracle database.
Steps :
1. step1 : load the driver class
2. Class.forName("oracle.jdbc.driver.OracleDriver");
1. step2: create the connection object
2. Connection con=DriverManager.getConnection
3. ( "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
4.
5. step3: create the statement object
6. Statement stmt=con.createStatement();
7.
8. step4: execute query
9. ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
10.
step5: close the connection object
11. con.close();
What is JDBC Drivers ? best core java training in bangalore
JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
1) JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is now discouraged because of thin driver.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.
3) Network Protocol driver
The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java. Core and advanced java institute in marathahalli
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as thin driver. It is fully written in Java language.
3. This is the short description about the JDBC connectivity . To get the more knowledge on java for getting a job as java developer join Infocampus Software Training Institute .

Author : Infocampus is the best institute for the java/j2ee classes Bangalore . It provides Advanced java training with the 100% job support . 3 days free demo classes are available for java training center Bangalore . Java course is provided by the more than 10 years experienced trainers .
Every weekend free demo classes are available. Syllabus is according to getting a job easily . Infocampus provides best core java training in bangalore with the real time project .
Learn OOPs concept , packages , multithreading , JDBC connectivity , Annotations , hibernate framework in Core & Advanced java class .
To book the free demo class call at : 9738001024 or to get the syllabus for java training enquire at : http://www.infocampus.co.in/java-training-bangalore.html

Total Views: 390Word Count: 877See All articles From Author

Add Comment

General Articles

1. Magento Developers Melbourne: Your Trusted Partner For Magento Web Development
Author: themerchantbuddy

2. How Can E-commerce App Development Help Your Business?
Author: Comfygen

3. The Best Baby Cot Mattress For Summer In Australia: Keep Your Baby Cool And Comfortable !
Author: Milari Organics

4. How Might An Amazon Fba Prep Service Center Help You Streamline Your Business?
Author: 3pshipping6

5. Reviving Artisanal Craftsmanship – Why Handmade Products Matter More Than Ever
Author: Chaitanya Kumari

6. What To Look For In A New York Labor And Employment Law Firm
Author: jewellansing792

7. 50 Years Of Long-lasting Performance
Author: Busch Vacuum Solutions

8. Optimize Healthcare Revenue Cycle: Strategies For Financial Success
Author: Albert brown

9. Offres Exclusives Pour Vos Voyages En Inde Du Nord — Économisez Jusqu’à 30% — Réservez Dès Maintenant!
Author: yatika

10. 200-hour Weekend Yoga Teacher Training Course (yttc) In Bengaluru: A Pathway To Mastery In Yoga
Author: Yogakulam Academy

11. Understanding Patient Behavior: The Importance Of Market Research In Healthcare Marketing
Author: Adomantra

12. Ensuring Safety And Privacy: The Advantages Of Close Protection Services In Dubai
Author: Shancy

13. Thermodynamic Steam Traps: A Compact Solution For Maximum Performance
Author: David John

14. Best Accounting Service In California – Mj Financials
Author: maria jeffery

15. Best Astrologer In Hosahalli
Author: Pandith Keshav Das

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: