Java/JDBC

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Data Types, Variables,and Arrays
Operators
Packages, Classes, and Interfaces
Java Program
Modifiers
Control Statements
Exception Handling
Object Oriented Programming
Wrappers
Strings
Math
Arrays
Random Numbers
Date and Time
Regular Expressions
Java Collections
Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
RMI
Image, Audio, Video
GUI
Applets
Internationalization
JDBC
* Database Connection
* Executing Queries
* Advanced Concepts
Java and XML
JDBC (Java Database Connectivity) is a framework provided by Sun Microsystems as part of JDK platform. The main purpose of this framework is a provide an abstraction for database connectivity. Also JDBC was formed to form a standard for java database application. It defines many interfaces like an example java.sql.Connection, java.sql.Statement, java.sql.ResultSet which should be implemented by the database provider like Oracle, Mysql, sybase, MS Sql, DB2 etc. We achieve a lot by using JDBC and the foremost achievement would be maintainability. Same code can be executed if properly design on Oracle, Mysql and others. Of course database tables and structures should be same everywhere in order to run the same code.

In our examples we will use Mysql as a database since Mysql is an open source project and it is freely available on net.

How to connect your application with Mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class JDBCConnectionExample {
 
	public static void main(String[] args) throws SQLException,
			ClassNotFoundException {
 
		// load the jdbc driver. Every database has it's
		// own driver.
		Class.forName("com.mysql.jdbc.Driver");
 
		// Provide the host url of your database,
		// username and password. In case of Oracle
		// it is like jdbc:oracle:thin:@127.0.0.1:1521:orcl.
		// So depending upon your database location and
		// schema name you have to set the configurations
		// like above. In case of mysql please refer
		// the settings below. Provide valid user
		// name and password.
		Connection conn = DriverManager.getConnection(
				"jdbc:mysql://localhost/database_name", 
				"dummy_user_name",
				"dummy_password");
	}
}

I know this URL jdbc:oracle:thin:@127.0.0.1:1521:orcl seem to be little confusing to you. So I would like to take some of your time to explain it to you.

jdbc - prefix used to present that it is a JDBC url.

oracle - which database provider it refers to. In case of Mysql it will be "mysql".

thin - presents which kind of driver to use. There are four type of drivers.

127.0.0.1:1521 - IP address and port number on which the database is listening for connection.

orcl - SID name. It is actually the database instance name.

java.sql.DriverManager
DriverManager is a concrete class in JDBC that is based on Factory Pattern to return the appropriate connection to the user. Programmer needs to register the driver


java.sql.Connection
Personal tools