Java connection and database (mysql, oracle, sql server)

Tuesday, January 12th, 2016 (0 Comment)

Today, most of computer programs are more or less related to the accessing of information in the database. That is why the supporting database programming is an indispensable function of the modern programming languages, including Java. JDBC API library contains classes and interfaces support the Java programmer connection and access to the database system.

JDBC API provides a mechanism that allows a program written in Java capable independent with the database system, have access to the system different databases without rewriting the program. JDBC is to simplify the creation and execution of SQL queries in the program. Structure of JDBC is similar as ODBC architecture built by Microsoft.

Java connection with the database using JDBC is done in two steps: registration JDBC driver; Next execute the getConnection method of DriverManager class.

1. Registration JDBC driver

JDBC drivers are loaded when bytecode is loaded into the JVM. A simple way to accomplish this task is implement the method Class.forName (“<JDBC Driver>”).

Example: to load drivers using JDBC libraries provided by Sun, we use the following command Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”).

2. Execute Java Connection

After you have loaded the JDBC driver, the connection to the database is done with one of the following methods in DriverManager class:

  • public static Connection getConnection(String url) throws SQLException.
  • public static Connection getConnection(String url, String user, String pass) throws SQLException.
  • public static Connection getConnection(String url, Properties info) throws SQLException.

The results returned by the method above is an object of java.sql.Connection class, it used to represent the connection to the database.

3. Practical detail

3.1. Create table in database with following script

Oracle Database

SQL Server Database

Mysql Database

3.2. Process java connection database

You need to import JDBC library for database

  • Oracle: ojdbc16.jar or ojdbc6.jar
  • SQL Server: sqljdbc4-4.0.jar
  • Mysql: mysql-connector-java-5.1.6.jar

Step 1: Declare the database java connection parameters

Step 2: Establish a connection and view information database

Step 3: Execute queries using Statement object

Step 4: Execute insert data using PreparedStatement object

Step 5: close connection

3.3. Transaction

Default of query execution is completed, automatically updated in the database (commit) –> called auto-commit mode.

The transaction is enabling one or multiple statements to be grouped into a single transaction. May be change auto-commit mode to hide the status (false).

 

Tags: , , ,

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Related Posts

core java
Export Crystal Report with Subreport in java
core java
Convert xml to object java and vice versa
core java
Java basic tutorial on lvtutorial.com
core java
Java hibernate manytomany to manage user role

Leave a Reply

Your email address will not be published. Required fields are marked *