Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views6 pages

JDBC Programs

The document contains Java code examples demonstrating database connectivity using JDBC with Oracle, MySQL, and Microsoft Access. It includes examples of executing SQL queries, using callable statements for stored procedures, and fetching specific records from a result set. Each section provides a brief overview of the connection setup and query execution process for different database systems.

Uploaded by

naryugam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

JDBC Programs

The document contains Java code examples demonstrating database connectivity using JDBC with Oracle, MySQL, and Microsoft Access. It includes examples of executing SQL queries, using callable statements for stored procedures, and fetching specific records from a result set. Each section provides a brief overview of the connection setup and query execution process for different database systems.

Uploaded by

naryugam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

//Java Application with Oracle database

import java.sql.*;

class OracleCon{

public static void main(String args[]){

try{

//step1 load the driver class

Class.forName("oracle.jdbc.driver.OracleDriver");

//step2 create the connection object

Connection con=DriverManager.getConnection(

"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");

//step3 create the statement object

Statement stmt=con.createStatement();

//step4 execute query

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));

//step5 close the connection object

con.close();

}catch(Exception e){ System.out.println(e);}


}

//Java Database Connectivity with MySQL

import java.sql.*;

class MysqlCon{

public static void main(String args[]){

try{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(

"jdbc:mysql://localhost:3306/sonoo","root","root");

//here sonoo is database name, root is username and password

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));

con.close();

}catch(Exception e){ System.out.println(e);}

// Connectivity with Access

// Without fan

import java.sql.*;

class Test{

public static void main(String ar[]){


try{

String database="student.mdb";//Here database exists in the current directory

String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};

DBQ=" + database + ";DriverID=22;READONLY=true";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection c=DriverManager.getConnection(url);

Statement st=c.createStatement();

ResultSet rs=st.executeQuery("select * from login");

while(rs.next()){

System.out.println(rs.getString(1));

}catch(Exception ee){System.out.println(ee);}

}}

// With dsn

import java.sql.*;

class Test{

public static void main(String ar[]){

try{

String url="jdbc:odbc:mydsn";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection(url);

Statement st=c.createStatement();

ResultSet rs=st.executeQuery("select * from login");

while(rs.next()){

System.out.println(rs.getString(1));

}catch(Exception ee){System.out.println(ee);}

}}

// Stored procedure and callable statement

import java.sql.CallableStatement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

public class CallingProcedure {

public static void main(String args[]) throws SQLException {

//Registering the Driver

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

//Getting the connection

String mysqlUrl = "jdbc:mysql://localhost/sampleDB";

Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");

System.out.println("Connection established......");
/Preparing a CallableStateement

CallableStatement cstmt = con.prepareCall("{call myProcedure()}");

//Retrieving the result

ResultSet rs = cstmt.executeQuery();

while(rs.next()) {

System.out.println("Product Name: "+rs.getString("Product_Name"));

System.out.println("Date of Dispatch: "+rs.getDate("Date_Of_Dispatch"));

System.out.println("Time of Dispatch: "+rs.getTime("Time_Of_Dispatch"));

System.out.println("Location: "+rs.getString("Location"));

System.out.println();

// Result set

import java.sql.*;

class FetchRecord{

public static void main(String args[])throws Exception{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");

Statement
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs=stmt.executeQuery("select * from emp765");

//getting the record of 3rd row


rs.absolute(3);

System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));

con.close();

}}

You might also like