Java Week 15
Java Week 15
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
JDBC JDBC
Resources Resources
JDBC
Resources
JAIN
DEEMED-TO-BE UNIVERSIT Y
JAIN
DEEMED-TO-BE UNIVERSIT Y
Native DBMS
Specific Call
JDBC Native ODBC
Call Call
SP API Oracle
ODBC API
JDBC API JDBC-
Java SQL
ODBC ODBC SP API
App
Bridge Driver Server
Driver
SP API Sybase
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
Client Side
Native
Call
JDBC Type-2
Call Driver
DBMS Specific
Native API
Java JDBC API
Database
App
Server
JAIN
DEEMED-TO-BE UNIVERSIT Y
JDBC
Call Middleware
Type-3
Server SP API Oracle
Driver
Java JDBC
Server SP API SQL
App Driver Server
SP API Sybase
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
Client Side
DB
Specific
JDBC Type-4
Call Driver
DBMS Specific
Java Network Protocol
JDBC API
App Database
Server
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
JAIN
DEEMED-TO-BE UNIVERSIT Y
JAIN
DEEMED-TO-BE UNIVERSIT Y
PreparedStatement
ResultSet
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
JAIN
DEEMED-TO-BE UNIVERSIT Y
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
For Type-1 driver, i.e., JDBC-ODBC Bridge Driver, the JDBC URL is:
jdbc: odbc: SuchitaDSN.
For Oracle Type-2 driver:
String dbName = "kogent";
String oracleURL = "jdbc:oracle:oci8:@" + dbName;
//oracleURL = "jdbc:oracle:oci8:@kogent"
For Oracle Type-4 driver:
String host = "localhost";
String dbName = "kogent";
int port = 1521;
String oracleURL = "jdbc:oracle:thin:@" + host + ":" + port + ":" +
dbName;
//oracleURL = "jdbc:oracle:thin:@192.168.1.123:1521:XE""
//Using executeQuery()
String query = "SELECT col1, col2, col3 FROM table_name";
ResultSet results = stmt.executeQuery(query);
//Using executeUpdate()
String query= "INSERT into table_name values (value1, value2, …, value
n)";
int count = stmt.executeUpdate(query);
while(results.next())
{
JAIN
DEEMED-TO-BE UNIVERSIT Y
con.close();
JAIN
DEEMED-TO-BE UNIVERSIT Y
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
JVM Database
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con= DriverManager.getConnection (url, "user", "password");
String query="insert into mytable values (?,?,?)";
//Step1: Get PreparedStatement object
PreparedStatement ps=con.prepareStatement (query);
ps.setInt(2,38);
ps.setDouble(3,158.75);
import java.sql.*;
public class PreparedStatementEx1 {
public static void main(String s[]) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con= DriverManager.getConnection (
"jdbc:oracle:thin:@localhost:1521:XE","scott","tiger");
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_
ONLY);
prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.
CONCUR_READ_ONLY);
JAIN
DEEMED-TO-BE UNIVERSIT Y
import java.sql.*;
import java.util.*;
import java.io.*;
public class ScrollableRSEx1 {
public static void main(String s[]) throws Exception {
Driver d= (Driver) (Class.forName(
"oracle.jdbc.driver.OracleDriver").newInstance());
Properties p=new Properties ();
p.put("user","scott");
p.put("password","tiger");
Connection con=d.connect("jdbc:oracle:thin:@
localhost:1521:XE",p);
Statement st= con.createStatement (
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
import java.util.*;
import java.io.*;
import java.sql.*;
public class ScrollableRSEx2 {
public static void main(String s[]) throws Exception {
Driver d= (Driver)(Class.forName(
"oracle.jdbc.driver.OracleDriver").newInstance());
Properties p=new Properties ();
p.put("user","scott");
p.put("password","tiger");
Connection con=d.connect
("jdbc:oracle:thin:@192.168.1.123:1521:XE ",p);
PreparedStatement ps= con.prepareStatement ("select * from
emp",
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs=ps.executeQuery();
System.out.println ("Index\tEmpNo\tName");
int i=1;
while (rs.next()){
System.out.print ((i++)+"\t");
System.out.print (rs.getInt(1)+"\t");
System.out.println (rs.getString(2));
}//while
System.out.println ();
//Now the cursor will be at afterLast
//From this point to move the cursor to 3rd record
rs.absolute(3);
System.out.println ("3rd record EmpNo : "+rs.getInt(1));
/*
To move the cursor to first record we can use
rs.first(); or with reference to the current row we can use
*/
rs.relative(-2);
System.out.println ("1st record EmpNo: "+rs.getInt(1));
JAIN
DEEMED-TO-BE UNIVERSIT Y
/*
To move the cursor to last record use rs.last () or with reference to the
current row we can use
*/
rs.afterLast (); //this moves the cursor to afterLast
rs.previous ();
//moves the cursor backward by one row (which in this case moves the
cursor last row
System.out.println ("last record EmpNo: "+ rs.getInt (1));
con.close ();
}//main
}//class
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
Statement s = conn.createStatement();
int rows = s.executeUpdate("INSERT INTO TABLE1 (COLUMN1) VALUES " +
"('FIRST')");
// set Savepoint
Savepoint sp = conn.setSavepoint("SAVEPOINT_1");
JAIN
DEEMED-TO-BE UNIVERSIT Y
package com.jdbc;
import java.sql.*;
import java.util.*;
import java.io.*;
/**
* @author Suchita
*/
public class TransferAmount {
public static void main(String s[]) throws Exception {
Driver d= (Driver) (Class.forName(
"oracle.jdbc.driver.OracleDriver").newInstance());
Properties p=new Properties();
p.put("user","scott");
p.put("password","tiger");
Connection con=d.
connect("jdbc:oracle:thin:@192.168.1.123:1521:XE",p);
con.setAutoCommit(false);
String srcaccno=s[0];
String destaccno=s[1];
PreparedStatement ps= con.prepareStatement(
"update bank set bal=bal+? where accno=?");
ps.setInt(1,500);
ps.setString(2,destaccno);
int i=ps.executeUpdate();
ps.setInt(1,-500);
ps.setString(2,srcaccno);
JAIN
DEEMED-TO-BE UNIVERSIT Y
int j=ps.executeUpdate();
if (i==1&&j==1){
con.commit();
System.out.println("Amount transfered");
con.close();
return;
}
con.rollback();
System.out.println("Cannot transfer the amount");
con.close();
}//main
}//class
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
JAIN
DEEMED-TO-BE UNIVERSIT Y
Conclusion
⚫
JAIN
DEEMED-TO-BE UNIVERSIT Y
⚫
JAINDEEMED-TO-BE UNIVERSIT Y
@
⚫
import java.sql.*;
public class JDBCExample {
static String url = "jdbc:mysql://localhost:3306/
testdb?useSSL=false";
JAIN
DEEMED-TO-BE UNIVERSIT Y
JDBCExample.show();
}
static void show()
{
ResultSet rs;
String name, address;
String sql1 = "Select Name, Age from Students where Name=?";
try(Connection con1 = DriverManager.getConnection(url, user,
password);
PreparedStatement pst1 = con1.prepareStatement(sql1)) {
pst1.setString(1,"John");
rs = pst1.executeQuery();
while (rs.next()) {
name = rs.getString(1);
address = rs.getString(2);
System.out.println("Student name = " + name + "Student address
= " + address);
}
} catch (SQLException ex) {
System.out.println(ex.toString());
}
}
}