Monday, August 31, 2020

Create File Java Connection Database with SQL Server (Sqljdbc)


What is SQL JDBC?

JDBC (Java Database Connectivity) is the Java API that manages connecting to a database, issuing queries and commands, and handling result sets obtained from the database. Released as part of JDK 1.1 in 1997, JDBC was one of the first components developed for the Java persistence layer.

How JDBC works

Developed as an alternative to the C-based ODBC (Open Database Connectivity) API, JDBC offers a programming-level interface that handles the mechanics of Java applications communicating with a database or RDBMS. The JDBC interface consists of two layers:

  1. The JDBC API supports communication between the Java application and the JDBC manager.
  2. The JDBC driver supports communication between the JDBC manager and the database driver.

JDBC is the common API that your application code interacts with. Beneath that is the JDBC-compliant driver for the database you are using.

Using JDBC to connect to a database

One of the fortunate facts of programming in the Java ecosystem is that you will likely find a stable JDBC database connector for whatever database you choose. In this tutorial we'll use SQLite to get to know JDBC, mainly because it's so easy to use.

  1. Install or locate the database you want to access.
  2. Include the JDBC library.
  3. Ensure the JDBC driver you need is on your classpath.
  4. Use the JDBC library to obtain a connection to the database.
  5. Use the connection to issue SQL commands.
  6. Close the connection when you're finished.


A. Download File Sqljdbc4.jar (Copy File Sqljdbc4.jar to folder library)

          
          link  1 : Download Here

link 2 : Download Here


B. Create File Java Connection Database with SQL Server ZK Framework (Sqljdbc4.jar)

SQLConnection.java

package com.apps.db;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.zkoss.zul.Messagebox;

public class DBSQLConnection {
    
    public Connection openConnection(){
        Connection Conn = null;
        try {

            Context initContext = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/apps");
            Conn = ds.getConnection();
            
        }catch (Exception e) {
            System.out.println("Error : " + e.getMessage());
            Messagebox.show("Connection Failed..: "+e.getMessage());
        }
        return Conn;
    }
}

 

C. Note 

 

No comments:

Post a Comment

Free Templates Source Code Simple Project Java ZK Framework

  Free Templates Source Code Simple Project Java ZK Framework   ZK Framework aims to combine the simplicity and security from its server-cen...