Вы находитесь на странице: 1из 5

UNIT-III

1.JDBC program to insert,delete,update and select data from the database

import java.sql.*;

public class CreateTableExample

try

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

catch (ClassNotFoundException e)

System.out.println("Unable To Load The Driver class");

public static void main(String[] args)

Connection con = null;

Statement stmt = null;

try

String URL = "jdbc:oracle:thin:@localhost:1521:XE";

String username = "username";

String password = "password";


con = DriverManager.getConnection(URL, username, password);

stmt = con.createStatement();

String sql = "CREATE TABLE EMPLOYEE(" +


"ID NUMBER NOT NULL, " +
"FIRST_NAME VARCHAR2(200), " +
"LAST_NAME VARCHAR2(200), " +
"DISIGNATION VARCHAR2(200))";

int i = stmt.executeUpdate(sql);

if(i == 0)

System.out.println("Table is created");

else

System.out.println("Table is not created");

catch (SQLException e)

e.printStackTrace();

try

String sql = "INSERT INTO EMPLOYEE VALUES" +


"(111, 'Navin', 'Sharma', 'CEO')";

int i = stmt.executeUpdate(sql);

if(i != 0)

System.out.println("Row is created");

else

System.out.println("Row is not created");

String sql = "SELECT * FROM EMPLOYEE";

rs = stmt.executeQuery(sql);

while (rs.next())

System.out.println("ID :"+rs.getInt(1));

System.out.println("First Name : "+rs.getString(2));

System.out.println("Last Name :"+rs.getString(3));

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

System.out.println("-------------------");

}
catch (SQLException e)

e.printStackTrace();

String sql = "UPDATE EMPLOYEE SET FIRST_NAME='Rakesh', " +


"LAST_NAME='Malhotra' WHERE DISIGNATION='CEO'";

int i = stmt.executeUpdate(sql);

if(i != 0)

System.out.println("Record is updated");

else

System.out.println("Record is not updated");

String sql = "DELETE FROM EMPLOYEE WHERE ID=111";

int i = stmt.executeUpdate(sql);

if(i != 0)

System.out.println("Record is deleted");

else

{
System.out.println("Record is not deleted");

Вам также может понравиться