JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to create JDBC Connection?


Description:

Steps to create JDBC connection:

  • Register the database driver with java.sql.DriverManager, where DriverManager is a class which is given under JDBC specifications. You can do this by loading driver implementation class into the JVM by using Class.forName().
  • Open a session to database, i.e. establish a connection to database. You can do this by calling DriverManager.getConnection() method, which returns Connection object.


Code:
package com.java2novice.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcConnection {

	public static void main(String a[]){
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection con = DriverManager.
				getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
					,"user","password");
			Statement stmt = con.createStatement();
			System.out.println("Created DB Connection....");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
Next Program >>
blog comments powered by Disqus

List Of All JDBC Programs:

  1. How to create JDBC Connection?
  2. What is Statement & ResultSet in JDBC?
  3. How to execute and read select queries using JDBC?
  4. How to update a record in the database using JDBC?
  5. How to execute any type of query in JDBC?
  6. What are the types of JDBC Statements available?
  7. Write an example code for JDBC prepared statement.
  8. Write an example for JDBC prepared statement with ResultSet.
  9. How to get primary key value (auto-generated keys) from inserted queries using JDBC?
  10. Write a simple program for CallableStatement statement to execute stored procedure.
  11. Write a program for CallableStatement statement with stored procedure returns OUT parameters.
  12. Write a program for CallableStatement statement with batch execution.
  13. Write a program to execute SQL function using CallableStatement.
  14. Write a program to execute SQL cursors using CallableStatement.
  15. How to get column properties from ResultSet using ResultSetMetaData?
  16. Write an example for batch update using Statement.
  17. Write an example for batch update using PreparedStatement.
  18. What are the types of ResultSets in JDBC?
  19. Write an example for scrollable result set with read only mode.
  20. Write an example for updatable result set.
  21. How to insert an image into database table? or Write an example for inserting BLOB into table.
  22. How to read an image from database table? or Write an example for reading BLOB from table.
  23. What is DatabaseMetaData? Write an example code.
  24. How to get JDBC Connection object using properties file?
Knowledge Centre
Different types of Access Modifiers
public: Any thing declared as public can be accessed from anywhere.

private: Any thing declared as private can't be seen outside of its class.

protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.

default modifier: Can be accessed only to classes in the same package.

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 6 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2014 by Nataraja Gootooru. All Rights Reserved.