JDBC Database Connection in Java

JDBC Database Connection

JDBC Database Connection is essential for the development of the database driven applications. Users can insert,update, delete,select the items according to the users requirements.JDBC database connection in application system is important.Every application requires database processings.Here is the java database connection code with sql server.If you are applying this codes, then you will be able to connect sql database server.

JDBC Database Connection

In JDBC Database Connection,The JDBC API is a Java application interface that enables Java developers to develop DBMS-independent java application system. It consists of classes and interfaces for establishing connections with databases.Within the processes, it sends SQL statements to databases, processing the results of SQL statements and obtaining the database metadata.Driver,Connection,Statement,and ResultSet are four key interfaces which are needed to develop the java application.

First of all,We need to import the required driver for the database connection.This is fairly simple.To work with database, you need to import the database driver which should be placed at the very begining of the program. The sample code is illustrated below.

[java] import java.sql.*;
[/java]

In this example, I have illustrated the example with mysql database connection.Inside the public class,create the static final variables to hold the mysql jdbc driver,database url,username of the database,password of the database.Along with these variables, create the interface variables for connection,statement execution and resultset of the database which is demonstrated below.


[java] public class DatabaseConnect {
static final String JDBC_DRIVER=”com.mysql.jdbc.Driver”;
static final String DB_URL= “jdbc:mysql://localhost:3306/grading_db”;
static final String USER = “root”;
static final String PASS = “”;
private static Connection conn;
private static Statement stmt;
private static ResultSet rs;

public DatabaseConnect() throws SQLException,ClassNotFoundException{
//creating its constructor
try {
Class.forName(JDBC_DRIVER);
//loading of driver,JDBC Database Connection
conn = DriverManager.getConnection(DB_URL, USER , PASS);
stmt = conn.createStatement();
String sql;

sql=”CREATE TABLE IF NOT EXISTS `grading_db`.`Student_marks_ITC000` (`Student_ID` INT NOT NULL,”;
sql+=” `Name` VARCHAR(45) NOT NULL,”;
sql+= ” `Assignment1` DOUBLE NULL,”;
sql+= ” `Assignment2` DOUBLE NULL,”;
sql+= “`Assignment3` DOUBLE NULL,”;
sql+= “`Final` DOUBLE NULL,”;
sql+= ” PRIMARY KEY (`Student_ID`))”;
stmt.executeUpdate(sql);
//query string is executed.

stmt.close();
conn.close();

} catch (ClassNotFoundException ex) {
Logger.getLogger(DatabaseConnect.class.getName()).log(Level.SEVERE, null, ex);
}catch(SQLException e){
System.err.println(e);
}

}
[/java]

In jdbc database connection,a class is created which is DatabaseConnect.Inside this class,jdbc driver is assigned to the sting variable called JDBC_DRIVER.Previously,We have created database called grading_db.This database is assigned into the string variable called DB_URL.Along with Database,its user,username and password variables are also assigned to required variables.Then after,connection string,statement string and resultset string are created.

JDBC Database Connection
JDBC Database Connection

Buy this book for Resources
The one of the best resources in Java

This entry was posted in Uncategorized. Bookmark the permalink.

2 thoughts on “JDBC Database Connection in Java

  1. Suresh Kumar Thapa says:

    Dear sir,
    I am very proud of you. I read your articles and that made me crazy towards you and I want to meet with you so that I can learn Java programming technique along with MIT from you easily.

    Your Beloved
    Suresh Kumar Thapa
    Computer Engineer
    From Gamgadi, Mugu, Karnali, Mid-western Region, Nepal
    Contact No. 9849035719

    • admin says:

      Suresj Jee

      I am very glad to see your message in my inbox.Keep on communicating. I wish I could be there with you for fun moments.

      Kind Regards
      Anchal

Comments are closed.