+92 332 4229 857 99ProjectIdeas@Gmail.com

Database Connectivity In Java


Database Connection Steps:


Import required package
   import java.sql.*;
Load Driver
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Define Connection URL
   String connectionUrl = "jdbc:odbc:Customer";//Customer is a database name
Establishing a Connection
   Connection con = null;
   con=DriverManager.getConnection (connectionUrl , "userName","password");
Create a Statement Object
   Statement st = con.createStatement();
Execute Query
   String sql = "SELECT * FROM CustomerTable";//Selecting all customerTableData
    ResultSet rs = st.executeQuery(sql); //Storing customerTableData to ResultSet(it is also a table)
Process Results
 while ( rs.next() ) {//for going to next rows of Table(ResultSet)
    String name = rs.getString("name");//columnName of customerDataTable
    String add = rs.getString("address");//columnName of customerDataTable
    String pNum = rs.getString("phoneNum");//columnName of customerDataTable
         System.out.println(name + " " + add + " " + pNum);
           }
Close Connection
   con.close();



Code

Java Connection with Access Database:

Example Code:

import java.sql.*;
import java.util.*;

public class AccessDatabaseConnection {
   Connection con = null;
     public void dbConnection() {
       try {
           String connectionUrl = "jdbc:odbc:Person";//Person is a database name
           String driver = "sun.jdbc.odbc.JdbcOdbcDriver";//Driver to connect ODBC
           Class.forName(driver);
           String userName = "Rehan";
           String password = "mascot";
           con = DriverManager.getConnection(connectionUrl, userName, password);
 //if you don’t have a userName and password means database is not password protected.
con = DriverManager.getConnection(connectionUrl, "", "");
       } catch (SQLException e) {
               e.printStackTrace();
       } finally {
               con.close();
       }
   }
}




Java Connection with SQL Server:

Example Code:

import java.sql.*;


public boolean openConnection(String serverAddress, String databaseName, String userName, String password) {
 //String connect to SQL server
  String connectionUrl = "jdbc:microsoft:sqlserver://" + serverAddress + ":1433" +
              ";DatabaseName=" + databaseName;
        try {
           //Loading the driver...
           Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
           //Buiding a Connection
           Connection con = DriverManager.getConnection(connectionUrl, userName, password);
                  if(con == null )
                     return false;
            } catch (SQLException e ) {
       
            } catch (ClassNotFoundException e) {}
             
            return true;
       }


Java Connection with MYSQL:

Example Code:


import java.sql.*;
import java.util.*;


public static void main(String[] args) {
Connection conn = null;
    try {
         //String connect to MYSQL server
        String connectionUrl = "jdbc:mysql://localhost/test";//test is the database name
       
        //Loading MySql Driver
        Class.forName("com.mysql.jdbc.Driver");
       
        String userName = "root";
        String password = "";
        conn = DriverManager.getConnection(connectionUrl, userName, password);
        System.out.println ("Database Connection Successfully");
   
    } catch (Exception e) {
   
       System.out.println ("Cannot connect to database server");
   
    } finally {
       
       if (conn != null) {
            try {
                conn.close();
                System.out.println ("Database connection terminated");
            } catch (Exception e) {}
        }
    }
  }
}

Java Connection with Oracle:

Example Code:

import java.sql.*;
import java.util.*;
public static void main(String[] args) {
    Connection con = null;
        try {
            //String connect to Oracle Database
            String connectionUrl = "jdbc:oracle:thin:@//server.local:1521/prod"; //prod is a database name
            //Loading Oracle Driver
              Class.forName("oracle.jdbc.driver.OracleDriver");         
              con = DriverManager.getConnection(connectionUrl,"","");
             
       } catch (ClassNotFoundException e) {
             
              e.printStackTrace();
      
       } catch (SQLException e) {
      
              e.printStackTrace();
       }
   }
}

0 comments: