JDBC Notes

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

connect system/system

create user sreekanth identified by sreekanth;


grant connect,resource to sreekanth;
connect sreekanth/sreekanth;
show user;

select * from tab;

create user kesava identified by kesava;


grant connect,resource to kesava;
connect kesava/kesava;

show user;

include below path in classpath


oracle type iv driver jar file exist in below folder
C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar

open command prompt to check whether oracle type iv driver exist or not
javap oracle.jdbc.driver.OracleDriver

// to connect to oracle from jdbc program


// DriverTest.java
import java.sql.*;
public class DriverTest {
public static void main(String rags[]) throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "kesava",
"kesava");
System.out.println(con);
}
}

javac DriverTest.java
java DriverTest

create table in sqlprompt


create table student (
sno number primary key,
rollno varchar2(10),
name varchar2(20)
);

// InsertDemo.java
import java.sql.*;
public class InsertDemo {
public static void main(String rags[]) throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "kesava",
"kesava");
Statement stmt=con.createStatement();
int i=stmt.executeUpdate("insert into student values (1, '20195A1234',
'ABC')");
System.out.println(i+" row inserted");
}
}

// DynamicInsertDemo.java
import java.sql.*;
import java.util.*;
public class DynamicInsertDemo {
public static void main(String rags[]) throws ClassNotFoundException,
SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "kesava",
"kesava");
Statement stmt=con.createStatement();
Scanner sc=new Scanner(System.in);
int sno=sc.nextInt();
String rollno=sc.next();
String name=sc.next();
int i=stmt.executeUpdate("insert into student values
("+sno+",'"+rollno+"','"+name+"')");
System.out.println(i+" row inserted");

}
}

You might also like