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

1st Unit Programs PDF

The document contains 6 sections that demonstrate how to perform common database operations using JDBC including: 1) Connecting to an Oracle database 2) Creating a new database table 3) Inserting records into the database table 4) Updating records in the database table 5) Deleting records from the database table 6) Reading data from the database table using a ResultSet object to retrieve and display the records

Uploaded by

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

1st Unit Programs PDF

The document contains 6 sections that demonstrate how to perform common database operations using JDBC including: 1) Connecting to an Oracle database 2) Creating a new database table 3) Inserting records into the database table 4) Updating records in the database table 5) Deleting records from the database table 6) Reading data from the database table using a ResultSet object to retrieve and display the records

Uploaded by

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

Q1.Jdbc program to connect the oracle database.

import java.sql.*;
class OracleXEDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connected");
Connection connection =
DriverManager.getConnection("jdbc:odbc:oracleXE","system","password");
System.out.println("The connection object is:"+connection);
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Q2.Create a new Database Table using JDBC
import java.sql.*;
class CreateTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("create table student(rollno int,name varchar(15))");
System.out.println("table created");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Q3.Jdbc Program to insert records into Database.

import java.sql.*;
class InsertTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("insert into student values(101,'mukesh')");
st.executeUpdate("insert into student values(102,'sukesh')");
st.executeUpdate("insert into student values(103,'rakesh')");
st.executeUpdate("insert into student values(104,'ramesh')");
st.executeUpdate("insert into student values(105,'suresh')");
st.executeUpdate("insert into student values(106,'nikesh')");
System.out.println("Values Inserted");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Q4.JDBC program to update records in database

import java.sql.*;
class UpdateTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("update student set rollno=107 where name='nikesh'");
System.out.println("Table Updated");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Q5.JDBC program to delete records from database/

import java.sql.*;
class DeleteTableRow
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("delete from student where name='nikesh'");
System.out.println("Row Deleted");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Q6.JDBC program to read the data from database using ResultSet.

import java.sql.*;
class ResultSetDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from student");
while(rs.next())
{
int i=rs.getInt(1);
String s=rs.getString(2);
System.out.println("Rollno="+i+"\t"+"Name="+s);
}
rs.close();
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

You might also like